blob: 0d9f6b21babb1bb4908571d5b37cfaad5298525b [file] [log] [blame]
Bjorn Helgaas7328c8f2018-01-26 11:45:16 -06001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/pci.h>
3#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09004#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/ioport.h>
Matthew Wilcox7ea7e982006-10-19 09:41:28 -06006#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Adrian Bunk48b19142005-11-06 01:45:08 +01008#include "pci.h"
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010/*
11 * This interrupt-safe spinlock protects all accesses to PCI
12 * configuration space.
13 */
14
Jan Kiszkaa2e27782011-11-04 09:46:00 +010015DEFINE_RAW_SPINLOCK(pci_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -060018 * Wrappers for all PCI configuration access functions. They just check
19 * alignment, do locking and call the low-level functions pointed to
20 * by pci_dev->ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#define PCI_byte_BAD 0
24#define PCI_word_BAD (pos & 1)
25#define PCI_dword_BAD (pos & 3)
26
Thomas Gleixner714fe382017-03-16 22:50:06 +010027#ifdef CONFIG_PCI_LOCKLESS_CONFIG
28# define pci_lock_config(f) do { (void)(f); } while (0)
29# define pci_unlock_config(f) do { (void)(f); } while (0)
30#else
31# define pci_lock_config(f) raw_spin_lock_irqsave(&pci_lock, f)
32# define pci_unlock_config(f) raw_spin_unlock_irqrestore(&pci_lock, f)
33#endif
34
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080035#define PCI_OP_READ(size, type, len) \
Keith Busch5180fd92018-09-18 17:58:37 -060036int noinline pci_bus_read_config_##size \
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 (struct pci_bus *bus, unsigned int devfn, int pos, type *value) \
38{ \
39 int res; \
40 unsigned long flags; \
41 u32 data = 0; \
42 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
Thomas Gleixner714fe382017-03-16 22:50:06 +010043 pci_lock_config(flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 res = bus->ops->read(bus, devfn, pos, len, &data); \
Naveen Naiduf4f7eb42021-11-18 19:33:12 +053045 if (res) \
46 PCI_SET_ERROR_RESPONSE(value); \
47 else \
48 *value = (type)data; \
Thomas Gleixner714fe382017-03-16 22:50:06 +010049 pci_unlock_config(flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return res; \
51}
52
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080053#define PCI_OP_WRITE(size, type, len) \
Keith Busch5180fd92018-09-18 17:58:37 -060054int noinline pci_bus_write_config_##size \
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 (struct pci_bus *bus, unsigned int devfn, int pos, type value) \
56{ \
57 int res; \
58 unsigned long flags; \
59 if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
Thomas Gleixner714fe382017-03-16 22:50:06 +010060 pci_lock_config(flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 res = bus->ops->write(bus, devfn, pos, len, value); \
Thomas Gleixner714fe382017-03-16 22:50:06 +010062 pci_unlock_config(flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return res; \
64}
65
66PCI_OP_READ(byte, u8, 1)
67PCI_OP_READ(word, u16, 2)
68PCI_OP_READ(dword, u32, 4)
69PCI_OP_WRITE(byte, u8, 1)
70PCI_OP_WRITE(word, u16, 2)
71PCI_OP_WRITE(dword, u32, 4)
72
73EXPORT_SYMBOL(pci_bus_read_config_byte);
74EXPORT_SYMBOL(pci_bus_read_config_word);
75EXPORT_SYMBOL(pci_bus_read_config_dword);
76EXPORT_SYMBOL(pci_bus_write_config_byte);
77EXPORT_SYMBOL(pci_bus_write_config_word);
78EXPORT_SYMBOL(pci_bus_write_config_dword);
Brian Kinge04b0ea2005-09-27 01:21:55 -070079
Rob Herring1f94a942015-01-09 20:34:39 -060080int pci_generic_config_read(struct pci_bus *bus, unsigned int devfn,
81 int where, int size, u32 *val)
82{
83 void __iomem *addr;
84
85 addr = bus->ops->map_bus(bus, devfn, where);
Naveen Naidu316df702021-11-18 19:33:14 +053086 if (!addr)
Rob Herring1f94a942015-01-09 20:34:39 -060087 return PCIBIOS_DEVICE_NOT_FOUND;
Rob Herring1f94a942015-01-09 20:34:39 -060088
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}
98EXPORT_SYMBOL_GPL(pci_generic_config_read);
99
100int 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}
118EXPORT_SYMBOL_GPL(pci_generic_config_write);
119
120int 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);
Naveen Naidu316df702021-11-18 19:33:14 +0530126 if (!addr)
Rob Herring1f94a942015-01-09 20:34:39 -0600127 return PCIBIOS_DEVICE_NOT_FOUND;
Rob Herring1f94a942015-01-09 20:34:39 -0600128
129 *val = readl(addr);
130
131 if (size <= 2)
132 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
133
134 return PCIBIOS_SUCCESSFUL;
135}
136EXPORT_SYMBOL_GPL(pci_generic_config_read32);
137
138int pci_generic_config_write32(struct pci_bus *bus, unsigned int devfn,
139 int where, int size, u32 val)
140{
141 void __iomem *addr;
142 u32 mask, tmp;
143
144 addr = bus->ops->map_bus(bus, devfn, where & ~0x3);
145 if (!addr)
146 return PCIBIOS_DEVICE_NOT_FOUND;
147
148 if (size == 4) {
149 writel(val, addr);
150 return PCIBIOS_SUCCESSFUL;
Rob Herring1f94a942015-01-09 20:34:39 -0600151 }
152
Bjorn Helgaasfb265922016-10-31 16:00:01 -0500153 /*
154 * In general, hardware that supports only 32-bit writes on PCI is
155 * not spec-compliant. For example, software may perform a 16-bit
156 * write. If the hardware only supports 32-bit accesses, we must
157 * do a 32-bit read, merge in the 16 bits we intend to write,
158 * followed by a 32-bit write. If the 16 bits we *don't* intend to
159 * write happen to have any RW1C (write-one-to-clear) bits set, we
160 * just inadvertently cleared something we shouldn't have.
161 */
162 dev_warn_ratelimited(&bus->dev, "%d-byte config write to %04x:%02x:%02x.%d offset %#x may corrupt adjacent RW1C bits\n",
163 size, pci_domain_nr(bus), bus->number,
164 PCI_SLOT(devfn), PCI_FUNC(devfn), where);
165
166 mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
Rob Herring1f94a942015-01-09 20:34:39 -0600167 tmp = readl(addr) & mask;
168 tmp |= val << ((where & 0x3) * 8);
169 writel(tmp, addr);
170
171 return PCIBIOS_SUCCESSFUL;
172}
173EXPORT_SYMBOL_GPL(pci_generic_config_write32);
174
Huang Yinga72b46c2009-04-24 10:45:17 +0800175/**
176 * pci_bus_set_ops - Set raw operations of pci bus
177 * @bus: pci bus struct
178 * @ops: new raw operations
179 *
180 * Return previous raw operations
181 */
182struct pci_ops *pci_bus_set_ops(struct pci_bus *bus, struct pci_ops *ops)
183{
184 struct pci_ops *old_ops;
185 unsigned long flags;
186
Thomas Gleixner511dd982010-02-17 14:35:19 +0000187 raw_spin_lock_irqsave(&pci_lock, flags);
Huang Yinga72b46c2009-04-24 10:45:17 +0800188 old_ops = bus->ops;
189 bus->ops = ops;
Thomas Gleixner511dd982010-02-17 14:35:19 +0000190 raw_spin_unlock_irqrestore(&pci_lock, flags);
Huang Yinga72b46c2009-04-24 10:45:17 +0800191 return old_ops;
192}
193EXPORT_SYMBOL(pci_bus_set_ops);
Stephen Hemminger287d19c2008-12-18 09:17:16 -0800194
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600195/*
196 * The following routines are to prevent the user from accessing PCI config
197 * space when it's unsafe to do so. Some devices require this during BIST and
198 * we're required to prevent it during D-state transitions.
199 *
200 * We have a bit per device to indicate it's blocked and a global wait queue
201 * for callers to sleep on until devices are unblocked.
202 */
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100203static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700204
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100205static noinline void pci_wait_cfg(struct pci_dev *dev)
Bjorn Helgaas2a7e32d02020-06-25 18:14:55 -0500206 __must_hold(&pci_lock)
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600207{
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600208 do {
Thomas Gleixner511dd982010-02-17 14:35:19 +0000209 raw_spin_unlock_irq(&pci_lock);
Bjorn Helgaas2a7e32d02020-06-25 18:14:55 -0500210 wait_event(pci_cfg_wait, !dev->block_cfg_access);
Thomas Gleixner511dd982010-02-17 14:35:19 +0000211 raw_spin_lock_irq(&pci_lock);
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100212 } while (dev->block_cfg_access);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700213}
214
Greg Thelen34e32072011-04-17 08:20:32 -0700215/* Returns 0 on success, negative values indicate error. */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800216#define PCI_USER_READ_CONFIG(size, type) \
Brian Kinge04b0ea2005-09-27 01:21:55 -0700217int pci_user_read_config_##size \
218 (struct pci_dev *dev, int pos, type *val) \
219{ \
Gavin Shand97ffe22014-05-21 15:23:30 +1000220 int ret = PCIBIOS_SUCCESSFUL; \
Brian Kinge04b0ea2005-09-27 01:21:55 -0700221 u32 data = -1; \
Greg Thelen34e32072011-04-17 08:20:32 -0700222 if (PCI_##size##_BAD) \
223 return -EINVAL; \
Thomas Gleixner511dd982010-02-17 14:35:19 +0000224 raw_spin_lock_irq(&pci_lock); \
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100225 if (unlikely(dev->block_cfg_access)) \
226 pci_wait_cfg(dev); \
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600227 ret = dev->bus->ops->read(dev->bus, dev->devfn, \
Brian Kinge04b0ea2005-09-27 01:21:55 -0700228 pos, sizeof(type), &data); \
Thomas Gleixner511dd982010-02-17 14:35:19 +0000229 raw_spin_unlock_irq(&pci_lock); \
Naveen Naiduf4f7eb42021-11-18 19:33:12 +0530230 if (ret) \
231 PCI_SET_ERROR_RESPONSE(val); \
232 else \
233 *val = (type)data; \
Gavin Shand97ffe22014-05-21 15:23:30 +1000234 return pcibios_err_to_errno(ret); \
Alex Williamsonc63587d2012-06-11 05:27:19 +0000235} \
236EXPORT_SYMBOL_GPL(pci_user_read_config_##size);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700237
Greg Thelen34e32072011-04-17 08:20:32 -0700238/* Returns 0 on success, negative values indicate error. */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800239#define PCI_USER_WRITE_CONFIG(size, type) \
Brian Kinge04b0ea2005-09-27 01:21:55 -0700240int pci_user_write_config_##size \
241 (struct pci_dev *dev, int pos, type val) \
242{ \
Gavin Shand97ffe22014-05-21 15:23:30 +1000243 int ret = PCIBIOS_SUCCESSFUL; \
Greg Thelen34e32072011-04-17 08:20:32 -0700244 if (PCI_##size##_BAD) \
245 return -EINVAL; \
Thomas Gleixner511dd982010-02-17 14:35:19 +0000246 raw_spin_lock_irq(&pci_lock); \
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100247 if (unlikely(dev->block_cfg_access)) \
248 pci_wait_cfg(dev); \
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600249 ret = dev->bus->ops->write(dev->bus, dev->devfn, \
Brian Kinge04b0ea2005-09-27 01:21:55 -0700250 pos, sizeof(type), val); \
Thomas Gleixner511dd982010-02-17 14:35:19 +0000251 raw_spin_unlock_irq(&pci_lock); \
Gavin Shand97ffe22014-05-21 15:23:30 +1000252 return pcibios_err_to_errno(ret); \
Alex Williamsonc63587d2012-06-11 05:27:19 +0000253} \
254EXPORT_SYMBOL_GPL(pci_user_write_config_##size);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700255
256PCI_USER_READ_CONFIG(byte, u8)
257PCI_USER_READ_CONFIG(word, u16)
258PCI_USER_READ_CONFIG(dword, u32)
259PCI_USER_WRITE_CONFIG(byte, u8)
260PCI_USER_WRITE_CONFIG(word, u16)
261PCI_USER_WRITE_CONFIG(dword, u32)
262
263/**
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100264 * pci_cfg_access_lock - Lock PCI config reads/writes
Brian Kinge04b0ea2005-09-27 01:21:55 -0700265 * @dev: pci device struct
266 *
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100267 * When access is locked, any userspace reads or writes to config
268 * space and concurrent lock requests will sleep until access is
Brian Norris0b131b12017-03-27 17:46:14 -0700269 * allowed via pci_cfg_access_unlock() again.
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600270 */
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100271void pci_cfg_access_lock(struct pci_dev *dev)
Brian Kinge04b0ea2005-09-27 01:21:55 -0700272{
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100273 might_sleep();
Brian Kinge04b0ea2005-09-27 01:21:55 -0700274
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100275 raw_spin_lock_irq(&pci_lock);
276 if (dev->block_cfg_access)
277 pci_wait_cfg(dev);
278 dev->block_cfg_access = 1;
279 raw_spin_unlock_irq(&pci_lock);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700280}
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100281EXPORT_SYMBOL_GPL(pci_cfg_access_lock);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700282
283/**
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100284 * pci_cfg_access_trylock - try to lock PCI config reads/writes
Brian Kinge04b0ea2005-09-27 01:21:55 -0700285 * @dev: pci device struct
286 *
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100287 * Same as pci_cfg_access_lock, but will return 0 if access is
288 * already locked, 1 otherwise. This function can be used from
289 * atomic contexts.
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600290 */
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100291bool pci_cfg_access_trylock(struct pci_dev *dev)
292{
293 unsigned long flags;
294 bool locked = true;
295
296 raw_spin_lock_irqsave(&pci_lock, flags);
297 if (dev->block_cfg_access)
298 locked = false;
299 else
300 dev->block_cfg_access = 1;
301 raw_spin_unlock_irqrestore(&pci_lock, flags);
302
303 return locked;
304}
305EXPORT_SYMBOL_GPL(pci_cfg_access_trylock);
306
307/**
308 * pci_cfg_access_unlock - Unlock PCI config reads/writes
309 * @dev: pci device struct
310 *
311 * This function allows PCI config accesses to resume.
312 */
313void pci_cfg_access_unlock(struct pci_dev *dev)
Brian Kinge04b0ea2005-09-27 01:21:55 -0700314{
315 unsigned long flags;
316
Thomas Gleixner511dd982010-02-17 14:35:19 +0000317 raw_spin_lock_irqsave(&pci_lock, flags);
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600318
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -0600319 /*
320 * This indicates a problem in the caller, but we don't need
321 * to kill them, unlike a double-block above.
322 */
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100323 WARN_ON(!dev->block_cfg_access);
Matthew Wilcox7ea7e982006-10-19 09:41:28 -0600324
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100325 dev->block_cfg_access = 0;
Thomas Gleixner511dd982010-02-17 14:35:19 +0000326 raw_spin_unlock_irqrestore(&pci_lock, flags);
Bjorn Helgaascdcb33f2017-01-13 18:05:12 -0600327
328 wake_up_all(&pci_cfg_wait);
Brian Kinge04b0ea2005-09-27 01:21:55 -0700329}
Jan Kiszkafb51ccb2011-11-04 09:45:59 +0100330EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800331
332static inline int pcie_cap_version(const struct pci_dev *dev)
333{
Myron Stowe1c531d82013-01-25 17:55:45 -0700334 return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS;
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800335}
336
Yinghai Lu7a1562d2014-11-11 12:09:46 -0800337bool pcie_cap_has_lnkctl(const struct pci_dev *dev)
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800338{
339 int type = pci_pcie_type(dev);
340
Bjorn Helgaasc8b303d2013-08-28 11:33:53 -0600341 return type == PCI_EXP_TYPE_ENDPOINT ||
Bjorn Helgaasd3694d42013-08-27 09:54:40 -0600342 type == PCI_EXP_TYPE_LEG_END ||
343 type == PCI_EXP_TYPE_ROOT_PORT ||
344 type == PCI_EXP_TYPE_UPSTREAM ||
345 type == PCI_EXP_TYPE_DOWNSTREAM ||
346 type == PCI_EXP_TYPE_PCI_BRIDGE ||
347 type == PCI_EXP_TYPE_PCIE_BRIDGE;
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800348}
349
350static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev)
351{
Bjorn Helgaasffb4d602015-06-24 16:05:54 -0500352 return pcie_downstream_port(dev) &&
Bjorn Helgaas6d3a1742013-08-28 12:01:03 -0600353 pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT;
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800354}
355
Patel, Mayurkumaraf65d1a2019-10-18 16:52:21 +0000356bool pcie_cap_has_rtctl(const struct pci_dev *dev)
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800357{
358 int type = pci_pcie_type(dev);
359
Bjorn Helgaasc8b303d2013-08-28 11:33:53 -0600360 return type == PCI_EXP_TYPE_ROOT_PORT ||
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800361 type == PCI_EXP_TYPE_RC_EC;
362}
363
364static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
365{
366 if (!pci_is_pcie(dev))
367 return false;
368
369 switch (pos) {
Alex Williamson969daa32013-02-14 11:35:42 -0700370 case PCI_EXP_FLAGS:
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800371 return true;
372 case PCI_EXP_DEVCAP:
373 case PCI_EXP_DEVCTL:
374 case PCI_EXP_DEVSTA:
Bjorn Helgaasfed24512013-08-28 12:03:42 -0600375 return true;
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800376 case PCI_EXP_LNKCAP:
377 case PCI_EXP_LNKCTL:
378 case PCI_EXP_LNKSTA:
379 return pcie_cap_has_lnkctl(dev);
380 case PCI_EXP_SLTCAP:
381 case PCI_EXP_SLTCTL:
382 case PCI_EXP_SLTSTA:
383 return pcie_cap_has_sltctl(dev);
384 case PCI_EXP_RTCTL:
385 case PCI_EXP_RTCAP:
386 case PCI_EXP_RTSTA:
387 return pcie_cap_has_rtctl(dev);
388 case PCI_EXP_DEVCAP2:
389 case PCI_EXP_DEVCTL2:
390 case PCI_EXP_LNKCAP2:
391 case PCI_EXP_LNKCTL2:
392 case PCI_EXP_LNKSTA2:
393 return pcie_cap_version(dev) > 1;
394 default:
395 return false;
396 }
397}
398
399/*
400 * Note that these accessor functions are only for the "PCI Express
401 * Capability" (see PCIe spec r3.0, sec 7.8). They do not apply to the
402 * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.)
403 */
404int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)
405{
406 int ret;
407
408 *val = 0;
409 if (pos & 1)
Bolarinwa Olayemi Saheedb9153582020-06-15 09:32:25 +0200410 return PCIBIOS_BAD_REGISTER_NUMBER;
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800411
412 if (pcie_capability_reg_implemented(dev, pos)) {
413 ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
414 /*
Naveen Naidu289e3ea2021-11-18 19:33:32 +0530415 * Reset *val to 0 if pci_read_config_word() fails; it may
416 * have been written as 0xFFFF (PCI_ERROR_RESPONSE) if the
417 * config read failed on PCI.
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800418 */
419 if (ret)
420 *val = 0;
421 return ret;
422 }
423
424 /*
425 * For Functions that do not implement the Slot Capabilities,
426 * Slot Status, and Slot Control registers, these spaces must
427 * be hardwired to 0b, with the exception of the Presence Detect
428 * State bit in the Slot Status register of Downstream Ports,
429 * which must be hardwired to 1b. (PCIe Base Spec 3.0, sec 7.8)
430 */
Bjorn Helgaasffb4d602015-06-24 16:05:54 -0500431 if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
432 pos == PCI_EXP_SLTSTA)
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800433 *val = PCI_EXP_SLTSTA_PDS;
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800434
435 return 0;
436}
437EXPORT_SYMBOL(pcie_capability_read_word);
438
439int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)
440{
441 int ret;
442
443 *val = 0;
444 if (pos & 3)
Bolarinwa Olayemi Saheedb9153582020-06-15 09:32:25 +0200445 return PCIBIOS_BAD_REGISTER_NUMBER;
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800446
447 if (pcie_capability_reg_implemented(dev, pos)) {
448 ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
449 /*
Naveen Naidu289e3ea2021-11-18 19:33:32 +0530450 * Reset *val to 0 if pci_read_config_dword() fails; it may
451 * have been written as 0xFFFFFFFF (PCI_ERROR_RESPONSE) if
452 * the config read failed on PCI.
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800453 */
454 if (ret)
455 *val = 0;
456 return ret;
457 }
458
Bjorn Helgaasffb4d602015-06-24 16:05:54 -0500459 if (pci_is_pcie(dev) && pcie_downstream_port(dev) &&
460 pos == PCI_EXP_SLTSTA)
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800461 *val = PCI_EXP_SLTSTA_PDS;
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800462
463 return 0;
464}
465EXPORT_SYMBOL(pcie_capability_read_dword);
466
467int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
468{
469 if (pos & 1)
Bolarinwa Olayemi Saheedb9153582020-06-15 09:32:25 +0200470 return PCIBIOS_BAD_REGISTER_NUMBER;
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800471
472 if (!pcie_capability_reg_implemented(dev, pos))
473 return 0;
474
475 return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
476}
477EXPORT_SYMBOL(pcie_capability_write_word);
478
479int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
480{
481 if (pos & 3)
Bolarinwa Olayemi Saheedb9153582020-06-15 09:32:25 +0200482 return PCIBIOS_BAD_REGISTER_NUMBER;
Jiang Liu8c0d3a02012-07-24 17:20:05 +0800483
484 if (!pcie_capability_reg_implemented(dev, pos))
485 return 0;
486
487 return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);
488}
489EXPORT_SYMBOL(pcie_capability_write_dword);
490
491int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
492 u16 clear, u16 set)
493{
494 int ret;
495 u16 val;
496
497 ret = pcie_capability_read_word(dev, pos, &val);
498 if (!ret) {
499 val &= ~clear;
500 val |= set;
501 ret = pcie_capability_write_word(dev, pos, val);
502 }
503
504 return ret;
505}
506EXPORT_SYMBOL(pcie_capability_clear_and_set_word);
507
508int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos,
509 u32 clear, u32 set)
510{
511 int ret;
512 u32 val;
513
514 ret = pcie_capability_read_dword(dev, pos, &val);
515 if (!ret) {
516 val &= ~clear;
517 val |= set;
518 ret = pcie_capability_write_dword(dev, pos, val);
519 }
520
521 return ret;
522}
523EXPORT_SYMBOL(pcie_capability_clear_and_set_dword);
Keith Buschd3881e52017-02-07 14:32:33 -0500524
525int pci_read_config_byte(const struct pci_dev *dev, int where, u8 *val)
526{
Keith Busch4b103882017-03-29 22:49:06 -0500527 if (pci_dev_is_disconnected(dev)) {
Naveen Naidu9bc93102021-11-18 19:33:13 +0530528 PCI_SET_ERROR_RESPONSE(val);
Brian Norris449e2f92017-05-23 12:36:58 -0700529 return PCIBIOS_DEVICE_NOT_FOUND;
Keith Busch4b103882017-03-29 22:49:06 -0500530 }
Keith Buschd3881e52017-02-07 14:32:33 -0500531 return pci_bus_read_config_byte(dev->bus, dev->devfn, where, val);
532}
533EXPORT_SYMBOL(pci_read_config_byte);
534
535int pci_read_config_word(const struct pci_dev *dev, int where, u16 *val)
536{
Keith Busch4b103882017-03-29 22:49:06 -0500537 if (pci_dev_is_disconnected(dev)) {
Naveen Naidu9bc93102021-11-18 19:33:13 +0530538 PCI_SET_ERROR_RESPONSE(val);
Brian Norris449e2f92017-05-23 12:36:58 -0700539 return PCIBIOS_DEVICE_NOT_FOUND;
Keith Busch4b103882017-03-29 22:49:06 -0500540 }
Keith Buschd3881e52017-02-07 14:32:33 -0500541 return pci_bus_read_config_word(dev->bus, dev->devfn, where, val);
542}
543EXPORT_SYMBOL(pci_read_config_word);
544
545int pci_read_config_dword(const struct pci_dev *dev, int where,
546 u32 *val)
547{
Keith Busch4b103882017-03-29 22:49:06 -0500548 if (pci_dev_is_disconnected(dev)) {
Naveen Naidu9bc93102021-11-18 19:33:13 +0530549 PCI_SET_ERROR_RESPONSE(val);
Brian Norris449e2f92017-05-23 12:36:58 -0700550 return PCIBIOS_DEVICE_NOT_FOUND;
Keith Busch4b103882017-03-29 22:49:06 -0500551 }
Keith Buschd3881e52017-02-07 14:32:33 -0500552 return pci_bus_read_config_dword(dev->bus, dev->devfn, where, val);
553}
554EXPORT_SYMBOL(pci_read_config_dword);
555
556int pci_write_config_byte(const struct pci_dev *dev, int where, u8 val)
557{
Keith Busch4b103882017-03-29 22:49:06 -0500558 if (pci_dev_is_disconnected(dev))
Brian Norris449e2f92017-05-23 12:36:58 -0700559 return PCIBIOS_DEVICE_NOT_FOUND;
Keith Buschd3881e52017-02-07 14:32:33 -0500560 return pci_bus_write_config_byte(dev->bus, dev->devfn, where, val);
561}
562EXPORT_SYMBOL(pci_write_config_byte);
563
564int pci_write_config_word(const struct pci_dev *dev, int where, u16 val)
565{
Keith Busch4b103882017-03-29 22:49:06 -0500566 if (pci_dev_is_disconnected(dev))
Brian Norris449e2f92017-05-23 12:36:58 -0700567 return PCIBIOS_DEVICE_NOT_FOUND;
Keith Buschd3881e52017-02-07 14:32:33 -0500568 return pci_bus_write_config_word(dev->bus, dev->devfn, where, val);
569}
570EXPORT_SYMBOL(pci_write_config_word);
571
572int pci_write_config_dword(const struct pci_dev *dev, int where,
573 u32 val)
574{
Keith Busch4b103882017-03-29 22:49:06 -0500575 if (pci_dev_is_disconnected(dev))
Brian Norris449e2f92017-05-23 12:36:58 -0700576 return PCIBIOS_DEVICE_NOT_FOUND;
Keith Buschd3881e52017-02-07 14:32:33 -0500577 return pci_bus_write_config_dword(dev->bus, dev->devfn, where, val);
578}
579EXPORT_SYMBOL(pci_write_config_dword);