watchdog/core: Rename some softlockup_* functions
[linux/fpc-iii.git] / drivers / pci / access.c
blob913d6722ece988463b9850b7e7add11634766091
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>
9 #include "pci.h"
12 * This interrupt-safe spinlock protects all accesses to PCI
13 * configuration space.
16 DEFINE_RAW_SPINLOCK(pci_lock);
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.
24 #define PCI_byte_BAD 0
25 #define PCI_word_BAD (pos & 1)
26 #define PCI_dword_BAD (pos & 3)
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
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; \
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; \
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)
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);
78 int pci_generic_config_read(struct pci_bus *bus, unsigned int devfn,
79 int where, int size, u32 *val)
81 void __iomem *addr;
83 addr = bus->ops->map_bus(bus, devfn, where);
84 if (!addr) {
85 *val = ~0;
86 return PCIBIOS_DEVICE_NOT_FOUND;
89 if (size == 1)
90 *val = readb(addr);
91 else if (size == 2)
92 *val = readw(addr);
93 else
94 *val = readl(addr);
96 return PCIBIOS_SUCCESSFUL;
98 EXPORT_SYMBOL_GPL(pci_generic_config_read);
100 int pci_generic_config_write(struct pci_bus *bus, unsigned int devfn,
101 int where, int size, u32 val)
103 void __iomem *addr;
105 addr = bus->ops->map_bus(bus, devfn, where);
106 if (!addr)
107 return PCIBIOS_DEVICE_NOT_FOUND;
109 if (size == 1)
110 writeb(val, addr);
111 else if (size == 2)
112 writew(val, addr);
113 else
114 writel(val, addr);
116 return PCIBIOS_SUCCESSFUL;
118 EXPORT_SYMBOL_GPL(pci_generic_config_write);
120 int pci_generic_config_read32(struct pci_bus *bus, unsigned int devfn,
121 int where, int size, u32 *val)
123 void __iomem *addr;
125 addr = bus->ops->map_bus(bus, devfn, where & ~0x3);
126 if (!addr) {
127 *val = ~0;
128 return PCIBIOS_DEVICE_NOT_FOUND;
131 *val = readl(addr);
133 if (size <= 2)
134 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
136 return PCIBIOS_SUCCESSFUL;
138 EXPORT_SYMBOL_GPL(pci_generic_config_read32);
140 int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn,
141 int where, int size, u32 val)
143 void __iomem *addr;
144 u32 mask, tmp;
146 addr = bus->ops->map_bus(bus, devfn, where & ~0x3);
147 if (!addr)
148 return PCIBIOS_DEVICE_NOT_FOUND;
150 if (size == 4) {
151 writel(val, addr);
152 return PCIBIOS_SUCCESSFUL;
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.
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);
168 mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
169 tmp = readl(addr) & mask;
170 tmp |= val << ((where & 0x3) * 8);
171 writel(tmp, addr);
173 return PCIBIOS_SUCCESSFUL;
175 EXPORT_SYMBOL_GPL(pci_generic_config_write32);
178 * pci_bus_set_ops - Set raw operations of pci bus
179 * @bus: pci bus struct
180 * @ops: new raw operations
182 * Return previous raw operations
184 struct pci_ops *pci_bus_set_ops(struct pci_bus *bus, struct pci_ops *ops)
186 struct pci_ops *old_ops;
187 unsigned long flags;
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;
195 EXPORT_SYMBOL(pci_bus_set_ops);
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.
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.
205 static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait);
207 static noinline void pci_wait_cfg(struct pci_dev *dev)
209 DECLARE_WAITQUEUE(wait, current);
211 __add_wait_queue(&pci_cfg_wait, &wait);
212 do {
213 set_current_state(TASK_UNINTERRUPTIBLE);
214 raw_spin_unlock_irq(&pci_lock);
215 schedule();
216 raw_spin_lock_irq(&pci_lock);
217 } while (dev->block_cfg_access);
218 __remove_wait_queue(&pci_cfg_wait, &wait);
221 /* Returns 0 on success, negative values indicate error. */
222 #define PCI_USER_READ_CONFIG(size, type) \
223 int pci_user_read_config_##size \
224 (struct pci_dev *dev, int pos, type *val) \
226 int ret = PCIBIOS_SUCCESSFUL; \
227 u32 data = -1; \
228 if (PCI_##size##_BAD) \
229 return -EINVAL; \
230 raw_spin_lock_irq(&pci_lock); \
231 if (unlikely(dev->block_cfg_access)) \
232 pci_wait_cfg(dev); \
233 ret = dev->bus->ops->read(dev->bus, dev->devfn, \
234 pos, sizeof(type), &data); \
235 raw_spin_unlock_irq(&pci_lock); \
236 *val = (type)data; \
237 return pcibios_err_to_errno(ret); \
239 EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
241 /* Returns 0 on success, negative values indicate error. */
242 #define PCI_USER_WRITE_CONFIG(size, type) \
243 int pci_user_write_config_##size \
244 (struct pci_dev *dev, int pos, type val) \
246 int ret = PCIBIOS_SUCCESSFUL; \
247 if (PCI_##size##_BAD) \
248 return -EINVAL; \
249 raw_spin_lock_irq(&pci_lock); \
250 if (unlikely(dev->block_cfg_access)) \
251 pci_wait_cfg(dev); \
252 ret = dev->bus->ops->write(dev->bus, dev->devfn, \
253 pos, sizeof(type), val); \
254 raw_spin_unlock_irq(&pci_lock); \
255 return pcibios_err_to_errno(ret); \
257 EXPORT_SYMBOL_GPL(pci_user_write_config_##size);
259 PCI_USER_READ_CONFIG(byte, u8)
260 PCI_USER_READ_CONFIG(word, u16)
261 PCI_USER_READ_CONFIG(dword, u32)
262 PCI_USER_WRITE_CONFIG(byte, u8)
263 PCI_USER_WRITE_CONFIG(word, u16)
264 PCI_USER_WRITE_CONFIG(dword, u32)
266 /* VPD access through PCI 2.2+ VPD capability */
269 * pci_read_vpd - Read one entry from Vital Product Data
270 * @dev: pci device struct
271 * @pos: offset in vpd space
272 * @count: number of bytes to read
273 * @buf: pointer to where to store result
275 ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
277 if (!dev->vpd || !dev->vpd->ops)
278 return -ENODEV;
279 return dev->vpd->ops->read(dev, pos, count, buf);
281 EXPORT_SYMBOL(pci_read_vpd);
284 * pci_write_vpd - Write entry to Vital Product Data
285 * @dev: pci device struct
286 * @pos: offset in vpd space
287 * @count: number of bytes to write
288 * @buf: buffer containing write data
290 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
292 if (!dev->vpd || !dev->vpd->ops)
293 return -ENODEV;
294 return dev->vpd->ops->write(dev, pos, count, buf);
296 EXPORT_SYMBOL(pci_write_vpd);
299 * pci_set_vpd_size - Set size of Vital Product Data space
300 * @dev: pci device struct
301 * @len: size of vpd space
303 int pci_set_vpd_size(struct pci_dev *dev, size_t len)
305 if (!dev->vpd || !dev->vpd->ops)
306 return -ENODEV;
307 return dev->vpd->ops->set_size(dev, len);
309 EXPORT_SYMBOL(pci_set_vpd_size);
311 #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
314 * pci_vpd_size - determine actual size of Vital Product Data
315 * @dev: pci device struct
316 * @old_size: current assumed size, also maximum allowed size
318 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
320 size_t off = 0;
321 unsigned char header[1+2]; /* 1 byte tag, 2 bytes length */
323 while (off < old_size &&
324 pci_read_vpd(dev, off, 1, header) == 1) {
325 unsigned char tag;
327 if (header[0] & PCI_VPD_LRDT) {
328 /* Large Resource Data Type Tag */
329 tag = pci_vpd_lrdt_tag(header);
330 /* Only read length from known tag items */
331 if ((tag == PCI_VPD_LTIN_ID_STRING) ||
332 (tag == PCI_VPD_LTIN_RO_DATA) ||
333 (tag == PCI_VPD_LTIN_RW_DATA)) {
334 if (pci_read_vpd(dev, off+1, 2,
335 &header[1]) != 2) {
336 dev_warn(&dev->dev,
337 "invalid large VPD tag %02x size at offset %zu",
338 tag, off + 1);
339 return 0;
341 off += PCI_VPD_LRDT_TAG_SIZE +
342 pci_vpd_lrdt_size(header);
344 } else {
345 /* Short Resource Data Type Tag */
346 off += PCI_VPD_SRDT_TAG_SIZE +
347 pci_vpd_srdt_size(header);
348 tag = pci_vpd_srdt_tag(header);
351 if (tag == PCI_VPD_STIN_END) /* End tag descriptor */
352 return off;
354 if ((tag != PCI_VPD_LTIN_ID_STRING) &&
355 (tag != PCI_VPD_LTIN_RO_DATA) &&
356 (tag != PCI_VPD_LTIN_RW_DATA)) {
357 dev_warn(&dev->dev,
358 "invalid %s VPD tag %02x at offset %zu",
359 (header[0] & PCI_VPD_LRDT) ? "large" : "short",
360 tag, off);
361 return 0;
364 return 0;
368 * Wait for last operation to complete.
369 * This code has to spin since there is no other notification from the PCI
370 * hardware. Since the VPD is often implemented by serial attachment to an
371 * EEPROM, it may take many milliseconds to complete.
373 * Returns 0 on success, negative values indicate error.
375 static int pci_vpd_wait(struct pci_dev *dev)
377 struct pci_vpd *vpd = dev->vpd;
378 unsigned long timeout = jiffies + msecs_to_jiffies(125);
379 unsigned long max_sleep = 16;
380 u16 status;
381 int ret;
383 if (!vpd->busy)
384 return 0;
386 while (time_before(jiffies, timeout)) {
387 ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
388 &status);
389 if (ret < 0)
390 return ret;
392 if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
393 vpd->busy = 0;
394 return 0;
397 if (fatal_signal_pending(current))
398 return -EINTR;
400 usleep_range(10, max_sleep);
401 if (max_sleep < 1024)
402 max_sleep *= 2;
405 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");
406 return -ETIMEDOUT;
409 static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
410 void *arg)
412 struct pci_vpd *vpd = dev->vpd;
413 int ret;
414 loff_t end = pos + count;
415 u8 *buf = arg;
417 if (pos < 0)
418 return -EINVAL;
420 if (!vpd->valid) {
421 vpd->valid = 1;
422 vpd->len = pci_vpd_size(dev, vpd->len);
425 if (vpd->len == 0)
426 return -EIO;
428 if (pos > vpd->len)
429 return 0;
431 if (end > vpd->len) {
432 end = vpd->len;
433 count = end - pos;
436 if (mutex_lock_killable(&vpd->lock))
437 return -EINTR;
439 ret = pci_vpd_wait(dev);
440 if (ret < 0)
441 goto out;
443 while (pos < end) {
444 u32 val;
445 unsigned int i, skip;
447 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
448 pos & ~3);
449 if (ret < 0)
450 break;
451 vpd->busy = 1;
452 vpd->flag = PCI_VPD_ADDR_F;
453 ret = pci_vpd_wait(dev);
454 if (ret < 0)
455 break;
457 ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
458 if (ret < 0)
459 break;
461 skip = pos & 3;
462 for (i = 0; i < sizeof(u32); i++) {
463 if (i >= skip) {
464 *buf++ = val;
465 if (++pos == end)
466 break;
468 val >>= 8;
471 out:
472 mutex_unlock(&vpd->lock);
473 return ret ? ret : count;
476 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
477 const void *arg)
479 struct pci_vpd *vpd = dev->vpd;
480 const u8 *buf = arg;
481 loff_t end = pos + count;
482 int ret = 0;
484 if (pos < 0 || (pos & 3) || (count & 3))
485 return -EINVAL;
487 if (!vpd->valid) {
488 vpd->valid = 1;
489 vpd->len = pci_vpd_size(dev, vpd->len);
492 if (vpd->len == 0)
493 return -EIO;
495 if (end > vpd->len)
496 return -EINVAL;
498 if (mutex_lock_killable(&vpd->lock))
499 return -EINTR;
501 ret = pci_vpd_wait(dev);
502 if (ret < 0)
503 goto out;
505 while (pos < end) {
506 u32 val;
508 val = *buf++;
509 val |= *buf++ << 8;
510 val |= *buf++ << 16;
511 val |= *buf++ << 24;
513 ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
514 if (ret < 0)
515 break;
516 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
517 pos | PCI_VPD_ADDR_F);
518 if (ret < 0)
519 break;
521 vpd->busy = 1;
522 vpd->flag = 0;
523 ret = pci_vpd_wait(dev);
524 if (ret < 0)
525 break;
527 pos += sizeof(u32);
529 out:
530 mutex_unlock(&vpd->lock);
531 return ret ? ret : count;
534 static int pci_vpd_set_size(struct pci_dev *dev, size_t len)
536 struct pci_vpd *vpd = dev->vpd;
538 if (len == 0 || len > PCI_VPD_MAX_SIZE)
539 return -EIO;
541 vpd->valid = 1;
542 vpd->len = len;
544 return 0;
547 static const struct pci_vpd_ops pci_vpd_ops = {
548 .read = pci_vpd_read,
549 .write = pci_vpd_write,
550 .set_size = pci_vpd_set_size,
553 static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
554 void *arg)
556 struct pci_dev *tdev = pci_get_slot(dev->bus,
557 PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
558 ssize_t ret;
560 if (!tdev)
561 return -ENODEV;
563 ret = pci_read_vpd(tdev, pos, count, arg);
564 pci_dev_put(tdev);
565 return ret;
568 static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
569 const void *arg)
571 struct pci_dev *tdev = pci_get_slot(dev->bus,
572 PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
573 ssize_t ret;
575 if (!tdev)
576 return -ENODEV;
578 ret = pci_write_vpd(tdev, pos, count, arg);
579 pci_dev_put(tdev);
580 return ret;
583 static int pci_vpd_f0_set_size(struct pci_dev *dev, size_t len)
585 struct pci_dev *tdev = pci_get_slot(dev->bus,
586 PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
587 int ret;
589 if (!tdev)
590 return -ENODEV;
592 ret = pci_set_vpd_size(tdev, len);
593 pci_dev_put(tdev);
594 return ret;
597 static const struct pci_vpd_ops pci_vpd_f0_ops = {
598 .read = pci_vpd_f0_read,
599 .write = pci_vpd_f0_write,
600 .set_size = pci_vpd_f0_set_size,
603 int pci_vpd_init(struct pci_dev *dev)
605 struct pci_vpd *vpd;
606 u8 cap;
608 cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
609 if (!cap)
610 return -ENODEV;
612 vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
613 if (!vpd)
614 return -ENOMEM;
616 vpd->len = PCI_VPD_MAX_SIZE;
617 if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0)
618 vpd->ops = &pci_vpd_f0_ops;
619 else
620 vpd->ops = &pci_vpd_ops;
621 mutex_init(&vpd->lock);
622 vpd->cap = cap;
623 vpd->busy = 0;
624 vpd->valid = 0;
625 dev->vpd = vpd;
626 return 0;
629 void pci_vpd_release(struct pci_dev *dev)
631 kfree(dev->vpd);
635 * pci_cfg_access_lock - Lock PCI config reads/writes
636 * @dev: pci device struct
638 * When access is locked, any userspace reads or writes to config
639 * space and concurrent lock requests will sleep until access is
640 * allowed via pci_cfg_access_unlock() again.
642 void pci_cfg_access_lock(struct pci_dev *dev)
644 might_sleep();
646 raw_spin_lock_irq(&pci_lock);
647 if (dev->block_cfg_access)
648 pci_wait_cfg(dev);
649 dev->block_cfg_access = 1;
650 raw_spin_unlock_irq(&pci_lock);
652 EXPORT_SYMBOL_GPL(pci_cfg_access_lock);
655 * pci_cfg_access_trylock - try to lock PCI config reads/writes
656 * @dev: pci device struct
658 * Same as pci_cfg_access_lock, but will return 0 if access is
659 * already locked, 1 otherwise. This function can be used from
660 * atomic contexts.
662 bool pci_cfg_access_trylock(struct pci_dev *dev)
664 unsigned long flags;
665 bool locked = true;
667 raw_spin_lock_irqsave(&pci_lock, flags);
668 if (dev->block_cfg_access)
669 locked = false;
670 else
671 dev->block_cfg_access = 1;
672 raw_spin_unlock_irqrestore(&pci_lock, flags);
674 return locked;
676 EXPORT_SYMBOL_GPL(pci_cfg_access_trylock);
679 * pci_cfg_access_unlock - Unlock PCI config reads/writes
680 * @dev: pci device struct
682 * This function allows PCI config accesses to resume.
684 void pci_cfg_access_unlock(struct pci_dev *dev)
686 unsigned long flags;
688 raw_spin_lock_irqsave(&pci_lock, flags);
690 /* This indicates a problem in the caller, but we don't need
691 * to kill them, unlike a double-block above. */
692 WARN_ON(!dev->block_cfg_access);
694 dev->block_cfg_access = 0;
695 raw_spin_unlock_irqrestore(&pci_lock, flags);
697 wake_up_all(&pci_cfg_wait);
699 EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
701 static inline int pcie_cap_version(const struct pci_dev *dev)
703 return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS;
706 static bool pcie_downstream_port(const struct pci_dev *dev)
708 int type = pci_pcie_type(dev);
710 return type == PCI_EXP_TYPE_ROOT_PORT ||
711 type == PCI_EXP_TYPE_DOWNSTREAM ||
712 type == PCI_EXP_TYPE_PCIE_BRIDGE;
715 bool pcie_cap_has_lnkctl(const struct pci_dev *dev)
717 int type = pci_pcie_type(dev);
719 return type == PCI_EXP_TYPE_ENDPOINT ||
720 type == PCI_EXP_TYPE_LEG_END ||
721 type == PCI_EXP_TYPE_ROOT_PORT ||
722 type == PCI_EXP_TYPE_UPSTREAM ||
723 type == PCI_EXP_TYPE_DOWNSTREAM ||
724 type == PCI_EXP_TYPE_PCI_BRIDGE ||
725 type == PCI_EXP_TYPE_PCIE_BRIDGE;
728 static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev)
730 return pcie_downstream_port(dev) &&
731 pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT;
734 static inline bool pcie_cap_has_rtctl(const struct pci_dev *dev)
736 int type = pci_pcie_type(dev);
738 return type == PCI_EXP_TYPE_ROOT_PORT ||
739 type == PCI_EXP_TYPE_RC_EC;
742 static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
744 if (!pci_is_pcie(dev))
745 return false;
747 switch (pos) {
748 case PCI_EXP_FLAGS:
749 return true;
750 case PCI_EXP_DEVCAP:
751 case PCI_EXP_DEVCTL:
752 case PCI_EXP_DEVSTA:
753 return true;
754 case PCI_EXP_LNKCAP:
755 case PCI_EXP_LNKCTL:
756 case PCI_EXP_LNKSTA:
757 return pcie_cap_has_lnkctl(dev);
758 case PCI_EXP_SLTCAP:
759 case PCI_EXP_SLTCTL:
760 case PCI_EXP_SLTSTA:
761 return pcie_cap_has_sltctl(dev);
762 case PCI_EXP_RTCTL:
763 case PCI_EXP_RTCAP:
764 case PCI_EXP_RTSTA:
765 return pcie_cap_has_rtctl(dev);
766 case PCI_EXP_DEVCAP2:
767 case PCI_EXP_DEVCTL2:
768 case PCI_EXP_LNKCAP2:
769 case PCI_EXP_LNKCTL2:
770 case PCI_EXP_LNKSTA2:
771 return pcie_cap_version(dev) > 1;
772 default:
773 return false;
778 * Note that these accessor functions are only for the "PCI Express
779 * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the
780 * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.)
782 int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)
784 int ret;
786 *val = 0;
787 if (pos & 1)
788 return -EINVAL;
790 if (pcie_capability_reg_implemented(dev, pos)) {
791 ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
793 * Reset *val to 0 if pci_read_config_word() fails, it may
794 * have been written as 0xFFFF if hardware error happens
795 * during pci_read_config_word().
797 if (ret)
798 *val = 0;
799 return ret;
803 * For Functions that do not implement the Slot Capabilities,
804 * Slot Status, and Slot Control registers, these spaces must
805 * be hardwired to 0b, with the exception of the Presence Detect
806 * State bit in the Slot Status register of Downstream Ports,
807 * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8)
809 if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
810 pos == PCI_EXP_SLTSTA)
811 *val = PCI_EXP_SLTSTA_PDS;
813 return 0;
815 EXPORT_SYMBOL(pcie_capability_read_word);
817 int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)
819 int ret;
821 *val = 0;
822 if (pos & 3)
823 return -EINVAL;
825 if (pcie_capability_reg_implemented(dev, pos)) {
826 ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
828 * Reset *val to 0 if pci_read_config_dword() fails, it may
829 * have been written as 0xFFFFFFFF if hardware error happens
830 * during pci_read_config_dword().
832 if (ret)
833 *val = 0;
834 return ret;
837 if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
838 pos == PCI_EXP_SLTSTA)
839 *val = PCI_EXP_SLTSTA_PDS;
841 return 0;
843 EXPORT_SYMBOL(pcie_capability_read_dword);
845 int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
847 if (pos & 1)
848 return -EINVAL;
850 if (!pcie_capability_reg_implemented(dev, pos))
851 return 0;
853 return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
855 EXPORT_SYMBOL(pcie_capability_write_word);
857 int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
859 if (pos & 3)
860 return -EINVAL;
862 if (!pcie_capability_reg_implemented(dev, pos))
863 return 0;
865 return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);
867 EXPORT_SYMBOL(pcie_capability_write_dword);
869 int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
870 u16 clear, u16 set)
872 int ret;
873 u16 val;
875 ret = pcie_capability_read_word(dev, pos, &val);
876 if (!ret) {
877 val &= ~clear;
878 val |= set;
879 ret = pcie_capability_write_word(dev, pos, val);
882 return ret;
884 EXPORT_SYMBOL(pcie_capability_clear_and_set_word);
886 int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
887 u32 clear, u32 set)
889 int ret;
890 u32 val;
892 ret = pcie_capability_read_dword(dev, pos, &val);
893 if (!ret) {
894 val &= ~clear;
895 val |= set;
896 ret = pcie_capability_write_dword(dev, pos, val);
899 return ret;
901 EXPORT_SYMBOL(pcie_capability_clear_and_set_dword);
903 int pci_read_config_byte(const struct pci_dev *dev, int where, u8 *val)
905 if (pci_dev_is_disconnected(dev)) {
906 *val = ~0;
907 return PCIBIOS_DEVICE_NOT_FOUND;
909 return pci_bus_read_config_byte(dev->bus, dev->devfn, where, val);
911 EXPORT_SYMBOL(pci_read_config_byte);
913 int pci_read_config_word(const struct pci_dev *dev, int where, u16 *val)
915 if (pci_dev_is_disconnected(dev)) {
916 *val = ~0;
917 return PCIBIOS_DEVICE_NOT_FOUND;
919 return pci_bus_read_config_word(dev->bus, dev->devfn, where, val);
921 EXPORT_SYMBOL(pci_read_config_word);
923 int pci_read_config_dword(const struct pci_dev *dev, int where,
924 u32 *val)
926 if (pci_dev_is_disconnected(dev)) {
927 *val = ~0;
928 return PCIBIOS_DEVICE_NOT_FOUND;
930 return pci_bus_read_config_dword(dev->bus, dev->devfn, where, val);
932 EXPORT_SYMBOL(pci_read_config_dword);
934 int pci_write_config_byte(const struct pci_dev *dev, int where, u8 val)
936 if (pci_dev_is_disconnected(dev))
937 return PCIBIOS_DEVICE_NOT_FOUND;
938 return pci_bus_write_config_byte(dev->bus, dev->devfn, where, val);
940 EXPORT_SYMBOL(pci_write_config_byte);
942 int pci_write_config_word(const struct pci_dev *dev, int where, u16 val)
944 if (pci_dev_is_disconnected(dev))
945 return PCIBIOS_DEVICE_NOT_FOUND;
946 return pci_bus_write_config_word(dev->bus, dev->devfn, where, val);
948 EXPORT_SYMBOL(pci_write_config_word);
950 int pci_write_config_dword(const struct pci_dev *dev, int where,
951 u32 val)
953 if (pci_dev_is_disconnected(dev))
954 return PCIBIOS_DEVICE_NOT_FOUND;
955 return pci_bus_write_config_dword(dev->bus, dev->devfn, where, val);
957 EXPORT_SYMBOL(pci_write_config_dword);