1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2010 Broadcom Corporation.
9 #include <linux/delay.h>
10 #include <linux/export.h>
11 #include <linux/sched/signal.h>
14 /* VPD access through PCI 2.2+ VPD capability */
17 ssize_t (*read
)(struct pci_dev
*dev
, loff_t pos
, size_t count
, void *buf
);
18 ssize_t (*write
)(struct pci_dev
*dev
, loff_t pos
, size_t count
, const void *buf
);
19 int (*set_size
)(struct pci_dev
*dev
, size_t len
);
23 const struct pci_vpd_ops
*ops
;
24 struct bin_attribute
*attr
; /* Descriptor for sysfs VPD entry */
34 * pci_read_vpd - Read one entry from Vital Product Data
35 * @dev: pci device struct
36 * @pos: offset in vpd space
37 * @count: number of bytes to read
38 * @buf: pointer to where to store result
40 ssize_t
pci_read_vpd(struct pci_dev
*dev
, loff_t pos
, size_t count
, void *buf
)
42 if (!dev
->vpd
|| !dev
->vpd
->ops
)
44 return dev
->vpd
->ops
->read(dev
, pos
, count
, buf
);
46 EXPORT_SYMBOL(pci_read_vpd
);
49 * pci_write_vpd - Write entry to Vital Product Data
50 * @dev: pci device struct
51 * @pos: offset in vpd space
52 * @count: number of bytes to write
53 * @buf: buffer containing write data
55 ssize_t
pci_write_vpd(struct pci_dev
*dev
, loff_t pos
, size_t count
, const void *buf
)
57 if (!dev
->vpd
|| !dev
->vpd
->ops
)
59 return dev
->vpd
->ops
->write(dev
, pos
, count
, buf
);
61 EXPORT_SYMBOL(pci_write_vpd
);
64 * pci_set_vpd_size - Set size of Vital Product Data space
65 * @dev: pci device struct
66 * @len: size of vpd space
68 int pci_set_vpd_size(struct pci_dev
*dev
, size_t len
)
70 if (!dev
->vpd
|| !dev
->vpd
->ops
)
72 return dev
->vpd
->ops
->set_size(dev
, len
);
74 EXPORT_SYMBOL(pci_set_vpd_size
);
76 #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
79 * pci_vpd_size - determine actual size of Vital Product Data
80 * @dev: pci device struct
81 * @old_size: current assumed size, also maximum allowed size
83 static size_t pci_vpd_size(struct pci_dev
*dev
, size_t old_size
)
86 unsigned char header
[1+2]; /* 1 byte tag, 2 bytes length */
88 while (off
< old_size
&&
89 pci_read_vpd(dev
, off
, 1, header
) == 1) {
92 if (header
[0] & PCI_VPD_LRDT
) {
93 /* Large Resource Data Type Tag */
94 tag
= pci_vpd_lrdt_tag(header
);
95 /* Only read length from known tag items */
96 if ((tag
== PCI_VPD_LTIN_ID_STRING
) ||
97 (tag
== PCI_VPD_LTIN_RO_DATA
) ||
98 (tag
== PCI_VPD_LTIN_RW_DATA
)) {
99 if (pci_read_vpd(dev
, off
+1, 2,
101 pci_warn(dev
, "invalid large VPD tag %02x size at offset %zu",
105 off
+= PCI_VPD_LRDT_TAG_SIZE
+
106 pci_vpd_lrdt_size(header
);
109 /* Short Resource Data Type Tag */
110 off
+= PCI_VPD_SRDT_TAG_SIZE
+
111 pci_vpd_srdt_size(header
);
112 tag
= pci_vpd_srdt_tag(header
);
115 if (tag
== PCI_VPD_STIN_END
) /* End tag descriptor */
118 if ((tag
!= PCI_VPD_LTIN_ID_STRING
) &&
119 (tag
!= PCI_VPD_LTIN_RO_DATA
) &&
120 (tag
!= PCI_VPD_LTIN_RW_DATA
)) {
121 pci_warn(dev
, "invalid %s VPD tag %02x at offset %zu",
122 (header
[0] & PCI_VPD_LRDT
) ? "large" : "short",
131 * Wait for last operation to complete.
132 * This code has to spin since there is no other notification from the PCI
133 * hardware. Since the VPD is often implemented by serial attachment to an
134 * EEPROM, it may take many milliseconds to complete.
136 * Returns 0 on success, negative values indicate error.
138 static int pci_vpd_wait(struct pci_dev
*dev
)
140 struct pci_vpd
*vpd
= dev
->vpd
;
141 unsigned long timeout
= jiffies
+ msecs_to_jiffies(125);
142 unsigned long max_sleep
= 16;
150 ret
= pci_user_read_config_word(dev
, vpd
->cap
+ PCI_VPD_ADDR
,
155 if ((status
& PCI_VPD_ADDR_F
) == vpd
->flag
) {
160 if (fatal_signal_pending(current
))
163 if (time_after(jiffies
, timeout
))
166 usleep_range(10, max_sleep
);
167 if (max_sleep
< 1024)
171 pci_warn(dev
, "VPD access failed. This is likely a firmware bug on this device. Contact the card vendor for a firmware update\n");
175 static ssize_t
pci_vpd_read(struct pci_dev
*dev
, loff_t pos
, size_t count
,
178 struct pci_vpd
*vpd
= dev
->vpd
;
180 loff_t end
= pos
+ count
;
188 vpd
->len
= pci_vpd_size(dev
, vpd
->len
);
197 if (end
> vpd
->len
) {
202 if (mutex_lock_killable(&vpd
->lock
))
205 ret
= pci_vpd_wait(dev
);
211 unsigned int i
, skip
;
213 ret
= pci_user_write_config_word(dev
, vpd
->cap
+ PCI_VPD_ADDR
,
218 vpd
->flag
= PCI_VPD_ADDR_F
;
219 ret
= pci_vpd_wait(dev
);
223 ret
= pci_user_read_config_dword(dev
, vpd
->cap
+ PCI_VPD_DATA
, &val
);
228 for (i
= 0; i
< sizeof(u32
); i
++) {
238 mutex_unlock(&vpd
->lock
);
239 return ret
? ret
: count
;
242 static ssize_t
pci_vpd_write(struct pci_dev
*dev
, loff_t pos
, size_t count
,
245 struct pci_vpd
*vpd
= dev
->vpd
;
247 loff_t end
= pos
+ count
;
250 if (pos
< 0 || (pos
& 3) || (count
& 3))
255 vpd
->len
= pci_vpd_size(dev
, vpd
->len
);
264 if (mutex_lock_killable(&vpd
->lock
))
267 ret
= pci_vpd_wait(dev
);
279 ret
= pci_user_write_config_dword(dev
, vpd
->cap
+ PCI_VPD_DATA
, val
);
282 ret
= pci_user_write_config_word(dev
, vpd
->cap
+ PCI_VPD_ADDR
,
283 pos
| PCI_VPD_ADDR_F
);
289 ret
= pci_vpd_wait(dev
);
296 mutex_unlock(&vpd
->lock
);
297 return ret
? ret
: count
;
300 static int pci_vpd_set_size(struct pci_dev
*dev
, size_t len
)
302 struct pci_vpd
*vpd
= dev
->vpd
;
304 if (len
== 0 || len
> PCI_VPD_MAX_SIZE
)
313 static const struct pci_vpd_ops pci_vpd_ops
= {
314 .read
= pci_vpd_read
,
315 .write
= pci_vpd_write
,
316 .set_size
= pci_vpd_set_size
,
319 static ssize_t
pci_vpd_f0_read(struct pci_dev
*dev
, loff_t pos
, size_t count
,
322 struct pci_dev
*tdev
= pci_get_slot(dev
->bus
,
323 PCI_DEVFN(PCI_SLOT(dev
->devfn
), 0));
329 ret
= pci_read_vpd(tdev
, pos
, count
, arg
);
334 static ssize_t
pci_vpd_f0_write(struct pci_dev
*dev
, loff_t pos
, size_t count
,
337 struct pci_dev
*tdev
= pci_get_slot(dev
->bus
,
338 PCI_DEVFN(PCI_SLOT(dev
->devfn
), 0));
344 ret
= pci_write_vpd(tdev
, pos
, count
, arg
);
349 static int pci_vpd_f0_set_size(struct pci_dev
*dev
, size_t len
)
351 struct pci_dev
*tdev
= pci_get_slot(dev
->bus
,
352 PCI_DEVFN(PCI_SLOT(dev
->devfn
), 0));
358 ret
= pci_set_vpd_size(tdev
, len
);
363 static const struct pci_vpd_ops pci_vpd_f0_ops
= {
364 .read
= pci_vpd_f0_read
,
365 .write
= pci_vpd_f0_write
,
366 .set_size
= pci_vpd_f0_set_size
,
369 int pci_vpd_init(struct pci_dev
*dev
)
374 cap
= pci_find_capability(dev
, PCI_CAP_ID_VPD
);
378 vpd
= kzalloc(sizeof(*vpd
), GFP_ATOMIC
);
382 vpd
->len
= PCI_VPD_MAX_SIZE
;
383 if (dev
->dev_flags
& PCI_DEV_FLAGS_VPD_REF_F0
)
384 vpd
->ops
= &pci_vpd_f0_ops
;
386 vpd
->ops
= &pci_vpd_ops
;
387 mutex_init(&vpd
->lock
);
395 void pci_vpd_release(struct pci_dev
*dev
)
400 static ssize_t
read_vpd_attr(struct file
*filp
, struct kobject
*kobj
,
401 struct bin_attribute
*bin_attr
, char *buf
,
402 loff_t off
, size_t count
)
404 struct pci_dev
*dev
= to_pci_dev(kobj_to_dev(kobj
));
406 if (bin_attr
->size
> 0) {
407 if (off
> bin_attr
->size
)
409 else if (count
> bin_attr
->size
- off
)
410 count
= bin_attr
->size
- off
;
413 return pci_read_vpd(dev
, off
, count
, buf
);
416 static ssize_t
write_vpd_attr(struct file
*filp
, struct kobject
*kobj
,
417 struct bin_attribute
*bin_attr
, char *buf
,
418 loff_t off
, size_t count
)
420 struct pci_dev
*dev
= to_pci_dev(kobj_to_dev(kobj
));
422 if (bin_attr
->size
> 0) {
423 if (off
> bin_attr
->size
)
425 else if (count
> bin_attr
->size
- off
)
426 count
= bin_attr
->size
- off
;
429 return pci_write_vpd(dev
, off
, count
, buf
);
432 void pcie_vpd_create_sysfs_dev_files(struct pci_dev
*dev
)
435 struct bin_attribute
*attr
;
440 attr
= kzalloc(sizeof(*attr
), GFP_ATOMIC
);
444 sysfs_bin_attr_init(attr
);
446 attr
->attr
.name
= "vpd";
447 attr
->attr
.mode
= S_IRUSR
| S_IWUSR
;
448 attr
->read
= read_vpd_attr
;
449 attr
->write
= write_vpd_attr
;
450 retval
= sysfs_create_bin_file(&dev
->dev
.kobj
, attr
);
456 dev
->vpd
->attr
= attr
;
459 void pcie_vpd_remove_sysfs_dev_files(struct pci_dev
*dev
)
461 if (dev
->vpd
&& dev
->vpd
->attr
) {
462 sysfs_remove_bin_file(&dev
->dev
.kobj
, dev
->vpd
->attr
);
463 kfree(dev
->vpd
->attr
);
467 int pci_vpd_find_tag(const u8
*buf
, unsigned int off
, unsigned int len
, u8 rdt
)
471 for (i
= off
; i
< len
; ) {
474 if (val
& PCI_VPD_LRDT
) {
475 /* Don't return success of the tag isn't complete */
476 if (i
+ PCI_VPD_LRDT_TAG_SIZE
> len
)
482 i
+= PCI_VPD_LRDT_TAG_SIZE
+
483 pci_vpd_lrdt_size(&buf
[i
]);
485 u8 tag
= val
& ~PCI_VPD_SRDT_LEN_MASK
;
490 if (tag
== PCI_VPD_SRDT_END
)
493 i
+= PCI_VPD_SRDT_TAG_SIZE
+
494 pci_vpd_srdt_size(&buf
[i
]);
500 EXPORT_SYMBOL_GPL(pci_vpd_find_tag
);
502 int pci_vpd_find_info_keyword(const u8
*buf
, unsigned int off
,
503 unsigned int len
, const char *kw
)
507 for (i
= off
; i
+ PCI_VPD_INFO_FLD_HDR_SIZE
<= off
+ len
;) {
508 if (buf
[i
+ 0] == kw
[0] &&
512 i
+= PCI_VPD_INFO_FLD_HDR_SIZE
+
513 pci_vpd_info_field_size(&buf
[i
]);
518 EXPORT_SYMBOL_GPL(pci_vpd_find_info_keyword
);
520 #ifdef CONFIG_PCI_QUIRKS
522 * Quirk non-zero PCI functions to route VPD access through function 0 for
523 * devices that share VPD resources between functions. The functions are
524 * expected to be identical devices.
526 static void quirk_f0_vpd_link(struct pci_dev
*dev
)
530 if (!PCI_FUNC(dev
->devfn
))
533 f0
= pci_get_slot(dev
->bus
, PCI_DEVFN(PCI_SLOT(dev
->devfn
), 0));
537 if (f0
->vpd
&& dev
->class == f0
->class &&
538 dev
->vendor
== f0
->vendor
&& dev
->device
== f0
->device
)
539 dev
->dev_flags
|= PCI_DEV_FLAGS_VPD_REF_F0
;
543 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL
, PCI_ANY_ID
,
544 PCI_CLASS_NETWORK_ETHERNET
, 8, quirk_f0_vpd_link
);
547 * If a device follows the VPD format spec, the PCI core will not read or
548 * write past the VPD End Tag. But some vendors do not follow the VPD
549 * format spec, so we can't tell how much data is safe to access. Devices
550 * may behave unpredictably if we access too much. Blacklist these devices
551 * so we don't touch VPD at all.
553 static void quirk_blacklist_vpd(struct pci_dev
*dev
)
557 pci_warn(dev
, FW_BUG
"disabling VPD access (can't determine size of non-standard VPD format)\n");
560 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x0060, quirk_blacklist_vpd
);
561 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x007c, quirk_blacklist_vpd
);
562 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x0413, quirk_blacklist_vpd
);
563 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x0078, quirk_blacklist_vpd
);
564 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x0079, quirk_blacklist_vpd
);
565 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x0073, quirk_blacklist_vpd
);
566 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x0071, quirk_blacklist_vpd
);
567 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x005b, quirk_blacklist_vpd
);
568 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x002f, quirk_blacklist_vpd
);
569 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x005d, quirk_blacklist_vpd
);
570 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC
, 0x005f, quirk_blacklist_vpd
);
571 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC
, PCI_ANY_ID
,
572 quirk_blacklist_vpd
);
573 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_QLOGIC
, 0x2261, quirk_blacklist_vpd
);
576 * For Broadcom 5706, 5708, 5709 rev. A nics, any read beyond the
577 * VPD end tag will hang the device. This problem was initially
578 * observed when a vpd entry was created in sysfs
579 * ('/sys/bus/pci/devices/<id>/vpd'). A read to this sysfs entry
580 * will dump 32k of data. Reading a full 32k will cause an access
581 * beyond the VPD end tag causing the device to hang. Once the device
582 * is hung, the bnx2 driver will not be able to reset the device.
583 * We believe that it is legal to read beyond the end tag and
584 * therefore the solution is to limit the read/write length.
586 static void quirk_brcm_570x_limit_vpd(struct pci_dev
*dev
)
589 * Only disable the VPD capability for 5706, 5706S, 5708,
590 * 5708S and 5709 rev. A
592 if ((dev
->device
== PCI_DEVICE_ID_NX2_5706
) ||
593 (dev
->device
== PCI_DEVICE_ID_NX2_5706S
) ||
594 (dev
->device
== PCI_DEVICE_ID_NX2_5708
) ||
595 (dev
->device
== PCI_DEVICE_ID_NX2_5708S
) ||
596 ((dev
->device
== PCI_DEVICE_ID_NX2_5709
) &&
597 (dev
->revision
& 0xf0) == 0x0)) {
599 dev
->vpd
->len
= 0x80;
602 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM
,
603 PCI_DEVICE_ID_NX2_5706
,
604 quirk_brcm_570x_limit_vpd
);
605 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM
,
606 PCI_DEVICE_ID_NX2_5706S
,
607 quirk_brcm_570x_limit_vpd
);
608 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM
,
609 PCI_DEVICE_ID_NX2_5708
,
610 quirk_brcm_570x_limit_vpd
);
611 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM
,
612 PCI_DEVICE_ID_NX2_5708S
,
613 quirk_brcm_570x_limit_vpd
);
614 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM
,
615 PCI_DEVICE_ID_NX2_5709
,
616 quirk_brcm_570x_limit_vpd
);
617 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM
,
618 PCI_DEVICE_ID_NX2_5709S
,
619 quirk_brcm_570x_limit_vpd
);
621 static void quirk_chelsio_extend_vpd(struct pci_dev
*dev
)
623 int chip
= (dev
->device
& 0xf000) >> 12;
624 int func
= (dev
->device
& 0x0f00) >> 8;
625 int prod
= (dev
->device
& 0x00ff) >> 0;
628 * If this is a T3-based adapter, there's a 1KB VPD area at offset
629 * 0xc00 which contains the preferred VPD values. If this is a T4 or
630 * later based adapter, the special VPD is at offset 0x400 for the
631 * Physical Functions (the SR-IOV Virtual Functions have no VPD
632 * Capabilities). The PCI VPD Access core routines will normally
633 * compute the size of the VPD by parsing the VPD Data Structure at
634 * offset 0x000. This will result in silent failures when attempting
635 * to accesses these other VPD areas which are beyond those computed
638 if (chip
== 0x0 && prod
>= 0x20)
639 pci_set_vpd_size(dev
, 8192);
640 else if (chip
>= 0x4 && func
< 0x8)
641 pci_set_vpd_size(dev
, 2048);
644 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO
, PCI_ANY_ID
,
645 quirk_chelsio_extend_vpd
);