rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include <linux/delay.h> |
| 2 | #include <linux/pci.h> |
| 3 | #include <linux/module.h> |
| 4 | #include <linux/sched/signal.h> |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/ioport.h> |
| 7 | #include <linux/wait.h> |
| 8 | |
| 9 | #include "pci.h" |
| 10 | |
| 11 | /* |
| 12 | * This interrupt-safe spinlock protects all accesses to PCI |
| 13 | * configuration space. |
| 14 | */ |
| 15 | |
| 16 | DEFINE_RAW_SPINLOCK(pci_lock); |
| 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 | #ifdef CONFIG_PCI_LOCKLESS_CONFIG |
| 29 | # define pci_lock_config(f) do { (void)(f); } while (0) |
| 30 | # define pci_unlock_config(f) do { (void)(f); } while (0) |
| 31 | #else |
| 32 | # define pci_lock_config(f) raw_spin_lock_irqsave(&pci_lock, f) |
| 33 | # define pci_unlock_config(f) raw_spin_unlock_irqrestore(&pci_lock, f) |
| 34 | #endif |
| 35 | |
| 36 | #define PCI_OP_READ(size, type, len) \ |
| 37 | int pci_bus_read_config_##size \ |
| 38 | (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \ |
| 39 | { \ |
| 40 | int res; \ |
| 41 | unsigned long flags; \ |
| 42 | u32 data = 0; \ |
| 43 | if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \ |
| 44 | pci_lock_config(flags); \ |
| 45 | res = bus->ops->read(bus, devfn, pos, len, &data); \ |
| 46 | *value = (type)data; \ |
| 47 | pci_unlock_config(flags); \ |
| 48 | return res; \ |
| 49 | } |
| 50 | |
| 51 | #define PCI_OP_WRITE(size, type, len) \ |
| 52 | int pci_bus_write_config_##size \ |
| 53 | (struct pci_bus *bus, unsigned int devfn, int pos, type value) \ |
| 54 | { \ |
| 55 | int res; \ |
| 56 | unsigned long flags; \ |
| 57 | if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \ |
| 58 | pci_lock_config(flags); \ |
| 59 | res = bus->ops->write(bus, devfn, pos, len, value); \ |
| 60 | pci_unlock_config(flags); \ |
| 61 | return res; \ |
| 62 | } |
| 63 | |
| 64 | PCI_OP_READ(byte, u8, 1) |
| 65 | PCI_OP_READ(word, u16, 2) |
| 66 | PCI_OP_READ(dword, u32, 4) |
| 67 | PCI_OP_WRITE(byte, u8, 1) |
| 68 | PCI_OP_WRITE(word, u16, 2) |
| 69 | PCI_OP_WRITE(dword, u32, 4) |
| 70 | |
| 71 | EXPORT_SYMBOL(pci_bus_read_config_byte); |
| 72 | EXPORT_SYMBOL(pci_bus_read_config_word); |
| 73 | EXPORT_SYMBOL(pci_bus_read_config_dword); |
| 74 | EXPORT_SYMBOL(pci_bus_write_config_byte); |
| 75 | EXPORT_SYMBOL(pci_bus_write_config_word); |
| 76 | EXPORT_SYMBOL(pci_bus_write_config_dword); |
| 77 | |
| 78 | int pci_generic_config_read(struct pci_bus *bus, unsigned int devfn, |
| 79 | int where, int size, u32 *val) |
| 80 | { |
| 81 | void __iomem *addr; |
| 82 | |
| 83 | addr = bus->ops->map_bus(bus, devfn, where); |
| 84 | if (!addr) { |
| 85 | *val = ~0; |
| 86 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 87 | } |
| 88 | |
| 89 | if (size == 1) |
| 90 | *val = readb(addr); |
| 91 | else if (size == 2) |
| 92 | *val = readw(addr); |
| 93 | else |
| 94 | *val = readl(addr); |
| 95 | |
| 96 | return PCIBIOS_SUCCESSFUL; |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(pci_generic_config_read); |
| 99 | |
| 100 | int pci_generic_config_write(struct pci_bus *bus, unsigned int devfn, |
| 101 | int where, int size, u32 val) |
| 102 | { |
| 103 | void __iomem *addr; |
| 104 | |
| 105 | addr = bus->ops->map_bus(bus, devfn, where); |
| 106 | if (!addr) |
| 107 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 108 | |
| 109 | if (size == 1) |
| 110 | writeb(val, addr); |
| 111 | else if (size == 2) |
| 112 | writew(val, addr); |
| 113 | else |
| 114 | writel(val, addr); |
| 115 | |
| 116 | return PCIBIOS_SUCCESSFUL; |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(pci_generic_config_write); |
| 119 | |
| 120 | int pci_generic_config_read32(struct pci_bus *bus, unsigned int devfn, |
| 121 | int where, int size, u32 *val) |
| 122 | { |
| 123 | void __iomem *addr; |
| 124 | |
| 125 | addr = bus->ops->map_bus(bus, devfn, where & ~0x3); |
| 126 | if (!addr) { |
| 127 | *val = ~0; |
| 128 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 129 | } |
| 130 | |
| 131 | *val = readl(addr); |
| 132 | |
| 133 | if (size <= 2) |
| 134 | *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1); |
| 135 | |
| 136 | return PCIBIOS_SUCCESSFUL; |
| 137 | } |
| 138 | EXPORT_SYMBOL_GPL(pci_generic_config_read32); |
| 139 | |
| 140 | int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn, |
| 141 | int where, int size, u32 val) |
| 142 | { |
| 143 | void __iomem *addr; |
| 144 | u32 mask, tmp; |
| 145 | |
| 146 | addr = bus->ops->map_bus(bus, devfn, where & ~0x3); |
| 147 | if (!addr) |
| 148 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 149 | |
| 150 | if (size == 4) { |
| 151 | writel(val, addr); |
| 152 | return PCIBIOS_SUCCESSFUL; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * In general, hardware that supports only 32-bit writes on PCI is |
| 157 | * not spec-compliant. For example, software may perform a 16-bit |
| 158 | * write. If the hardware only supports 32-bit accesses, we must |
| 159 | * do a 32-bit read, merge in the 16 bits we intend to write, |
| 160 | * followed by a 32-bit write. If the 16 bits we *don't* intend to |
| 161 | * write happen to have any RW1C (write-one-to-clear) bits set, we |
| 162 | * just inadvertently cleared something we shouldn't have. |
| 163 | */ |
| 164 | dev_warn_ratelimited(&bus->dev, "%d-byte config write to %04x:%02x:%02x.%d offset %#x may corrupt adjacent RW1C bits\n", |
| 165 | size, pci_domain_nr(bus), bus->number, |
| 166 | PCI_SLOT(devfn), PCI_FUNC(devfn), where); |
| 167 | |
| 168 | mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8)); |
| 169 | tmp = readl(addr) & mask; |
| 170 | tmp |= val << ((where & 0x3) * 8); |
| 171 | writel(tmp, addr); |
| 172 | |
| 173 | return PCIBIOS_SUCCESSFUL; |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(pci_generic_config_write32); |
| 176 | |
| 177 | /** |
| 178 | * pci_bus_set_ops - Set raw operations of pci bus |
| 179 | * @bus: pci bus struct |
| 180 | * @ops: new raw operations |
| 181 | * |
| 182 | * Return previous raw operations |
| 183 | */ |
| 184 | struct pci_ops *pci_bus_set_ops(struct pci_bus *bus, struct pci_ops *ops) |
| 185 | { |
| 186 | struct pci_ops *old_ops; |
| 187 | unsigned long flags; |
| 188 | |
| 189 | raw_spin_lock_irqsave(&pci_lock, flags); |
| 190 | old_ops = bus->ops; |
| 191 | bus->ops = ops; |
| 192 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
| 193 | return old_ops; |
| 194 | } |
| 195 | EXPORT_SYMBOL(pci_bus_set_ops); |
| 196 | |
| 197 | /* |
| 198 | * The following routines are to prevent the user from accessing PCI config |
| 199 | * space when it's unsafe to do so. Some devices require this during BIST and |
| 200 | * we're required to prevent it during D-state transitions. |
| 201 | * |
| 202 | * We have a bit per device to indicate it's blocked and a global wait queue |
| 203 | * for callers to sleep on until devices are unblocked. |
| 204 | */ |
| 205 | static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait); |
| 206 | |
| 207 | static noinline void pci_wait_cfg(struct pci_dev *dev) |
| 208 | __must_hold(&pci_lock) |
| 209 | { |
| 210 | do { |
| 211 | raw_spin_unlock_irq(&pci_lock); |
| 212 | wait_event(pci_cfg_wait, !dev->block_cfg_access); |
| 213 | raw_spin_lock_irq(&pci_lock); |
| 214 | } while (dev->block_cfg_access); |
| 215 | } |
| 216 | |
| 217 | /* Returns 0 on success, negative values indicate error. */ |
| 218 | #define PCI_USER_READ_CONFIG(size, type) \ |
| 219 | int pci_user_read_config_##size \ |
| 220 | (struct pci_dev *dev, int pos, type *val) \ |
| 221 | { \ |
| 222 | int ret = PCIBIOS_SUCCESSFUL; \ |
| 223 | u32 data = -1; \ |
| 224 | if (PCI_##size##_BAD) \ |
| 225 | return -EINVAL; \ |
| 226 | raw_spin_lock_irq(&pci_lock); \ |
| 227 | if (unlikely(dev->block_cfg_access)) \ |
| 228 | pci_wait_cfg(dev); \ |
| 229 | ret = dev->bus->ops->read(dev->bus, dev->devfn, \ |
| 230 | pos, sizeof(type), &data); \ |
| 231 | raw_spin_unlock_irq(&pci_lock); \ |
| 232 | *val = (type)data; \ |
| 233 | return pcibios_err_to_errno(ret); \ |
| 234 | } \ |
| 235 | EXPORT_SYMBOL_GPL(pci_user_read_config_##size); |
| 236 | |
| 237 | /* Returns 0 on success, negative values indicate error. */ |
| 238 | #define PCI_USER_WRITE_CONFIG(size, type) \ |
| 239 | int pci_user_write_config_##size \ |
| 240 | (struct pci_dev *dev, int pos, type val) \ |
| 241 | { \ |
| 242 | int ret = PCIBIOS_SUCCESSFUL; \ |
| 243 | if (PCI_##size##_BAD) \ |
| 244 | return -EINVAL; \ |
| 245 | raw_spin_lock_irq(&pci_lock); \ |
| 246 | if (unlikely(dev->block_cfg_access)) \ |
| 247 | pci_wait_cfg(dev); \ |
| 248 | ret = dev->bus->ops->write(dev->bus, dev->devfn, \ |
| 249 | pos, sizeof(type), val); \ |
| 250 | raw_spin_unlock_irq(&pci_lock); \ |
| 251 | return pcibios_err_to_errno(ret); \ |
| 252 | } \ |
| 253 | EXPORT_SYMBOL_GPL(pci_user_write_config_##size); |
| 254 | |
| 255 | PCI_USER_READ_CONFIG(byte, u8) |
| 256 | PCI_USER_READ_CONFIG(word, u16) |
| 257 | PCI_USER_READ_CONFIG(dword, u32) |
| 258 | PCI_USER_WRITE_CONFIG(byte, u8) |
| 259 | PCI_USER_WRITE_CONFIG(word, u16) |
| 260 | PCI_USER_WRITE_CONFIG(dword, u32) |
| 261 | |
| 262 | /* VPD access through PCI 2.2+ VPD capability */ |
| 263 | |
| 264 | /** |
| 265 | * pci_read_vpd - Read one entry from Vital Product Data |
| 266 | * @dev: pci device struct |
| 267 | * @pos: offset in vpd space |
| 268 | * @count: number of bytes to read |
| 269 | * @buf: pointer to where to store result |
| 270 | */ |
| 271 | ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf) |
| 272 | { |
| 273 | if (!dev->vpd || !dev->vpd->ops) |
| 274 | return -ENODEV; |
| 275 | return dev->vpd->ops->read(dev, pos, count, buf); |
| 276 | } |
| 277 | EXPORT_SYMBOL(pci_read_vpd); |
| 278 | |
| 279 | /** |
| 280 | * pci_write_vpd - Write entry to Vital Product Data |
| 281 | * @dev: pci device struct |
| 282 | * @pos: offset in vpd space |
| 283 | * @count: number of bytes to write |
| 284 | * @buf: buffer containing write data |
| 285 | */ |
| 286 | ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf) |
| 287 | { |
| 288 | if (!dev->vpd || !dev->vpd->ops) |
| 289 | return -ENODEV; |
| 290 | return dev->vpd->ops->write(dev, pos, count, buf); |
| 291 | } |
| 292 | EXPORT_SYMBOL(pci_write_vpd); |
| 293 | |
| 294 | /** |
| 295 | * pci_set_vpd_size - Set size of Vital Product Data space |
| 296 | * @dev: pci device struct |
| 297 | * @len: size of vpd space |
| 298 | */ |
| 299 | int pci_set_vpd_size(struct pci_dev *dev, size_t len) |
| 300 | { |
| 301 | if (!dev->vpd || !dev->vpd->ops) |
| 302 | return -ENODEV; |
| 303 | return dev->vpd->ops->set_size(dev, len); |
| 304 | } |
| 305 | EXPORT_SYMBOL(pci_set_vpd_size); |
| 306 | |
| 307 | #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1) |
| 308 | |
| 309 | /** |
| 310 | * pci_vpd_size - determine actual size of Vital Product Data |
| 311 | * @dev: pci device struct |
| 312 | * @old_size: current assumed size, also maximum allowed size |
| 313 | */ |
| 314 | static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size) |
| 315 | { |
| 316 | size_t off = 0; |
| 317 | unsigned char header[1+2]; /* 1 byte tag, 2 bytes length */ |
| 318 | |
| 319 | while (off < old_size && |
| 320 | pci_read_vpd(dev, off, 1, header) == 1) { |
| 321 | unsigned char tag; |
| 322 | |
| 323 | if (header[0] & PCI_VPD_LRDT) { |
| 324 | /* Large Resource Data Type Tag */ |
| 325 | tag = pci_vpd_lrdt_tag(header); |
| 326 | /* Only read length from known tag items */ |
| 327 | if ((tag == PCI_VPD_LTIN_ID_STRING) || |
| 328 | (tag == PCI_VPD_LTIN_RO_DATA) || |
| 329 | (tag == PCI_VPD_LTIN_RW_DATA)) { |
| 330 | if (pci_read_vpd(dev, off+1, 2, |
| 331 | &header[1]) != 2) { |
| 332 | dev_warn(&dev->dev, |
| 333 | "invalid large VPD tag %02x size at offset %zu", |
| 334 | tag, off + 1); |
| 335 | return 0; |
| 336 | } |
| 337 | off += PCI_VPD_LRDT_TAG_SIZE + |
| 338 | pci_vpd_lrdt_size(header); |
| 339 | } |
| 340 | } else { |
| 341 | /* Short Resource Data Type Tag */ |
| 342 | off += PCI_VPD_SRDT_TAG_SIZE + |
| 343 | pci_vpd_srdt_size(header); |
| 344 | tag = pci_vpd_srdt_tag(header); |
| 345 | } |
| 346 | |
| 347 | if (tag == PCI_VPD_STIN_END) /* End tag descriptor */ |
| 348 | return off; |
| 349 | |
| 350 | if ((tag != PCI_VPD_LTIN_ID_STRING) && |
| 351 | (tag != PCI_VPD_LTIN_RO_DATA) && |
| 352 | (tag != PCI_VPD_LTIN_RW_DATA)) { |
| 353 | dev_warn(&dev->dev, |
| 354 | "invalid %s VPD tag %02x at offset %zu", |
| 355 | (header[0] & PCI_VPD_LRDT) ? "large" : "short", |
| 356 | tag, off); |
| 357 | return 0; |
| 358 | } |
| 359 | } |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Wait for last operation to complete. |
| 365 | * This code has to spin since there is no other notification from the PCI |
| 366 | * hardware. Since the VPD is often implemented by serial attachment to an |
| 367 | * EEPROM, it may take many milliseconds to complete. |
| 368 | * |
| 369 | * Returns 0 on success, negative values indicate error. |
| 370 | */ |
| 371 | static int pci_vpd_wait(struct pci_dev *dev) |
| 372 | { |
| 373 | struct pci_vpd *vpd = dev->vpd; |
| 374 | unsigned long timeout = jiffies + msecs_to_jiffies(125); |
| 375 | unsigned long max_sleep = 16; |
| 376 | u16 status; |
| 377 | int ret; |
| 378 | |
| 379 | if (!vpd->busy) |
| 380 | return 0; |
| 381 | |
| 382 | while (time_before(jiffies, timeout)) { |
| 383 | ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR, |
| 384 | &status); |
| 385 | if (ret < 0) |
| 386 | return ret; |
| 387 | |
| 388 | if ((status & PCI_VPD_ADDR_F) == vpd->flag) { |
| 389 | vpd->busy = 0; |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | if (fatal_signal_pending(current)) |
| 394 | return -EINTR; |
| 395 | |
| 396 | usleep_range(10, max_sleep); |
| 397 | if (max_sleep < 1024) |
| 398 | max_sleep *= 2; |
| 399 | } |
| 400 | |
| 401 | dev_warn(&dev->dev, "VPD access failed. This is likely a firmware bug on this device. Contact the card vendor for a firmware update\n"); |
| 402 | return -ETIMEDOUT; |
| 403 | } |
| 404 | |
| 405 | static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count, |
| 406 | void *arg) |
| 407 | { |
| 408 | struct pci_vpd *vpd = dev->vpd; |
| 409 | int ret; |
| 410 | loff_t end = pos + count; |
| 411 | u8 *buf = arg; |
| 412 | |
| 413 | if (pos < 0) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | if (!vpd->valid) { |
| 417 | vpd->valid = 1; |
| 418 | vpd->len = pci_vpd_size(dev, vpd->len); |
| 419 | } |
| 420 | |
| 421 | if (vpd->len == 0) |
| 422 | return -EIO; |
| 423 | |
| 424 | if (pos > vpd->len) |
| 425 | return 0; |
| 426 | |
| 427 | if (end > vpd->len) { |
| 428 | end = vpd->len; |
| 429 | count = end - pos; |
| 430 | } |
| 431 | |
| 432 | if (mutex_lock_killable(&vpd->lock)) |
| 433 | return -EINTR; |
| 434 | |
| 435 | ret = pci_vpd_wait(dev); |
| 436 | if (ret < 0) |
| 437 | goto out; |
| 438 | |
| 439 | while (pos < end) { |
| 440 | u32 val; |
| 441 | unsigned int i, skip; |
| 442 | |
| 443 | ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR, |
| 444 | pos & ~3); |
| 445 | if (ret < 0) |
| 446 | break; |
| 447 | vpd->busy = 1; |
| 448 | vpd->flag = PCI_VPD_ADDR_F; |
| 449 | ret = pci_vpd_wait(dev); |
| 450 | if (ret < 0) |
| 451 | break; |
| 452 | |
| 453 | ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val); |
| 454 | if (ret < 0) |
| 455 | break; |
| 456 | |
| 457 | skip = pos & 3; |
| 458 | for (i = 0; i < sizeof(u32); i++) { |
| 459 | if (i >= skip) { |
| 460 | *buf++ = val; |
| 461 | if (++pos == end) |
| 462 | break; |
| 463 | } |
| 464 | val >>= 8; |
| 465 | } |
| 466 | } |
| 467 | out: |
| 468 | mutex_unlock(&vpd->lock); |
| 469 | return ret ? ret : count; |
| 470 | } |
| 471 | |
| 472 | static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count, |
| 473 | const void *arg) |
| 474 | { |
| 475 | struct pci_vpd *vpd = dev->vpd; |
| 476 | const u8 *buf = arg; |
| 477 | loff_t end = pos + count; |
| 478 | int ret = 0; |
| 479 | |
| 480 | if (pos < 0 || (pos & 3) || (count & 3)) |
| 481 | return -EINVAL; |
| 482 | |
| 483 | if (!vpd->valid) { |
| 484 | vpd->valid = 1; |
| 485 | vpd->len = pci_vpd_size(dev, vpd->len); |
| 486 | } |
| 487 | |
| 488 | if (vpd->len == 0) |
| 489 | return -EIO; |
| 490 | |
| 491 | if (end > vpd->len) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | if (mutex_lock_killable(&vpd->lock)) |
| 495 | return -EINTR; |
| 496 | |
| 497 | ret = pci_vpd_wait(dev); |
| 498 | if (ret < 0) |
| 499 | goto out; |
| 500 | |
| 501 | while (pos < end) { |
| 502 | u32 val; |
| 503 | |
| 504 | val = *buf++; |
| 505 | val |= *buf++ << 8; |
| 506 | val |= *buf++ << 16; |
| 507 | val |= *buf++ << 24; |
| 508 | |
| 509 | ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val); |
| 510 | if (ret < 0) |
| 511 | break; |
| 512 | ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR, |
| 513 | pos | PCI_VPD_ADDR_F); |
| 514 | if (ret < 0) |
| 515 | break; |
| 516 | |
| 517 | vpd->busy = 1; |
| 518 | vpd->flag = 0; |
| 519 | ret = pci_vpd_wait(dev); |
| 520 | if (ret < 0) |
| 521 | break; |
| 522 | |
| 523 | pos += sizeof(u32); |
| 524 | } |
| 525 | out: |
| 526 | mutex_unlock(&vpd->lock); |
| 527 | return ret ? ret : count; |
| 528 | } |
| 529 | |
| 530 | static int pci_vpd_set_size(struct pci_dev *dev, size_t len) |
| 531 | { |
| 532 | struct pci_vpd *vpd = dev->vpd; |
| 533 | |
| 534 | if (len == 0 || len > PCI_VPD_MAX_SIZE) |
| 535 | return -EIO; |
| 536 | |
| 537 | vpd->valid = 1; |
| 538 | vpd->len = len; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static const struct pci_vpd_ops pci_vpd_ops = { |
| 544 | .read = pci_vpd_read, |
| 545 | .write = pci_vpd_write, |
| 546 | .set_size = pci_vpd_set_size, |
| 547 | }; |
| 548 | |
| 549 | static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count, |
| 550 | void *arg) |
| 551 | { |
| 552 | struct pci_dev *tdev = pci_get_slot(dev->bus, |
| 553 | PCI_DEVFN(PCI_SLOT(dev->devfn), 0)); |
| 554 | ssize_t ret; |
| 555 | |
| 556 | if (!tdev) |
| 557 | return -ENODEV; |
| 558 | |
| 559 | ret = pci_read_vpd(tdev, pos, count, arg); |
| 560 | pci_dev_put(tdev); |
| 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count, |
| 565 | const void *arg) |
| 566 | { |
| 567 | struct pci_dev *tdev = pci_get_slot(dev->bus, |
| 568 | PCI_DEVFN(PCI_SLOT(dev->devfn), 0)); |
| 569 | ssize_t ret; |
| 570 | |
| 571 | if (!tdev) |
| 572 | return -ENODEV; |
| 573 | |
| 574 | ret = pci_write_vpd(tdev, pos, count, arg); |
| 575 | pci_dev_put(tdev); |
| 576 | return ret; |
| 577 | } |
| 578 | |
| 579 | static int pci_vpd_f0_set_size(struct pci_dev *dev, size_t len) |
| 580 | { |
| 581 | struct pci_dev *tdev = pci_get_slot(dev->bus, |
| 582 | PCI_DEVFN(PCI_SLOT(dev->devfn), 0)); |
| 583 | int ret; |
| 584 | |
| 585 | if (!tdev) |
| 586 | return -ENODEV; |
| 587 | |
| 588 | ret = pci_set_vpd_size(tdev, len); |
| 589 | pci_dev_put(tdev); |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | static const struct pci_vpd_ops pci_vpd_f0_ops = { |
| 594 | .read = pci_vpd_f0_read, |
| 595 | .write = pci_vpd_f0_write, |
| 596 | .set_size = pci_vpd_f0_set_size, |
| 597 | }; |
| 598 | |
| 599 | int pci_vpd_init(struct pci_dev *dev) |
| 600 | { |
| 601 | struct pci_vpd *vpd; |
| 602 | u8 cap; |
| 603 | |
| 604 | cap = pci_find_capability(dev, PCI_CAP_ID_VPD); |
| 605 | if (!cap) |
| 606 | return -ENODEV; |
| 607 | |
| 608 | vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC); |
| 609 | if (!vpd) |
| 610 | return -ENOMEM; |
| 611 | |
| 612 | vpd->len = PCI_VPD_MAX_SIZE; |
| 613 | if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) |
| 614 | vpd->ops = &pci_vpd_f0_ops; |
| 615 | else |
| 616 | vpd->ops = &pci_vpd_ops; |
| 617 | mutex_init(&vpd->lock); |
| 618 | vpd->cap = cap; |
| 619 | vpd->busy = 0; |
| 620 | vpd->valid = 0; |
| 621 | dev->vpd = vpd; |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | void pci_vpd_release(struct pci_dev *dev) |
| 626 | { |
| 627 | kfree(dev->vpd); |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * pci_cfg_access_lock - Lock PCI config reads/writes |
| 632 | * @dev: pci device struct |
| 633 | * |
| 634 | * When access is locked, any userspace reads or writes to config |
| 635 | * space and concurrent lock requests will sleep until access is |
| 636 | * allowed via pci_cfg_access_unlock() again. |
| 637 | */ |
| 638 | void pci_cfg_access_lock(struct pci_dev *dev) |
| 639 | { |
| 640 | might_sleep(); |
| 641 | |
| 642 | raw_spin_lock_irq(&pci_lock); |
| 643 | if (dev->block_cfg_access) |
| 644 | pci_wait_cfg(dev); |
| 645 | dev->block_cfg_access = 1; |
| 646 | raw_spin_unlock_irq(&pci_lock); |
| 647 | } |
| 648 | EXPORT_SYMBOL_GPL(pci_cfg_access_lock); |
| 649 | |
| 650 | /** |
| 651 | * pci_cfg_access_trylock - try to lock PCI config reads/writes |
| 652 | * @dev: pci device struct |
| 653 | * |
| 654 | * Same as pci_cfg_access_lock, but will return 0 if access is |
| 655 | * already locked, 1 otherwise. This function can be used from |
| 656 | * atomic contexts. |
| 657 | */ |
| 658 | bool pci_cfg_access_trylock(struct pci_dev *dev) |
| 659 | { |
| 660 | unsigned long flags; |
| 661 | bool locked = true; |
| 662 | |
| 663 | raw_spin_lock_irqsave(&pci_lock, flags); |
| 664 | if (dev->block_cfg_access) |
| 665 | locked = false; |
| 666 | else |
| 667 | dev->block_cfg_access = 1; |
| 668 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
| 669 | |
| 670 | return locked; |
| 671 | } |
| 672 | EXPORT_SYMBOL_GPL(pci_cfg_access_trylock); |
| 673 | |
| 674 | /** |
| 675 | * pci_cfg_access_unlock - Unlock PCI config reads/writes |
| 676 | * @dev: pci device struct |
| 677 | * |
| 678 | * This function allows PCI config accesses to resume. |
| 679 | */ |
| 680 | void pci_cfg_access_unlock(struct pci_dev *dev) |
| 681 | { |
| 682 | unsigned long flags; |
| 683 | |
| 684 | raw_spin_lock_irqsave(&pci_lock, flags); |
| 685 | |
| 686 | /* This indicates a problem in the caller, but we don't need |
| 687 | * to kill them, unlike a double-block above. */ |
| 688 | WARN_ON(!dev->block_cfg_access); |
| 689 | |
| 690 | dev->block_cfg_access = 0; |
| 691 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
| 692 | |
| 693 | wake_up_all(&pci_cfg_wait); |
| 694 | } |
| 695 | EXPORT_SYMBOL_GPL(pci_cfg_access_unlock); |
| 696 | |
| 697 | static inline int pcie_cap_version(const struct pci_dev *dev) |
| 698 | { |
| 699 | return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS; |
| 700 | } |
| 701 | |
| 702 | static bool pcie_downstream_port(const struct pci_dev *dev) |
| 703 | { |
| 704 | int type = pci_pcie_type(dev); |
| 705 | |
| 706 | return type == PCI_EXP_TYPE_ROOT_PORT || |
| 707 | type == PCI_EXP_TYPE_DOWNSTREAM || |
| 708 | type == PCI_EXP_TYPE_PCIE_BRIDGE; |
| 709 | } |
| 710 | |
| 711 | bool pcie_cap_has_lnkctl(const struct pci_dev *dev) |
| 712 | { |
| 713 | int type = pci_pcie_type(dev); |
| 714 | |
| 715 | return type == PCI_EXP_TYPE_ENDPOINT || |
| 716 | type == PCI_EXP_TYPE_LEG_END || |
| 717 | type == PCI_EXP_TYPE_ROOT_PORT || |
| 718 | type == PCI_EXP_TYPE_UPSTREAM || |
| 719 | type == PCI_EXP_TYPE_DOWNSTREAM || |
| 720 | type == PCI_EXP_TYPE_PCI_BRIDGE || |
| 721 | type == PCI_EXP_TYPE_PCIE_BRIDGE; |
| 722 | } |
| 723 | |
| 724 | static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev) |
| 725 | { |
| 726 | return pcie_downstream_port(dev) && |
| 727 | pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT; |
| 728 | } |
| 729 | |
| 730 | static inline bool pcie_cap_has_rtctl(const struct pci_dev *dev) |
| 731 | { |
| 732 | int type = pci_pcie_type(dev); |
| 733 | |
| 734 | return type == PCI_EXP_TYPE_ROOT_PORT || |
| 735 | type == PCI_EXP_TYPE_RC_EC; |
| 736 | } |
| 737 | |
| 738 | static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos) |
| 739 | { |
| 740 | if (!pci_is_pcie(dev)) |
| 741 | return false; |
| 742 | |
| 743 | switch (pos) { |
| 744 | case PCI_EXP_FLAGS: |
| 745 | return true; |
| 746 | case PCI_EXP_DEVCAP: |
| 747 | case PCI_EXP_DEVCTL: |
| 748 | case PCI_EXP_DEVSTA: |
| 749 | return true; |
| 750 | case PCI_EXP_LNKCAP: |
| 751 | case PCI_EXP_LNKCTL: |
| 752 | case PCI_EXP_LNKSTA: |
| 753 | return pcie_cap_has_lnkctl(dev); |
| 754 | case PCI_EXP_SLTCAP: |
| 755 | case PCI_EXP_SLTCTL: |
| 756 | case PCI_EXP_SLTSTA: |
| 757 | return pcie_cap_has_sltctl(dev); |
| 758 | case PCI_EXP_RTCTL: |
| 759 | case PCI_EXP_RTCAP: |
| 760 | case PCI_EXP_RTSTA: |
| 761 | return pcie_cap_has_rtctl(dev); |
| 762 | case PCI_EXP_DEVCAP2: |
| 763 | case PCI_EXP_DEVCTL2: |
| 764 | case PCI_EXP_LNKCAP2: |
| 765 | case PCI_EXP_LNKCTL2: |
| 766 | case PCI_EXP_LNKSTA2: |
| 767 | return pcie_cap_version(dev) > 1; |
| 768 | default: |
| 769 | return false; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /* |
| 774 | * Note that these accessor functions are only for the "PCI Express |
| 775 | * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the |
| 776 | * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.) |
| 777 | */ |
| 778 | int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val) |
| 779 | { |
| 780 | int ret; |
| 781 | |
| 782 | *val = 0; |
| 783 | if (pos & 1) |
| 784 | return -EINVAL; |
| 785 | |
| 786 | if (pcie_capability_reg_implemented(dev, pos)) { |
| 787 | ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val); |
| 788 | /* |
| 789 | * Reset *val to 0 if pci_read_config_word() fails, it may |
| 790 | * have been written as 0xFFFF if hardware error happens |
| 791 | * during pci_read_config_word(). |
| 792 | */ |
| 793 | if (ret) |
| 794 | *val = 0; |
| 795 | return ret; |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * For Functions that do not implement the Slot Capabilities, |
| 800 | * Slot Status, and Slot Control registers, these spaces must |
| 801 | * be hardwired to 0b, with the exception of the Presence Detect |
| 802 | * State bit in the Slot Status register of Downstream Ports, |
| 803 | * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8) |
| 804 | */ |
| 805 | if (pci_is_pcie(dev) && pcie_downstream_port(dev) && |
| 806 | pos == PCI_EXP_SLTSTA) |
| 807 | *val = PCI_EXP_SLTSTA_PDS; |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | EXPORT_SYMBOL(pcie_capability_read_word); |
| 812 | |
| 813 | int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val) |
| 814 | { |
| 815 | int ret; |
| 816 | |
| 817 | *val = 0; |
| 818 | if (pos & 3) |
| 819 | return -EINVAL; |
| 820 | |
| 821 | if (pcie_capability_reg_implemented(dev, pos)) { |
| 822 | ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val); |
| 823 | /* |
| 824 | * Reset *val to 0 if pci_read_config_dword() fails, it may |
| 825 | * have been written as 0xFFFFFFFF if hardware error happens |
| 826 | * during pci_read_config_dword(). |
| 827 | */ |
| 828 | if (ret) |
| 829 | *val = 0; |
| 830 | return ret; |
| 831 | } |
| 832 | |
| 833 | if (pci_is_pcie(dev) && pcie_downstream_port(dev) && |
| 834 | pos == PCI_EXP_SLTSTA) |
| 835 | *val = PCI_EXP_SLTSTA_PDS; |
| 836 | |
| 837 | return 0; |
| 838 | } |
| 839 | EXPORT_SYMBOL(pcie_capability_read_dword); |
| 840 | |
| 841 | int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val) |
| 842 | { |
| 843 | if (pos & 1) |
| 844 | return -EINVAL; |
| 845 | |
| 846 | if (!pcie_capability_reg_implemented(dev, pos)) |
| 847 | return 0; |
| 848 | |
| 849 | return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val); |
| 850 | } |
| 851 | EXPORT_SYMBOL(pcie_capability_write_word); |
| 852 | |
| 853 | int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val) |
| 854 | { |
| 855 | if (pos & 3) |
| 856 | return -EINVAL; |
| 857 | |
| 858 | if (!pcie_capability_reg_implemented(dev, pos)) |
| 859 | return 0; |
| 860 | |
| 861 | return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val); |
| 862 | } |
| 863 | EXPORT_SYMBOL(pcie_capability_write_dword); |
| 864 | |
| 865 | int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos, |
| 866 | u16 clear, u16 set) |
| 867 | { |
| 868 | int ret; |
| 869 | u16 val; |
| 870 | |
| 871 | ret = pcie_capability_read_word(dev, pos, &val); |
| 872 | if (!ret) { |
| 873 | val &= ~clear; |
| 874 | val |= set; |
| 875 | ret = pcie_capability_write_word(dev, pos, val); |
| 876 | } |
| 877 | |
| 878 | return ret; |
| 879 | } |
| 880 | EXPORT_SYMBOL(pcie_capability_clear_and_set_word); |
| 881 | |
| 882 | int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos, |
| 883 | u32 clear, u32 set) |
| 884 | { |
| 885 | int ret; |
| 886 | u32 val; |
| 887 | |
| 888 | ret = pcie_capability_read_dword(dev, pos, &val); |
| 889 | if (!ret) { |
| 890 | val &= ~clear; |
| 891 | val |= set; |
| 892 | ret = pcie_capability_write_dword(dev, pos, val); |
| 893 | } |
| 894 | |
| 895 | return ret; |
| 896 | } |
| 897 | EXPORT_SYMBOL(pcie_capability_clear_and_set_dword); |
| 898 | |
| 899 | int pci_read_config_byte(const struct pci_dev *dev, int where, u8 *val) |
| 900 | { |
| 901 | if (pci_dev_is_disconnected(dev)) { |
| 902 | *val = ~0; |
| 903 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 904 | } |
| 905 | return pci_bus_read_config_byte(dev->bus, dev->devfn, where, val); |
| 906 | } |
| 907 | EXPORT_SYMBOL(pci_read_config_byte); |
| 908 | |
| 909 | int pci_read_config_word(const struct pci_dev *dev, int where, u16 *val) |
| 910 | { |
| 911 | if (pci_dev_is_disconnected(dev)) { |
| 912 | *val = ~0; |
| 913 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 914 | } |
| 915 | return pci_bus_read_config_word(dev->bus, dev->devfn, where, val); |
| 916 | } |
| 917 | EXPORT_SYMBOL(pci_read_config_word); |
| 918 | |
| 919 | int pci_read_config_dword(const struct pci_dev *dev, int where, |
| 920 | u32 *val) |
| 921 | { |
| 922 | if (pci_dev_is_disconnected(dev)) { |
| 923 | *val = ~0; |
| 924 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 925 | } |
| 926 | return pci_bus_read_config_dword(dev->bus, dev->devfn, where, val); |
| 927 | } |
| 928 | EXPORT_SYMBOL(pci_read_config_dword); |
| 929 | |
| 930 | int pci_write_config_byte(const struct pci_dev *dev, int where, u8 val) |
| 931 | { |
| 932 | if (pci_dev_is_disconnected(dev)) |
| 933 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 934 | return pci_bus_write_config_byte(dev->bus, dev->devfn, where, val); |
| 935 | } |
| 936 | EXPORT_SYMBOL(pci_write_config_byte); |
| 937 | |
| 938 | int pci_write_config_word(const struct pci_dev *dev, int where, u16 val) |
| 939 | { |
| 940 | if (pci_dev_is_disconnected(dev)) |
| 941 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 942 | return pci_bus_write_config_word(dev->bus, dev->devfn, where, val); |
| 943 | } |
| 944 | EXPORT_SYMBOL(pci_write_config_word); |
| 945 | |
| 946 | int pci_write_config_dword(const struct pci_dev *dev, int where, |
| 947 | u32 val) |
| 948 | { |
| 949 | if (pci_dev_is_disconnected(dev)) |
| 950 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 951 | return pci_bus_write_config_dword(dev->bus, dev->devfn, where, val); |
| 952 | } |
| 953 | EXPORT_SYMBOL(pci_write_config_dword); |