]>
Commit | Line | Data |
---|---|---|
94e61088 | 1 | #include <linux/delay.h> |
1da177e4 LT |
2 | #include <linux/pci.h> |
3 | #include <linux/module.h> | |
174cd4b1 | 4 | #include <linux/sched/signal.h> |
5a0e3ad6 | 5 | #include <linux/slab.h> |
1da177e4 | 6 | #include <linux/ioport.h> |
7ea7e98f | 7 | #include <linux/wait.h> |
1da177e4 | 8 | |
48b19148 AB |
9 | #include "pci.h" |
10 | ||
1da177e4 LT |
11 | /* |
12 | * This interrupt-safe spinlock protects all accesses to PCI | |
13 | * configuration space. | |
14 | */ | |
15 | ||
a2e27787 | 16 | DEFINE_RAW_SPINLOCK(pci_lock); |
1da177e4 LT |
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 | ||
714fe383 TG |
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 | ||
ff3ce480 | 36 | #define PCI_OP_READ(size, type, len) \ |
1da177e4 LT |
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; \ | |
714fe383 | 44 | pci_lock_config(flags); \ |
1da177e4 LT |
45 | res = bus->ops->read(bus, devfn, pos, len, &data); \ |
46 | *value = (type)data; \ | |
714fe383 | 47 | pci_unlock_config(flags); \ |
1da177e4 LT |
48 | return res; \ |
49 | } | |
50 | ||
ff3ce480 | 51 | #define PCI_OP_WRITE(size, type, len) \ |
1da177e4 LT |
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; \ | |
714fe383 | 58 | pci_lock_config(flags); \ |
1da177e4 | 59 | res = bus->ops->write(bus, devfn, pos, len, value); \ |
714fe383 | 60 | pci_unlock_config(flags); \ |
1da177e4 LT |
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); | |
e04b0ea2 | 77 | |
1f94a94f RH |
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; | |
1f94a94f RH |
153 | } |
154 | ||
fb265923 BH |
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)); | |
1f94a94f RH |
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 | ||
a72b46c3 YH |
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 | ||
511dd98c | 189 | raw_spin_lock_irqsave(&pci_lock, flags); |
a72b46c3 YH |
190 | old_ops = bus->ops; |
191 | bus->ops = ops; | |
511dd98c | 192 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
a72b46c3 YH |
193 | return old_ops; |
194 | } | |
195 | EXPORT_SYMBOL(pci_bus_set_ops); | |
287d19ce | 196 | |
7ea7e98f MW |
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 | */ | |
fb51ccbf | 205 | static DECLARE_WAIT_QUEUE_HEAD(pci_cfg_wait); |
e04b0ea2 | 206 | |
fb51ccbf | 207 | static noinline void pci_wait_cfg(struct pci_dev *dev) |
7ea7e98f MW |
208 | { |
209 | DECLARE_WAITQUEUE(wait, current); | |
210 | ||
fb51ccbf | 211 | __add_wait_queue(&pci_cfg_wait, &wait); |
7ea7e98f MW |
212 | do { |
213 | set_current_state(TASK_UNINTERRUPTIBLE); | |
511dd98c | 214 | raw_spin_unlock_irq(&pci_lock); |
7ea7e98f | 215 | schedule(); |
511dd98c | 216 | raw_spin_lock_irq(&pci_lock); |
fb51ccbf JK |
217 | } while (dev->block_cfg_access); |
218 | __remove_wait_queue(&pci_cfg_wait, &wait); | |
e04b0ea2 BK |
219 | } |
220 | ||
34e32072 | 221 | /* Returns 0 on success, negative values indicate error. */ |
ff3ce480 | 222 | #define PCI_USER_READ_CONFIG(size, type) \ |
e04b0ea2 BK |
223 | int pci_user_read_config_##size \ |
224 | (struct pci_dev *dev, int pos, type *val) \ | |
225 | { \ | |
d97ffe23 | 226 | int ret = PCIBIOS_SUCCESSFUL; \ |
e04b0ea2 | 227 | u32 data = -1; \ |
34e32072 GT |
228 | if (PCI_##size##_BAD) \ |
229 | return -EINVAL; \ | |
511dd98c | 230 | raw_spin_lock_irq(&pci_lock); \ |
fb51ccbf JK |
231 | if (unlikely(dev->block_cfg_access)) \ |
232 | pci_wait_cfg(dev); \ | |
7ea7e98f | 233 | ret = dev->bus->ops->read(dev->bus, dev->devfn, \ |
e04b0ea2 | 234 | pos, sizeof(type), &data); \ |
511dd98c | 235 | raw_spin_unlock_irq(&pci_lock); \ |
e04b0ea2 | 236 | *val = (type)data; \ |
d97ffe23 | 237 | return pcibios_err_to_errno(ret); \ |
c63587d7 AW |
238 | } \ |
239 | EXPORT_SYMBOL_GPL(pci_user_read_config_##size); | |
e04b0ea2 | 240 | |
34e32072 | 241 | /* Returns 0 on success, negative values indicate error. */ |
ff3ce480 | 242 | #define PCI_USER_WRITE_CONFIG(size, type) \ |
e04b0ea2 BK |
243 | int pci_user_write_config_##size \ |
244 | (struct pci_dev *dev, int pos, type val) \ | |
245 | { \ | |
d97ffe23 | 246 | int ret = PCIBIOS_SUCCESSFUL; \ |
34e32072 GT |
247 | if (PCI_##size##_BAD) \ |
248 | return -EINVAL; \ | |
511dd98c | 249 | raw_spin_lock_irq(&pci_lock); \ |
fb51ccbf JK |
250 | if (unlikely(dev->block_cfg_access)) \ |
251 | pci_wait_cfg(dev); \ | |
7ea7e98f | 252 | ret = dev->bus->ops->write(dev->bus, dev->devfn, \ |
e04b0ea2 | 253 | pos, sizeof(type), val); \ |
511dd98c | 254 | raw_spin_unlock_irq(&pci_lock); \ |
d97ffe23 | 255 | return pcibios_err_to_errno(ret); \ |
c63587d7 AW |
256 | } \ |
257 | EXPORT_SYMBOL_GPL(pci_user_write_config_##size); | |
e04b0ea2 BK |
258 | |
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) | |
265 | ||
94e61088 BH |
266 | /* VPD access through PCI 2.2+ VPD capability */ |
267 | ||
fc0a407e BH |
268 | /** |
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 | |
274 | */ | |
275 | ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf) | |
276 | { | |
277 | if (!dev->vpd || !dev->vpd->ops) | |
278 | return -ENODEV; | |
279 | return dev->vpd->ops->read(dev, pos, count, buf); | |
280 | } | |
281 | EXPORT_SYMBOL(pci_read_vpd); | |
282 | ||
283 | /** | |
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 | |
289 | */ | |
290 | ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf) | |
291 | { | |
292 | if (!dev->vpd || !dev->vpd->ops) | |
293 | return -ENODEV; | |
294 | return dev->vpd->ops->write(dev, pos, count, buf); | |
295 | } | |
296 | EXPORT_SYMBOL(pci_write_vpd); | |
297 | ||
cb92148b HS |
298 | /** |
299 | * pci_set_vpd_size - Set size of Vital Product Data space | |
300 | * @dev: pci device struct | |
301 | * @len: size of vpd space | |
302 | */ | |
303 | int pci_set_vpd_size(struct pci_dev *dev, size_t len) | |
304 | { | |
305 | if (!dev->vpd || !dev->vpd->ops) | |
306 | return -ENODEV; | |
307 | return dev->vpd->ops->set_size(dev, len); | |
308 | } | |
309 | EXPORT_SYMBOL(pci_set_vpd_size); | |
310 | ||
f1cd93f9 | 311 | #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1) |
94e61088 | 312 | |
104daa71 HR |
313 | /** |
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 | |
317 | */ | |
f1cd93f9 | 318 | static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size) |
104daa71 HR |
319 | { |
320 | size_t off = 0; | |
321 | unsigned char header[1+2]; /* 1 byte tag, 2 bytes length */ | |
322 | ||
323 | while (off < old_size && | |
324 | pci_read_vpd(dev, off, 1, header) == 1) { | |
325 | unsigned char tag; | |
326 | ||
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; | |
340 | } | |
341 | off += PCI_VPD_LRDT_TAG_SIZE + | |
342 | pci_vpd_lrdt_size(header); | |
343 | } | |
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); | |
349 | } | |
350 | ||
351 | if (tag == PCI_VPD_STIN_END) /* End tag descriptor */ | |
352 | return off; | |
353 | ||
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; | |
362 | } | |
363 | } | |
364 | return 0; | |
365 | } | |
366 | ||
1120f8b8 SH |
367 | /* |
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. | |
34e32072 GT |
372 | * |
373 | * Returns 0 on success, negative values indicate error. | |
1120f8b8 | 374 | */ |
f1cd93f9 | 375 | static int pci_vpd_wait(struct pci_dev *dev) |
94e61088 | 376 | { |
408641e9 | 377 | struct pci_vpd *vpd = dev->vpd; |
4f69bd16 | 378 | unsigned long timeout = jiffies + msecs_to_jiffies(125); |
c521b014 | 379 | unsigned long max_sleep = 16; |
1120f8b8 | 380 | u16 status; |
94e61088 BH |
381 | int ret; |
382 | ||
383 | if (!vpd->busy) | |
384 | return 0; | |
385 | ||
c521b014 | 386 | while (time_before(jiffies, timeout)) { |
1120f8b8 | 387 | ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR, |
94e61088 | 388 | &status); |
34e32072 | 389 | if (ret < 0) |
94e61088 | 390 | return ret; |
1120f8b8 SH |
391 | |
392 | if ((status & PCI_VPD_ADDR_F) == vpd->flag) { | |
c5563887 | 393 | vpd->busy = 0; |
94e61088 BH |
394 | return 0; |
395 | } | |
1120f8b8 | 396 | |
1120f8b8 SH |
397 | if (fatal_signal_pending(current)) |
398 | return -EINTR; | |
c521b014 BH |
399 | |
400 | usleep_range(10, max_sleep); | |
401 | if (max_sleep < 1024) | |
402 | max_sleep *= 2; | |
94e61088 | 403 | } |
c521b014 BH |
404 | |
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; | |
94e61088 BH |
407 | } |
408 | ||
f1cd93f9 BH |
409 | static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count, |
410 | void *arg) | |
94e61088 | 411 | { |
408641e9 | 412 | struct pci_vpd *vpd = dev->vpd; |
287d19ce SH |
413 | int ret; |
414 | loff_t end = pos + count; | |
415 | u8 *buf = arg; | |
94e61088 | 416 | |
104daa71 | 417 | if (pos < 0) |
94e61088 | 418 | return -EINVAL; |
94e61088 | 419 | |
104daa71 HR |
420 | if (!vpd->valid) { |
421 | vpd->valid = 1; | |
408641e9 | 422 | vpd->len = pci_vpd_size(dev, vpd->len); |
104daa71 HR |
423 | } |
424 | ||
408641e9 | 425 | if (vpd->len == 0) |
104daa71 HR |
426 | return -EIO; |
427 | ||
408641e9 | 428 | if (pos > vpd->len) |
104daa71 HR |
429 | return 0; |
430 | ||
408641e9 BH |
431 | if (end > vpd->len) { |
432 | end = vpd->len; | |
104daa71 HR |
433 | count = end - pos; |
434 | } | |
435 | ||
1120f8b8 SH |
436 | if (mutex_lock_killable(&vpd->lock)) |
437 | return -EINTR; | |
438 | ||
f1cd93f9 | 439 | ret = pci_vpd_wait(dev); |
94e61088 BH |
440 | if (ret < 0) |
441 | goto out; | |
1120f8b8 | 442 | |
287d19ce SH |
443 | while (pos < end) { |
444 | u32 val; | |
445 | unsigned int i, skip; | |
446 | ||
447 | ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR, | |
448 | pos & ~3); | |
449 | if (ret < 0) | |
450 | break; | |
c5563887 | 451 | vpd->busy = 1; |
287d19ce | 452 | vpd->flag = PCI_VPD_ADDR_F; |
f1cd93f9 | 453 | ret = pci_vpd_wait(dev); |
287d19ce SH |
454 | if (ret < 0) |
455 | break; | |
456 | ||
457 | ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val); | |
458 | if (ret < 0) | |
459 | break; | |
460 | ||
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; | |
467 | } | |
468 | val >>= 8; | |
469 | } | |
470 | } | |
94e61088 | 471 | out: |
1120f8b8 | 472 | mutex_unlock(&vpd->lock); |
287d19ce | 473 | return ret ? ret : count; |
94e61088 BH |
474 | } |
475 | ||
f1cd93f9 BH |
476 | static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count, |
477 | const void *arg) | |
94e61088 | 478 | { |
408641e9 | 479 | struct pci_vpd *vpd = dev->vpd; |
287d19ce SH |
480 | const u8 *buf = arg; |
481 | loff_t end = pos + count; | |
1120f8b8 | 482 | int ret = 0; |
94e61088 | 483 | |
104daa71 HR |
484 | if (pos < 0 || (pos & 3) || (count & 3)) |
485 | return -EINVAL; | |
486 | ||
487 | if (!vpd->valid) { | |
488 | vpd->valid = 1; | |
408641e9 | 489 | vpd->len = pci_vpd_size(dev, vpd->len); |
104daa71 HR |
490 | } |
491 | ||
408641e9 | 492 | if (vpd->len == 0) |
104daa71 HR |
493 | return -EIO; |
494 | ||
408641e9 | 495 | if (end > vpd->len) |
94e61088 BH |
496 | return -EINVAL; |
497 | ||
1120f8b8 SH |
498 | if (mutex_lock_killable(&vpd->lock)) |
499 | return -EINTR; | |
287d19ce | 500 | |
f1cd93f9 | 501 | ret = pci_vpd_wait(dev); |
94e61088 BH |
502 | if (ret < 0) |
503 | goto out; | |
287d19ce SH |
504 | |
505 | while (pos < end) { | |
506 | u32 val; | |
507 | ||
508 | val = *buf++; | |
509 | val |= *buf++ << 8; | |
510 | val |= *buf++ << 16; | |
511 | val |= *buf++ << 24; | |
512 | ||
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; | |
520 | ||
c5563887 | 521 | vpd->busy = 1; |
287d19ce | 522 | vpd->flag = 0; |
f1cd93f9 | 523 | ret = pci_vpd_wait(dev); |
d97ecd81 GT |
524 | if (ret < 0) |
525 | break; | |
287d19ce SH |
526 | |
527 | pos += sizeof(u32); | |
528 | } | |
94e61088 | 529 | out: |
1120f8b8 | 530 | mutex_unlock(&vpd->lock); |
287d19ce | 531 | return ret ? ret : count; |
94e61088 BH |
532 | } |
533 | ||
cb92148b HS |
534 | static int pci_vpd_set_size(struct pci_dev *dev, size_t len) |
535 | { | |
536 | struct pci_vpd *vpd = dev->vpd; | |
537 | ||
538 | if (len == 0 || len > PCI_VPD_MAX_SIZE) | |
539 | return -EIO; | |
540 | ||
541 | vpd->valid = 1; | |
542 | vpd->len = len; | |
543 | ||
544 | return 0; | |
545 | } | |
546 | ||
f1cd93f9 BH |
547 | static const struct pci_vpd_ops pci_vpd_ops = { |
548 | .read = pci_vpd_read, | |
549 | .write = pci_vpd_write, | |
cb92148b | 550 | .set_size = pci_vpd_set_size, |
94e61088 BH |
551 | }; |
552 | ||
932c435c MR |
553 | static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count, |
554 | void *arg) | |
555 | { | |
9d924075 AW |
556 | struct pci_dev *tdev = pci_get_slot(dev->bus, |
557 | PCI_DEVFN(PCI_SLOT(dev->devfn), 0)); | |
932c435c MR |
558 | ssize_t ret; |
559 | ||
560 | if (!tdev) | |
561 | return -ENODEV; | |
562 | ||
563 | ret = pci_read_vpd(tdev, pos, count, arg); | |
564 | pci_dev_put(tdev); | |
565 | return ret; | |
566 | } | |
567 | ||
568 | static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count, | |
569 | const void *arg) | |
570 | { | |
9d924075 AW |
571 | struct pci_dev *tdev = pci_get_slot(dev->bus, |
572 | PCI_DEVFN(PCI_SLOT(dev->devfn), 0)); | |
932c435c MR |
573 | ssize_t ret; |
574 | ||
575 | if (!tdev) | |
576 | return -ENODEV; | |
577 | ||
578 | ret = pci_write_vpd(tdev, pos, count, arg); | |
579 | pci_dev_put(tdev); | |
580 | return ret; | |
581 | } | |
582 | ||
cb92148b HS |
583 | static int pci_vpd_f0_set_size(struct pci_dev *dev, size_t len) |
584 | { | |
585 | struct pci_dev *tdev = pci_get_slot(dev->bus, | |
586 | PCI_DEVFN(PCI_SLOT(dev->devfn), 0)); | |
587 | int ret; | |
588 | ||
589 | if (!tdev) | |
590 | return -ENODEV; | |
591 | ||
592 | ret = pci_set_vpd_size(tdev, len); | |
593 | pci_dev_put(tdev); | |
594 | return ret; | |
595 | } | |
596 | ||
932c435c MR |
597 | static const struct pci_vpd_ops pci_vpd_f0_ops = { |
598 | .read = pci_vpd_f0_read, | |
599 | .write = pci_vpd_f0_write, | |
cb92148b | 600 | .set_size = pci_vpd_f0_set_size, |
932c435c MR |
601 | }; |
602 | ||
f1cd93f9 | 603 | int pci_vpd_init(struct pci_dev *dev) |
94e61088 | 604 | { |
408641e9 | 605 | struct pci_vpd *vpd; |
94e61088 BH |
606 | u8 cap; |
607 | ||
608 | cap = pci_find_capability(dev, PCI_CAP_ID_VPD); | |
609 | if (!cap) | |
610 | return -ENODEV; | |
932c435c | 611 | |
94e61088 BH |
612 | vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC); |
613 | if (!vpd) | |
614 | return -ENOMEM; | |
615 | ||
408641e9 | 616 | vpd->len = PCI_VPD_MAX_SIZE; |
932c435c | 617 | if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) |
408641e9 | 618 | vpd->ops = &pci_vpd_f0_ops; |
932c435c | 619 | else |
408641e9 | 620 | vpd->ops = &pci_vpd_ops; |
1120f8b8 | 621 | mutex_init(&vpd->lock); |
94e61088 | 622 | vpd->cap = cap; |
c5563887 | 623 | vpd->busy = 0; |
104daa71 | 624 | vpd->valid = 0; |
408641e9 | 625 | dev->vpd = vpd; |
94e61088 BH |
626 | return 0; |
627 | } | |
628 | ||
64379079 BH |
629 | void pci_vpd_release(struct pci_dev *dev) |
630 | { | |
408641e9 | 631 | kfree(dev->vpd); |
64379079 BH |
632 | } |
633 | ||
e04b0ea2 | 634 | /** |
fb51ccbf | 635 | * pci_cfg_access_lock - Lock PCI config reads/writes |
e04b0ea2 BK |
636 | * @dev: pci device struct |
637 | * | |
fb51ccbf JK |
638 | * When access is locked, any userspace reads or writes to config |
639 | * space and concurrent lock requests will sleep until access is | |
0b131b13 | 640 | * allowed via pci_cfg_access_unlock() again. |
7ea7e98f | 641 | */ |
fb51ccbf JK |
642 | void pci_cfg_access_lock(struct pci_dev *dev) |
643 | { | |
644 | might_sleep(); | |
645 | ||
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); | |
651 | } | |
652 | EXPORT_SYMBOL_GPL(pci_cfg_access_lock); | |
653 | ||
654 | /** | |
655 | * pci_cfg_access_trylock - try to lock PCI config reads/writes | |
656 | * @dev: pci device struct | |
657 | * | |
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. | |
661 | */ | |
662 | bool pci_cfg_access_trylock(struct pci_dev *dev) | |
e04b0ea2 BK |
663 | { |
664 | unsigned long flags; | |
fb51ccbf | 665 | bool locked = true; |
e04b0ea2 | 666 | |
511dd98c | 667 | raw_spin_lock_irqsave(&pci_lock, flags); |
fb51ccbf JK |
668 | if (dev->block_cfg_access) |
669 | locked = false; | |
670 | else | |
671 | dev->block_cfg_access = 1; | |
511dd98c | 672 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
7ea7e98f | 673 | |
fb51ccbf | 674 | return locked; |
e04b0ea2 | 675 | } |
fb51ccbf | 676 | EXPORT_SYMBOL_GPL(pci_cfg_access_trylock); |
e04b0ea2 BK |
677 | |
678 | /** | |
fb51ccbf | 679 | * pci_cfg_access_unlock - Unlock PCI config reads/writes |
e04b0ea2 BK |
680 | * @dev: pci device struct |
681 | * | |
fb51ccbf | 682 | * This function allows PCI config accesses to resume. |
7ea7e98f | 683 | */ |
fb51ccbf | 684 | void pci_cfg_access_unlock(struct pci_dev *dev) |
e04b0ea2 BK |
685 | { |
686 | unsigned long flags; | |
687 | ||
511dd98c | 688 | raw_spin_lock_irqsave(&pci_lock, flags); |
7ea7e98f MW |
689 | |
690 | /* This indicates a problem in the caller, but we don't need | |
691 | * to kill them, unlike a double-block above. */ | |
fb51ccbf | 692 | WARN_ON(!dev->block_cfg_access); |
7ea7e98f | 693 | |
fb51ccbf | 694 | dev->block_cfg_access = 0; |
511dd98c | 695 | raw_spin_unlock_irqrestore(&pci_lock, flags); |
cdcb33f9 BH |
696 | |
697 | wake_up_all(&pci_cfg_wait); | |
e04b0ea2 | 698 | } |
fb51ccbf | 699 | EXPORT_SYMBOL_GPL(pci_cfg_access_unlock); |
8c0d3a02 JL |
700 | |
701 | static inline int pcie_cap_version(const struct pci_dev *dev) | |
702 | { | |
1c531d82 | 703 | return pcie_caps_reg(dev) & PCI_EXP_FLAGS_VERS; |
8c0d3a02 JL |
704 | } |
705 | ||
ffb4d602 BH |
706 | static bool pcie_downstream_port(const struct pci_dev *dev) |
707 | { | |
708 | int type = pci_pcie_type(dev); | |
709 | ||
710 | return type == PCI_EXP_TYPE_ROOT_PORT || | |
9b70ae49 BH |
711 | type == PCI_EXP_TYPE_DOWNSTREAM || |
712 | type == PCI_EXP_TYPE_PCIE_BRIDGE; | |
ffb4d602 BH |
713 | } |
714 | ||
7a1562d4 | 715 | bool pcie_cap_has_lnkctl(const struct pci_dev *dev) |
8c0d3a02 JL |
716 | { |
717 | int type = pci_pcie_type(dev); | |
718 | ||
c8b303d0 | 719 | return type == PCI_EXP_TYPE_ENDPOINT || |
d3694d4f BH |
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; | |
8c0d3a02 JL |
726 | } |
727 | ||
728 | static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev) | |
729 | { | |
ffb4d602 | 730 | return pcie_downstream_port(dev) && |
6d3a1741 | 731 | pcie_caps_reg(dev) & PCI_EXP_FLAGS_SLOT; |
8c0d3a02 JL |
732 | } |
733 | ||
734 | static inline bool pcie_cap_has_rtctl(const struct pci_dev *dev) | |
735 | { | |
736 | int type = pci_pcie_type(dev); | |
737 | ||
c8b303d0 | 738 | return type == PCI_EXP_TYPE_ROOT_PORT || |
8c0d3a02 JL |
739 | type == PCI_EXP_TYPE_RC_EC; |
740 | } | |
741 | ||
742 | static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos) | |
743 | { | |
744 | if (!pci_is_pcie(dev)) | |
745 | return false; | |
746 | ||
747 | switch (pos) { | |
969daa34 | 748 | case PCI_EXP_FLAGS: |
8c0d3a02 JL |
749 | return true; |
750 | case PCI_EXP_DEVCAP: | |
751 | case PCI_EXP_DEVCTL: | |
752 | case PCI_EXP_DEVSTA: | |
fed24515 | 753 | return true; |
8c0d3a02 JL |
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; | |
774 | } | |
775 | } | |
776 | ||
777 | /* | |
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.) | |
781 | */ | |
782 | int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val) | |
783 | { | |
784 | int ret; | |
785 | ||
786 | *val = 0; | |
787 | if (pos & 1) | |
788 | return -EINVAL; | |
789 | ||
790 | if (pcie_capability_reg_implemented(dev, pos)) { | |
791 | ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val); | |
792 | /* | |
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(). | |
796 | */ | |
797 | if (ret) | |
798 | *val = 0; | |
799 | return ret; | |
800 | } | |
801 | ||
802 | /* | |
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) | |
808 | */ | |
ffb4d602 BH |
809 | if (pci_is_pcie(dev) && pcie_downstream_port(dev) && |
810 | pos == PCI_EXP_SLTSTA) | |
8c0d3a02 | 811 | *val = PCI_EXP_SLTSTA_PDS; |
8c0d3a02 JL |
812 | |
813 | return 0; | |
814 | } | |
815 | EXPORT_SYMBOL(pcie_capability_read_word); | |
816 | ||
817 | int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val) | |
818 | { | |
819 | int ret; | |
820 | ||
821 | *val = 0; | |
822 | if (pos & 3) | |
823 | return -EINVAL; | |
824 | ||
825 | if (pcie_capability_reg_implemented(dev, pos)) { | |
826 | ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val); | |
827 | /* | |
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(). | |
831 | */ | |
832 | if (ret) | |
833 | *val = 0; | |
834 | return ret; | |
835 | } | |
836 | ||
ffb4d602 BH |
837 | if (pci_is_pcie(dev) && pcie_downstream_port(dev) && |
838 | pos == PCI_EXP_SLTSTA) | |
8c0d3a02 | 839 | *val = PCI_EXP_SLTSTA_PDS; |
8c0d3a02 JL |
840 | |
841 | return 0; | |
842 | } | |
843 | EXPORT_SYMBOL(pcie_capability_read_dword); | |
844 | ||
845 | int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val) | |
846 | { | |
847 | if (pos & 1) | |
848 | return -EINVAL; | |
849 | ||
850 | if (!pcie_capability_reg_implemented(dev, pos)) | |
851 | return 0; | |
852 | ||
853 | return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val); | |
854 | } | |
855 | EXPORT_SYMBOL(pcie_capability_write_word); | |
856 | ||
857 | int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val) | |
858 | { | |
859 | if (pos & 3) | |
860 | return -EINVAL; | |
861 | ||
862 | if (!pcie_capability_reg_implemented(dev, pos)) | |
863 | return 0; | |
864 | ||
865 | return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val); | |
866 | } | |
867 | EXPORT_SYMBOL(pcie_capability_write_dword); | |
868 | ||
869 | int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos, | |
870 | u16 clear, u16 set) | |
871 | { | |
872 | int ret; | |
873 | u16 val; | |
874 | ||
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); | |
880 | } | |
881 | ||
882 | return ret; | |
883 | } | |
884 | EXPORT_SYMBOL(pcie_capability_clear_and_set_word); | |
885 | ||
886 | int pcie_capability_clear_and_set_dword(struct pci_dev *dev, int pos, | |
887 | u32 clear, u32 set) | |
888 | { | |
889 | int ret; | |
890 | u32 val; | |
891 | ||
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); | |
897 | } | |
898 | ||
899 | return ret; | |
900 | } | |
901 | EXPORT_SYMBOL(pcie_capability_clear_and_set_dword); | |
d3881e50 KB |
902 | |
903 | int pci_read_config_byte(const struct pci_dev *dev, int where, u8 *val) | |
904 | { | |
4b103883 KB |
905 | if (pci_dev_is_disconnected(dev)) { |
906 | *val = ~0; | |
449e2f9e | 907 | return PCIBIOS_DEVICE_NOT_FOUND; |
4b103883 | 908 | } |
d3881e50 KB |
909 | return pci_bus_read_config_byte(dev->bus, dev->devfn, where, val); |
910 | } | |
911 | EXPORT_SYMBOL(pci_read_config_byte); | |
912 | ||
913 | int pci_read_config_word(const struct pci_dev *dev, int where, u16 *val) | |
914 | { | |
4b103883 KB |
915 | if (pci_dev_is_disconnected(dev)) { |
916 | *val = ~0; | |
449e2f9e | 917 | return PCIBIOS_DEVICE_NOT_FOUND; |
4b103883 | 918 | } |
d3881e50 KB |
919 | return pci_bus_read_config_word(dev->bus, dev->devfn, where, val); |
920 | } | |
921 | EXPORT_SYMBOL(pci_read_config_word); | |
922 | ||
923 | int pci_read_config_dword(const struct pci_dev *dev, int where, | |
924 | u32 *val) | |
925 | { | |
4b103883 KB |
926 | if (pci_dev_is_disconnected(dev)) { |
927 | *val = ~0; | |
449e2f9e | 928 | return PCIBIOS_DEVICE_NOT_FOUND; |
4b103883 | 929 | } |
d3881e50 KB |
930 | return pci_bus_read_config_dword(dev->bus, dev->devfn, where, val); |
931 | } | |
932 | EXPORT_SYMBOL(pci_read_config_dword); | |
933 | ||
934 | int pci_write_config_byte(const struct pci_dev *dev, int where, u8 val) | |
935 | { | |
4b103883 | 936 | if (pci_dev_is_disconnected(dev)) |
449e2f9e | 937 | return PCIBIOS_DEVICE_NOT_FOUND; |
d3881e50 KB |
938 | return pci_bus_write_config_byte(dev->bus, dev->devfn, where, val); |
939 | } | |
940 | EXPORT_SYMBOL(pci_write_config_byte); | |
941 | ||
942 | int pci_write_config_word(const struct pci_dev *dev, int where, u16 val) | |
943 | { | |
4b103883 | 944 | if (pci_dev_is_disconnected(dev)) |
449e2f9e | 945 | return PCIBIOS_DEVICE_NOT_FOUND; |
d3881e50 KB |
946 | return pci_bus_write_config_word(dev->bus, dev->devfn, where, val); |
947 | } | |
948 | EXPORT_SYMBOL(pci_write_config_word); | |
949 | ||
950 | int pci_write_config_dword(const struct pci_dev *dev, int where, | |
951 | u32 val) | |
952 | { | |
4b103883 | 953 | if (pci_dev_is_disconnected(dev)) |
449e2f9e | 954 | return PCIBIOS_DEVICE_NOT_FOUND; |
d3881e50 KB |
955 | return pci_bus_write_config_dword(dev->bus, dev->devfn, where, val); |
956 | } | |
957 | EXPORT_SYMBOL(pci_write_config_dword); |