1 // SPDX-License-Identifier: GPL-2.0
4 * Purpose: Provide PCI VPD support
6 * Copyright (C) 2010 Broadcom Corporation.
10 #include <linux/delay.h>
11 #include <linux/export.h>
12 #include <linux/sched/signal.h>
15 /* VPD access through PCI 2.2+ VPD capability */
18 ssize_t (*read)(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
19 ssize_t (*write)(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
20 int (*set_size)(struct pci_dev *dev, size_t len);
24 const struct pci_vpd_ops *ops;
25 struct bin_attribute *attr; /* Descriptor for sysfs VPD entry */
35 * pci_read_vpd - Read one entry from Vital Product Data
36 * @dev: pci device struct
37 * @pos: offset in vpd space
38 * @count: number of bytes to read
39 * @buf: pointer to where to store result
41 ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
43 if (!dev->vpd || !dev->vpd->ops)
45 return dev->vpd->ops->read(dev, pos, count, buf);
47 EXPORT_SYMBOL(pci_read_vpd);
50 * pci_write_vpd - Write entry to Vital Product Data
51 * @dev: pci device struct
52 * @pos: offset in vpd space
53 * @count: number of bytes to write
54 * @buf: buffer containing write data
56 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
58 if (!dev->vpd || !dev->vpd->ops)
60 return dev->vpd->ops->write(dev, pos, count, buf);
62 EXPORT_SYMBOL(pci_write_vpd);
65 * pci_set_vpd_size - Set size of Vital Product Data space
66 * @dev: pci device struct
67 * @len: size of vpd space
69 int pci_set_vpd_size(struct pci_dev *dev, size_t len)
71 if (!dev->vpd || !dev->vpd->ops)
73 return dev->vpd->ops->set_size(dev, len);
75 EXPORT_SYMBOL(pci_set_vpd_size);
77 #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
80 * pci_vpd_size - determine actual size of Vital Product Data
81 * @dev: pci device struct
82 * @old_size: current assumed size, also maximum allowed size
84 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
87 unsigned char header[1+2]; /* 1 byte tag, 2 bytes length */
89 while (off < old_size &&
90 pci_read_vpd(dev, off, 1, header) == 1) {
93 if (header[0] & PCI_VPD_LRDT) {
94 /* Large Resource Data Type Tag */
95 tag = pci_vpd_lrdt_tag(header);
96 /* Only read length from known tag items */
97 if ((tag == PCI_VPD_LTIN_ID_STRING) ||
98 (tag == PCI_VPD_LTIN_RO_DATA) ||
99 (tag == PCI_VPD_LTIN_RW_DATA)) {
100 if (pci_read_vpd(dev, off+1, 2,
102 pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
106 off += PCI_VPD_LRDT_TAG_SIZE +
107 pci_vpd_lrdt_size(header);
110 /* Short Resource Data Type Tag */
111 off += PCI_VPD_SRDT_TAG_SIZE +
112 pci_vpd_srdt_size(header);
113 tag = pci_vpd_srdt_tag(header);
116 if (tag == PCI_VPD_STIN_END) /* End tag descriptor */
119 if ((tag != PCI_VPD_LTIN_ID_STRING) &&
120 (tag != PCI_VPD_LTIN_RO_DATA) &&
121 (tag != PCI_VPD_LTIN_RW_DATA)) {
122 pci_warn(dev, "invalid %s VPD tag %02x at offset %zu",
123 (header[0] & PCI_VPD_LRDT) ? "large" : "short",
132 * Wait for last operation to complete.
133 * This code has to spin since there is no other notification from the PCI
134 * hardware. Since the VPD is often implemented by serial attachment to an
135 * EEPROM, it may take many milliseconds to complete.
137 * Returns 0 on success, negative values indicate error.
139 static int pci_vpd_wait(struct pci_dev *dev)
141 struct pci_vpd *vpd = dev->vpd;
142 unsigned long timeout = jiffies + msecs_to_jiffies(125);
143 unsigned long max_sleep = 16;
150 while (time_before(jiffies, timeout)) {
151 ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
156 if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
161 if (fatal_signal_pending(current))
164 usleep_range(10, max_sleep);
165 if (max_sleep < 1024)
169 pci_warn(dev, "VPD access failed. This is likely a firmware bug on this device. Contact the card vendor for a firmware update\n");
173 static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
176 struct pci_vpd *vpd = dev->vpd;
178 loff_t end = pos + count;
186 vpd->len = pci_vpd_size(dev, vpd->len);
195 if (end > vpd->len) {
200 if (mutex_lock_killable(&vpd->lock))
203 ret = pci_vpd_wait(dev);
209 unsigned int i, skip;
211 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
216 vpd->flag = PCI_VPD_ADDR_F;
217 ret = pci_vpd_wait(dev);
221 ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
226 for (i = 0; i < sizeof(u32); i++) {
236 mutex_unlock(&vpd->lock);
237 return ret ? ret : count;
240 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
243 struct pci_vpd *vpd = dev->vpd;
245 loff_t end = pos + count;
248 if (pos < 0 || (pos & 3) || (count & 3))
253 vpd->len = pci_vpd_size(dev, vpd->len);
262 if (mutex_lock_killable(&vpd->lock))
265 ret = pci_vpd_wait(dev);
277 ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
280 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
281 pos | PCI_VPD_ADDR_F);
287 ret = pci_vpd_wait(dev);
294 mutex_unlock(&vpd->lock);
295 return ret ? ret : count;
298 static int pci_vpd_set_size(struct pci_dev *dev, size_t len)
300 struct pci_vpd *vpd = dev->vpd;
302 if (len == 0 || len > PCI_VPD_MAX_SIZE)
311 static const struct pci_vpd_ops pci_vpd_ops = {
312 .read = pci_vpd_read,
313 .write = pci_vpd_write,
314 .set_size = pci_vpd_set_size,
317 static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
320 struct pci_dev *tdev = pci_get_slot(dev->bus,
321 PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
327 ret = pci_read_vpd(tdev, pos, count, arg);
332 static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
335 struct pci_dev *tdev = pci_get_slot(dev->bus,
336 PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
342 ret = pci_write_vpd(tdev, pos, count, arg);
347 static int pci_vpd_f0_set_size(struct pci_dev *dev, size_t len)
349 struct pci_dev *tdev = pci_get_slot(dev->bus,
350 PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
356 ret = pci_set_vpd_size(tdev, len);
361 static const struct pci_vpd_ops pci_vpd_f0_ops = {
362 .read = pci_vpd_f0_read,
363 .write = pci_vpd_f0_write,
364 .set_size = pci_vpd_f0_set_size,
367 int pci_vpd_init(struct pci_dev *dev)
372 cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
376 vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
380 vpd->len = PCI_VPD_MAX_SIZE;
381 if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0)
382 vpd->ops = &pci_vpd_f0_ops;
384 vpd->ops = &pci_vpd_ops;
385 mutex_init(&vpd->lock);
393 void pci_vpd_release(struct pci_dev *dev)
398 static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj,
399 struct bin_attribute *bin_attr, char *buf,
400 loff_t off, size_t count)
402 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
404 if (bin_attr->size > 0) {
405 if (off > bin_attr->size)
407 else if (count > bin_attr->size - off)
408 count = bin_attr->size - off;
411 return pci_read_vpd(dev, off, count, buf);
414 static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj,
415 struct bin_attribute *bin_attr, char *buf,
416 loff_t off, size_t count)
418 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
420 if (bin_attr->size > 0) {
421 if (off > bin_attr->size)
423 else if (count > bin_attr->size - off)
424 count = bin_attr->size - off;
427 return pci_write_vpd(dev, off, count, buf);
430 void pcie_vpd_create_sysfs_dev_files(struct pci_dev *dev)
433 struct bin_attribute *attr;
438 attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
442 sysfs_bin_attr_init(attr);
444 attr->attr.name = "vpd";
445 attr->attr.mode = S_IRUSR | S_IWUSR;
446 attr->read = read_vpd_attr;
447 attr->write = write_vpd_attr;
448 retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
454 dev->vpd->attr = attr;
457 void pcie_vpd_remove_sysfs_dev_files(struct pci_dev *dev)
459 if (dev->vpd && dev->vpd->attr) {
460 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
461 kfree(dev->vpd->attr);
465 int pci_vpd_find_tag(const u8 *buf, unsigned int off, unsigned int len, u8 rdt)
469 for (i = off; i < len; ) {
472 if (val & PCI_VPD_LRDT) {
473 /* Don't return success of the tag isn't complete */
474 if (i + PCI_VPD_LRDT_TAG_SIZE > len)
480 i += PCI_VPD_LRDT_TAG_SIZE +
481 pci_vpd_lrdt_size(&buf[i]);
483 u8 tag = val & ~PCI_VPD_SRDT_LEN_MASK;
488 if (tag == PCI_VPD_SRDT_END)
491 i += PCI_VPD_SRDT_TAG_SIZE +
492 pci_vpd_srdt_size(&buf[i]);
498 EXPORT_SYMBOL_GPL(pci_vpd_find_tag);
500 int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
501 unsigned int len, const char *kw)
505 for (i = off; i + PCI_VPD_INFO_FLD_HDR_SIZE <= off + len;) {
506 if (buf[i + 0] == kw[0] &&
510 i += PCI_VPD_INFO_FLD_HDR_SIZE +
511 pci_vpd_info_field_size(&buf[i]);
516 EXPORT_SYMBOL_GPL(pci_vpd_find_info_keyword);
518 #ifdef CONFIG_PCI_QUIRKS
520 * Quirk non-zero PCI functions to route VPD access through function 0 for
521 * devices that share VPD resources between functions. The functions are
522 * expected to be identical devices.
524 static void quirk_f0_vpd_link(struct pci_dev *dev)
528 if (!PCI_FUNC(dev->devfn))
531 f0 = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
535 if (f0->vpd && dev->class == f0->class &&
536 dev->vendor == f0->vendor && dev->device == f0->device)
537 dev->dev_flags |= PCI_DEV_FLAGS_VPD_REF_F0;
541 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
542 PCI_CLASS_NETWORK_ETHERNET, 8, quirk_f0_vpd_link);
545 * If a device follows the VPD format spec, the PCI core will not read or
546 * write past the VPD End Tag. But some vendors do not follow the VPD
547 * format spec, so we can't tell how much data is safe to access. Devices
548 * may behave unpredictably if we access too much. Blacklist these devices
549 * so we don't touch VPD at all.
551 static void quirk_blacklist_vpd(struct pci_dev *dev)
555 pci_warn(dev, FW_BUG "disabling VPD access (can't determine size of non-standard VPD format)\n");
558 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0060, quirk_blacklist_vpd);
559 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x007c, quirk_blacklist_vpd);
560 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0413, quirk_blacklist_vpd);
561 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0078, quirk_blacklist_vpd);
562 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0079, quirk_blacklist_vpd);
563 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0073, quirk_blacklist_vpd);
564 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0071, quirk_blacklist_vpd);
565 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005b, quirk_blacklist_vpd);
566 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x002f, quirk_blacklist_vpd);
567 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005d, quirk_blacklist_vpd);
568 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005f, quirk_blacklist_vpd);
569 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, PCI_ANY_ID,
570 quirk_blacklist_vpd);
571 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_QLOGIC, 0x2261, quirk_blacklist_vpd);
574 * For Broadcom 5706, 5708, 5709 rev. A nics, any read beyond the
575 * VPD end tag will hang the device. This problem was initially
576 * observed when a vpd entry was created in sysfs
577 * ('/sys/bus/pci/devices/<id>/vpd'). A read to this sysfs entry
578 * will dump 32k of data. Reading a full 32k will cause an access
579 * beyond the VPD end tag causing the device to hang. Once the device
580 * is hung, the bnx2 driver will not be able to reset the device.
581 * We believe that it is legal to read beyond the end tag and
582 * therefore the solution is to limit the read/write length.
584 static void quirk_brcm_570x_limit_vpd(struct pci_dev *dev)
587 * Only disable the VPD capability for 5706, 5706S, 5708,
588 * 5708S and 5709 rev. A
590 if ((dev->device == PCI_DEVICE_ID_NX2_5706) ||
591 (dev->device == PCI_DEVICE_ID_NX2_5706S) ||
592 (dev->device == PCI_DEVICE_ID_NX2_5708) ||
593 (dev->device == PCI_DEVICE_ID_NX2_5708S) ||
594 ((dev->device == PCI_DEVICE_ID_NX2_5709) &&
595 (dev->revision & 0xf0) == 0x0)) {
597 dev->vpd->len = 0x80;
600 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
601 PCI_DEVICE_ID_NX2_5706,
602 quirk_brcm_570x_limit_vpd);
603 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
604 PCI_DEVICE_ID_NX2_5706S,
605 quirk_brcm_570x_limit_vpd);
606 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
607 PCI_DEVICE_ID_NX2_5708,
608 quirk_brcm_570x_limit_vpd);
609 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
610 PCI_DEVICE_ID_NX2_5708S,
611 quirk_brcm_570x_limit_vpd);
612 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
613 PCI_DEVICE_ID_NX2_5709,
614 quirk_brcm_570x_limit_vpd);
615 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
616 PCI_DEVICE_ID_NX2_5709S,
617 quirk_brcm_570x_limit_vpd);
619 static void quirk_chelsio_extend_vpd(struct pci_dev *dev)
621 pci_set_vpd_size(dev, 8192);
623 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x20, quirk_chelsio_extend_vpd);
624 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x21, quirk_chelsio_extend_vpd);
625 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x22, quirk_chelsio_extend_vpd);
626 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x23, quirk_chelsio_extend_vpd);
627 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x24, quirk_chelsio_extend_vpd);
628 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x25, quirk_chelsio_extend_vpd);
629 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x26, quirk_chelsio_extend_vpd);
630 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x30, quirk_chelsio_extend_vpd);
631 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x31, quirk_chelsio_extend_vpd);
632 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x32, quirk_chelsio_extend_vpd);
633 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x35, quirk_chelsio_extend_vpd);
634 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x36, quirk_chelsio_extend_vpd);
635 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, 0x37, quirk_chelsio_extend_vpd);