3 #include <linux/module.h>
4 #include <linux/sched.h>
5 #include <linux/slab.h>
7 #include <linux/wait.h>
24 #define PCI_byte_BAD 0
25 #define PCI_word_BAD (pos & 1)
26 #define PCI_dword_BAD (pos & 3)
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) \
33 unsigned long flags; \
35 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
36 raw_spin_lock_irqsave(&pci_lock, flags); \
37 res = bus->ops->read(bus, devfn, pos, len, &data); \
38 *value = (type)data; \
39 raw_spin_unlock_irqrestore(&pci_lock, flags); \
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) \
48 unsigned long flags; \
49 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
50 raw_spin_lock_irqsave(&pci_lock, flags); \
51 res = bus->ops->write(bus, devfn, pos, len, value); \
52 raw_spin_unlock_irqrestore(&pci_lock, flags); \
100 if (!dev->
vpd || !dev->
vpd->ops)
102 return dev->
vpd->ops->read(dev, pos, count, buf);
116 if (!dev->
vpd || !dev->
vpd->ops)
118 return dev->
vpd->ops->write(dev, pos, count, buf);
136 __add_wait_queue(&pci_cfg_wait, &
wait);
143 __remove_wait_queue(&pci_cfg_wait, &
wait);
147 #define PCI_USER_READ_CONFIG(size,type) \
148 int pci_user_read_config_##size \
149 (struct pci_dev *dev, int pos, type *val) \
153 if (PCI_##size##_BAD) \
155 raw_spin_lock_irq(&pci_lock); \
156 if (unlikely(dev->block_cfg_access)) \
158 ret = dev->bus->ops->read(dev->bus, dev->devfn, \
159 pos, sizeof(type), &data); \
160 raw_spin_unlock_irq(&pci_lock); \
166 EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
169 #define PCI_USER_WRITE_CONFIG(size,type) \
170 int pci_user_write_config_##size \
171 (struct pci_dev *dev, int pos, type val) \
174 if (PCI_##size##_BAD) \
176 raw_spin_lock_irq(&pci_lock); \
177 if (unlikely(dev->block_cfg_access)) \
179 ret = dev->bus->ops->write(dev->bus, dev->devfn, \
180 pos, sizeof(type), val); \
181 raw_spin_unlock_irq(&pci_lock); \
186 EXPORT_SYMBOL_GPL(pci_user_write_config_##size);
197 #define PCI_VPD_PCI22_SIZE (PCI_VPD_ADDR_MASK + 1)
215 static int pci_vpd_pci22_wait(
struct pci_dev *dev)
239 "vpd r/w failed. This is likely a firmware "
240 "bug on this device. Contact the card "
241 "vendor for a firmware update.");
244 if (fatal_signal_pending(
current))
260 if (pos < 0 || pos > vpd->
base.len || end > vpd->
base.len)
266 ret = pci_vpd_pci22_wait(dev);
272 unsigned int i,
skip;
280 ret = pci_vpd_pci22_wait(dev);
289 for (i = 0; i <
sizeof(
u32); i++) {
300 return ret ? ret :
count;
303 static ssize_t pci_vpd_pci22_write(
struct pci_dev *dev, loff_t pos,
size_t count,
309 loff_t end = pos +
count;
312 if (pos < 0 || (pos & 3) || (count & 3) || end > vpd->
base.len)
318 ret = pci_vpd_pci22_wait(dev);
340 ret = pci_vpd_pci22_wait(dev);
348 return ret ? ret :
count;
351 static void pci_vpd_pci22_release(
struct pci_dev *dev)
356 static const struct pci_vpd_ops pci_vpd_pci22_ops = {
357 .read = pci_vpd_pci22_read,
358 .write = pci_vpd_pci22_write,
359 .release = pci_vpd_pci22_release,
375 vpd->
base.ops = &pci_vpd_pci22_ops;
396 if (size > dev->
vpd->len)
473 static inline int pcie_cap_version(
const struct pci_dev *dev)
478 static inline bool pcie_cap_has_devctl(
const struct pci_dev *dev)
483 static inline bool pcie_cap_has_lnkctl(
const struct pci_dev *dev)
485 int type = pci_pcie_type(dev);
487 return pcie_cap_version(dev) > 1 ||
493 static inline bool pcie_cap_has_sltctl(
const struct pci_dev *dev)
495 int type = pci_pcie_type(dev);
497 return pcie_cap_version(dev) > 1 ||
503 static inline bool pcie_cap_has_rtctl(
const struct pci_dev *dev)
505 int type = pci_pcie_type(dev);
507 return pcie_cap_version(dev) > 1 ||
512 static bool pcie_capability_reg_implemented(
struct pci_dev *dev,
int pos)
514 if (!pci_is_pcie(dev))
523 return pcie_cap_has_devctl(dev);
527 return pcie_cap_has_lnkctl(dev);
531 return pcie_cap_has_sltctl(dev);
535 return pcie_cap_has_rtctl(dev);
541 return pcie_cap_version(dev) > 1;
560 if (pcie_capability_reg_implemented(dev, pos)) {
561 ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
596 if (pcie_capability_reg_implemented(dev, pos)) {
597 ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
622 if (!pcie_capability_reg_implemented(dev, pos))
625 return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
634 if (!pcie_capability_reg_implemented(dev, pos))
637 return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);