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);
22 const struct pci_vpd_ops *ops;
31 static struct pci_dev *pci_get_func0_dev(struct pci_dev *dev)
33 return pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
37 * pci_read_vpd - Read one entry from Vital Product Data
38 * @dev: pci device struct
39 * @pos: offset in vpd space
40 * @count: number of bytes to read
41 * @buf: pointer to where to store result
43 ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
45 if (!dev->vpd || !dev->vpd->ops)
47 return dev->vpd->ops->read(dev, pos, count, buf);
49 EXPORT_SYMBOL(pci_read_vpd);
52 * pci_write_vpd - Write entry to Vital Product Data
53 * @dev: pci device struct
54 * @pos: offset in vpd space
55 * @count: number of bytes to write
56 * @buf: buffer containing write data
58 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
60 if (!dev->vpd || !dev->vpd->ops)
62 return dev->vpd->ops->write(dev, pos, count, buf);
64 EXPORT_SYMBOL(pci_write_vpd);
66 #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
69 * pci_vpd_size - determine actual size of Vital Product Data
70 * @dev: pci device struct
71 * @old_size: current assumed size, also maximum allowed size
73 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
76 unsigned char tag, header[1+2]; /* 1 byte tag, 2 bytes length */
78 while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
81 if (off == 0 && (header[0] == 0x00 || header[0] == 0xff))
84 if (header[0] & PCI_VPD_LRDT) {
85 /* Large Resource Data Type Tag */
86 if (pci_read_vpd(dev, off + 1, 2, &header[1]) != 2) {
87 pci_warn(dev, "failed VPD read at offset %zu\n",
91 size = pci_vpd_lrdt_size(header);
92 if (off + size > PCI_VPD_MAX_SIZE)
95 off += PCI_VPD_LRDT_TAG_SIZE + size;
97 /* Short Resource Data Type Tag */
98 tag = pci_vpd_srdt_tag(header);
99 size = pci_vpd_srdt_size(header);
100 if (off + size > PCI_VPD_MAX_SIZE)
103 off += PCI_VPD_SRDT_TAG_SIZE + size;
104 if (tag == PCI_VPD_STIN_END) /* End tag descriptor */
111 pci_info(dev, "invalid VPD tag %#04x (size %zu) at offset %zu%s\n",
112 header[0], size, off, off == 0 ?
113 "; assume missing optional EEPROM" : "");
118 * Wait for last operation to complete.
119 * This code has to spin since there is no other notification from the PCI
120 * hardware. Since the VPD is often implemented by serial attachment to an
121 * EEPROM, it may take many milliseconds to complete.
123 * Returns 0 on success, negative values indicate error.
125 static int pci_vpd_wait(struct pci_dev *dev)
127 struct pci_vpd *vpd = dev->vpd;
128 unsigned long timeout = jiffies + msecs_to_jiffies(125);
129 unsigned long max_sleep = 16;
137 ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
142 if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
147 if (fatal_signal_pending(current))
150 if (time_after(jiffies, timeout))
153 usleep_range(10, max_sleep);
154 if (max_sleep < 1024)
158 pci_warn(dev, "VPD access failed. This is likely a firmware bug on this device. Contact the card vendor for a firmware update\n");
162 static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
165 struct pci_vpd *vpd = dev->vpd;
167 loff_t end = pos + count;
175 vpd->len = pci_vpd_size(dev, vpd->len);
184 if (end > vpd->len) {
189 if (mutex_lock_killable(&vpd->lock))
192 ret = pci_vpd_wait(dev);
198 unsigned int i, skip;
200 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
205 vpd->flag = PCI_VPD_ADDR_F;
206 ret = pci_vpd_wait(dev);
210 ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
215 for (i = 0; i < sizeof(u32); i++) {
225 mutex_unlock(&vpd->lock);
226 return ret ? ret : count;
229 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
232 struct pci_vpd *vpd = dev->vpd;
234 loff_t end = pos + count;
237 if (pos < 0 || (pos & 3) || (count & 3))
242 vpd->len = pci_vpd_size(dev, vpd->len);
251 if (mutex_lock_killable(&vpd->lock))
254 ret = pci_vpd_wait(dev);
266 ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
269 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
270 pos | PCI_VPD_ADDR_F);
276 ret = pci_vpd_wait(dev);
283 mutex_unlock(&vpd->lock);
284 return ret ? ret : count;
287 static const struct pci_vpd_ops pci_vpd_ops = {
288 .read = pci_vpd_read,
289 .write = pci_vpd_write,
292 static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
295 struct pci_dev *tdev = pci_get_func0_dev(dev);
301 ret = pci_read_vpd(tdev, pos, count, arg);
306 static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
309 struct pci_dev *tdev = pci_get_func0_dev(dev);
315 ret = pci_write_vpd(tdev, pos, count, arg);
320 static const struct pci_vpd_ops pci_vpd_f0_ops = {
321 .read = pci_vpd_f0_read,
322 .write = pci_vpd_f0_write,
325 void pci_vpd_init(struct pci_dev *dev)
330 cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
334 vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
338 vpd->len = PCI_VPD_MAX_SIZE;
339 if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0)
340 vpd->ops = &pci_vpd_f0_ops;
342 vpd->ops = &pci_vpd_ops;
343 mutex_init(&vpd->lock);
350 void pci_vpd_release(struct pci_dev *dev)
355 static ssize_t vpd_read(struct file *filp, struct kobject *kobj,
356 struct bin_attribute *bin_attr, char *buf, loff_t off,
359 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
361 return pci_read_vpd(dev, off, count, buf);
364 static ssize_t vpd_write(struct file *filp, struct kobject *kobj,
365 struct bin_attribute *bin_attr, char *buf, loff_t off,
368 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
370 return pci_write_vpd(dev, off, count, buf);
372 static BIN_ATTR(vpd, 0600, vpd_read, vpd_write, 0);
374 static struct bin_attribute *vpd_attrs[] = {
379 static umode_t vpd_attr_is_visible(struct kobject *kobj,
380 struct bin_attribute *a, int n)
382 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
390 const struct attribute_group pci_dev_vpd_attr_group = {
391 .bin_attrs = vpd_attrs,
392 .is_bin_visible = vpd_attr_is_visible,
395 int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt)
399 /* look for LRDT tags only, end tag is the only SRDT tag */
400 while (i + PCI_VPD_LRDT_TAG_SIZE <= len && buf[i] & PCI_VPD_LRDT) {
404 i += PCI_VPD_LRDT_TAG_SIZE + pci_vpd_lrdt_size(buf + i);
409 EXPORT_SYMBOL_GPL(pci_vpd_find_tag);
411 int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
412 unsigned int len, const char *kw)
416 for (i = off; i + PCI_VPD_INFO_FLD_HDR_SIZE <= off + len;) {
417 if (buf[i + 0] == kw[0] &&
421 i += PCI_VPD_INFO_FLD_HDR_SIZE +
422 pci_vpd_info_field_size(&buf[i]);
427 EXPORT_SYMBOL_GPL(pci_vpd_find_info_keyword);
429 #ifdef CONFIG_PCI_QUIRKS
431 * Quirk non-zero PCI functions to route VPD access through function 0 for
432 * devices that share VPD resources between functions. The functions are
433 * expected to be identical devices.
435 static void quirk_f0_vpd_link(struct pci_dev *dev)
439 if (!PCI_FUNC(dev->devfn))
442 f0 = pci_get_func0_dev(dev);
446 if (f0->vpd && dev->class == f0->class &&
447 dev->vendor == f0->vendor && dev->device == f0->device)
448 dev->dev_flags |= PCI_DEV_FLAGS_VPD_REF_F0;
452 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
453 PCI_CLASS_NETWORK_ETHERNET, 8, quirk_f0_vpd_link);
456 * If a device follows the VPD format spec, the PCI core will not read or
457 * write past the VPD End Tag. But some vendors do not follow the VPD
458 * format spec, so we can't tell how much data is safe to access. Devices
459 * may behave unpredictably if we access too much. Blacklist these devices
460 * so we don't touch VPD at all.
462 static void quirk_blacklist_vpd(struct pci_dev *dev)
466 pci_warn(dev, FW_BUG "disabling VPD access (can't determine size of non-standard VPD format)\n");
469 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0060, quirk_blacklist_vpd);
470 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x007c, quirk_blacklist_vpd);
471 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0413, quirk_blacklist_vpd);
472 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0078, quirk_blacklist_vpd);
473 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0079, quirk_blacklist_vpd);
474 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0073, quirk_blacklist_vpd);
475 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0071, quirk_blacklist_vpd);
476 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005b, quirk_blacklist_vpd);
477 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x002f, quirk_blacklist_vpd);
478 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005d, quirk_blacklist_vpd);
479 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005f, quirk_blacklist_vpd);
480 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, PCI_ANY_ID,
481 quirk_blacklist_vpd);
483 * The Amazon Annapurna Labs 0x0031 device id is reused for other non Root Port
484 * device types, so the quirk is registered for the PCI_CLASS_BRIDGE_PCI class.
486 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_AMAZON_ANNAPURNA_LABS, 0x0031,
487 PCI_CLASS_BRIDGE_PCI, 8, quirk_blacklist_vpd);
489 static void pci_vpd_set_size(struct pci_dev *dev, size_t len)
491 struct pci_vpd *vpd = dev->vpd;
493 if (!vpd || len == 0 || len > PCI_VPD_MAX_SIZE)
500 static void quirk_chelsio_extend_vpd(struct pci_dev *dev)
502 int chip = (dev->device & 0xf000) >> 12;
503 int func = (dev->device & 0x0f00) >> 8;
504 int prod = (dev->device & 0x00ff) >> 0;
507 * If this is a T3-based adapter, there's a 1KB VPD area at offset
508 * 0xc00 which contains the preferred VPD values. If this is a T4 or
509 * later based adapter, the special VPD is at offset 0x400 for the
510 * Physical Functions (the SR-IOV Virtual Functions have no VPD
511 * Capabilities). The PCI VPD Access core routines will normally
512 * compute the size of the VPD by parsing the VPD Data Structure at
513 * offset 0x000. This will result in silent failures when attempting
514 * to accesses these other VPD areas which are beyond those computed
517 if (chip == 0x0 && prod >= 0x20)
518 pci_vpd_set_size(dev, 8192);
519 else if (chip >= 0x4 && func < 0x8)
520 pci_vpd_set_size(dev, 2048);
523 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, PCI_ANY_ID,
524 quirk_chelsio_extend_vpd);