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>
12 #include <asm/unaligned.h>
15 #define PCI_VPD_LRDT_TAG_SIZE 3
16 #define PCI_VPD_SRDT_LEN_MASK 0x07
17 #define PCI_VPD_SRDT_TAG_SIZE 1
18 #define PCI_VPD_STIN_END 0x0f
19 #define PCI_VPD_INFO_FLD_HDR_SIZE 3
21 static u16 pci_vpd_lrdt_size(const u8 *lrdt)
23 return get_unaligned_le16(lrdt + 1);
26 static u8 pci_vpd_srdt_tag(const u8 *srdt)
31 static u8 pci_vpd_srdt_size(const u8 *srdt)
33 return *srdt & PCI_VPD_SRDT_LEN_MASK;
36 static u8 pci_vpd_info_field_size(const u8 *info_field)
41 /* VPD access through PCI 2.2+ VPD capability */
43 static struct pci_dev *pci_get_func0_dev(struct pci_dev *dev)
45 return pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
48 #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
49 #define PCI_VPD_SZ_INVALID UINT_MAX
52 * pci_vpd_size - determine actual size of Vital Product Data
53 * @dev: pci device struct
55 static size_t pci_vpd_size(struct pci_dev *dev)
58 unsigned char tag, header[1+2]; /* 1 byte tag, 2 bytes length */
60 /* Otherwise the following reads would fail. */
61 dev->vpd.len = PCI_VPD_MAX_SIZE;
63 while (pci_read_vpd(dev, off, 1, header) == 1) {
66 if (off == 0 && (header[0] == 0x00 || header[0] == 0xff))
69 if (header[0] & PCI_VPD_LRDT) {
70 /* Large Resource Data Type Tag */
71 if (pci_read_vpd(dev, off + 1, 2, &header[1]) != 2) {
72 pci_warn(dev, "failed VPD read at offset %zu\n",
74 return off ?: PCI_VPD_SZ_INVALID;
76 size = pci_vpd_lrdt_size(header);
77 if (off + size > PCI_VPD_MAX_SIZE)
80 off += PCI_VPD_LRDT_TAG_SIZE + size;
82 /* Short Resource Data Type Tag */
83 tag = pci_vpd_srdt_tag(header);
84 size = pci_vpd_srdt_size(header);
85 if (off + size > PCI_VPD_MAX_SIZE)
88 off += PCI_VPD_SRDT_TAG_SIZE + size;
89 if (tag == PCI_VPD_STIN_END) /* End tag descriptor */
96 pci_info(dev, "invalid VPD tag %#04x (size %zu) at offset %zu%s\n",
97 header[0], size, off, off == 0 ?
98 "; assume missing optional EEPROM" : "");
99 return off ?: PCI_VPD_SZ_INVALID;
102 static bool pci_vpd_available(struct pci_dev *dev)
104 struct pci_vpd *vpd = &dev->vpd;
110 vpd->len = pci_vpd_size(dev);
111 if (vpd->len == PCI_VPD_SZ_INVALID) {
121 * Wait for last operation to complete.
122 * This code has to spin since there is no other notification from the PCI
123 * hardware. Since the VPD is often implemented by serial attachment to an
124 * EEPROM, it may take many milliseconds to complete.
125 * @set: if true wait for flag to be set, else wait for it to be cleared
127 * Returns 0 on success, negative values indicate error.
129 static int pci_vpd_wait(struct pci_dev *dev, bool set)
131 struct pci_vpd *vpd = &dev->vpd;
132 unsigned long timeout = jiffies + msecs_to_jiffies(125);
133 unsigned long max_sleep = 16;
138 ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
143 if (!!(status & PCI_VPD_ADDR_F) == set)
146 if (time_after(jiffies, timeout))
149 usleep_range(10, max_sleep);
150 if (max_sleep < 1024)
154 pci_warn(dev, "VPD access failed. This is likely a firmware bug on this device. Contact the card vendor for a firmware update\n");
158 static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
161 struct pci_vpd *vpd = &dev->vpd;
163 loff_t end = pos + count;
166 if (!pci_vpd_available(dev))
175 if (end > vpd->len) {
180 if (mutex_lock_killable(&vpd->lock))
185 unsigned int i, skip;
187 if (fatal_signal_pending(current)) {
192 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
196 ret = pci_vpd_wait(dev, true);
200 ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
205 for (i = 0; i < sizeof(u32); i++) {
215 mutex_unlock(&vpd->lock);
216 return ret ? ret : count;
219 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
222 struct pci_vpd *vpd = &dev->vpd;
224 loff_t end = pos + count;
227 if (!pci_vpd_available(dev))
230 if (pos < 0 || (pos & 3) || (count & 3))
236 if (mutex_lock_killable(&vpd->lock))
240 ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA,
241 get_unaligned_le32(buf));
244 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
245 pos | PCI_VPD_ADDR_F);
249 ret = pci_vpd_wait(dev, false);
257 mutex_unlock(&vpd->lock);
258 return ret ? ret : count;
261 void pci_vpd_init(struct pci_dev *dev)
263 if (dev->vpd.len == PCI_VPD_SZ_INVALID)
266 dev->vpd.cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
267 mutex_init(&dev->vpd.lock);
270 static ssize_t vpd_read(struct file *filp, struct kobject *kobj,
271 struct bin_attribute *bin_attr, char *buf, loff_t off,
274 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
276 return pci_read_vpd(dev, off, count, buf);
279 static ssize_t vpd_write(struct file *filp, struct kobject *kobj,
280 struct bin_attribute *bin_attr, char *buf, loff_t off,
283 struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
285 return pci_write_vpd(dev, off, count, buf);
287 static BIN_ATTR(vpd, 0600, vpd_read, vpd_write, 0);
289 static struct bin_attribute *vpd_attrs[] = {
294 static umode_t vpd_attr_is_visible(struct kobject *kobj,
295 struct bin_attribute *a, int n)
297 struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
305 const struct attribute_group pci_dev_vpd_attr_group = {
306 .bin_attrs = vpd_attrs,
307 .is_bin_visible = vpd_attr_is_visible,
310 void *pci_vpd_alloc(struct pci_dev *dev, unsigned int *size)
316 if (!pci_vpd_available(dev))
317 return ERR_PTR(-ENODEV);
320 buf = kmalloc(len, GFP_KERNEL);
322 return ERR_PTR(-ENOMEM);
324 cnt = pci_read_vpd(dev, 0, len, buf);
327 return ERR_PTR(-EIO);
335 EXPORT_SYMBOL_GPL(pci_vpd_alloc);
337 static int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt, unsigned int *size)
341 /* look for LRDT tags only, end tag is the only SRDT tag */
342 while (i + PCI_VPD_LRDT_TAG_SIZE <= len && buf[i] & PCI_VPD_LRDT) {
343 unsigned int lrdt_len = pci_vpd_lrdt_size(buf + i);
346 i += PCI_VPD_LRDT_TAG_SIZE;
348 if (i + lrdt_len > len)
361 int pci_vpd_find_id_string(const u8 *buf, unsigned int len, unsigned int *size)
363 return pci_vpd_find_tag(buf, len, PCI_VPD_LRDT_ID_STRING, size);
365 EXPORT_SYMBOL_GPL(pci_vpd_find_id_string);
367 static int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
368 unsigned int len, const char *kw)
372 for (i = off; i + PCI_VPD_INFO_FLD_HDR_SIZE <= off + len;) {
373 if (buf[i + 0] == kw[0] &&
377 i += PCI_VPD_INFO_FLD_HDR_SIZE +
378 pci_vpd_info_field_size(&buf[i]);
385 * pci_read_vpd - Read one entry from Vital Product Data
386 * @dev: PCI device struct
387 * @pos: offset in VPD space
388 * @count: number of bytes to read
389 * @buf: pointer to where to store result
391 ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
395 if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) {
396 dev = pci_get_func0_dev(dev);
400 ret = pci_vpd_read(dev, pos, count, buf);
405 return pci_vpd_read(dev, pos, count, buf);
407 EXPORT_SYMBOL(pci_read_vpd);
410 * pci_write_vpd - Write entry to Vital Product Data
411 * @dev: PCI device struct
412 * @pos: offset in VPD space
413 * @count: number of bytes to write
414 * @buf: buffer containing write data
416 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
420 if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) {
421 dev = pci_get_func0_dev(dev);
425 ret = pci_vpd_write(dev, pos, count, buf);
430 return pci_vpd_write(dev, pos, count, buf);
432 EXPORT_SYMBOL(pci_write_vpd);
434 int pci_vpd_find_ro_info_keyword(const void *buf, unsigned int len,
435 const char *kw, unsigned int *size)
437 int ro_start, infokw_start;
438 unsigned int ro_len, infokw_size;
440 ro_start = pci_vpd_find_tag(buf, len, PCI_VPD_LRDT_RO_DATA, &ro_len);
444 infokw_start = pci_vpd_find_info_keyword(buf, ro_start, ro_len, kw);
445 if (infokw_start < 0)
448 infokw_size = pci_vpd_info_field_size(buf + infokw_start);
449 infokw_start += PCI_VPD_INFO_FLD_HDR_SIZE;
451 if (infokw_start + infokw_size > len)
459 EXPORT_SYMBOL_GPL(pci_vpd_find_ro_info_keyword);
461 int pci_vpd_check_csum(const void *buf, unsigned int len)
468 rv_start = pci_vpd_find_ro_info_keyword(buf, len, PCI_VPD_RO_KEYWORD_CHKSUM, &size);
469 if (rv_start == -ENOENT) /* no checksum in VPD */
471 else if (rv_start < 0)
477 while (rv_start >= 0)
478 csum += vpd[rv_start--];
480 return csum ? -EILSEQ : 0;
482 EXPORT_SYMBOL_GPL(pci_vpd_check_csum);
484 #ifdef CONFIG_PCI_QUIRKS
486 * Quirk non-zero PCI functions to route VPD access through function 0 for
487 * devices that share VPD resources between functions. The functions are
488 * expected to be identical devices.
490 static void quirk_f0_vpd_link(struct pci_dev *dev)
494 if (!PCI_FUNC(dev->devfn))
497 f0 = pci_get_func0_dev(dev);
501 if (f0->vpd.cap && dev->class == f0->class &&
502 dev->vendor == f0->vendor && dev->device == f0->device)
503 dev->dev_flags |= PCI_DEV_FLAGS_VPD_REF_F0;
507 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
508 PCI_CLASS_NETWORK_ETHERNET, 8, quirk_f0_vpd_link);
511 * If a device follows the VPD format spec, the PCI core will not read or
512 * write past the VPD End Tag. But some vendors do not follow the VPD
513 * format spec, so we can't tell how much data is safe to access. Devices
514 * may behave unpredictably if we access too much. Blacklist these devices
515 * so we don't touch VPD at all.
517 static void quirk_blacklist_vpd(struct pci_dev *dev)
519 dev->vpd.len = PCI_VPD_SZ_INVALID;
520 pci_warn(dev, FW_BUG "disabling VPD access (can't determine size of non-standard VPD format)\n");
522 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x0060, quirk_blacklist_vpd);
523 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x007c, quirk_blacklist_vpd);
524 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x0413, quirk_blacklist_vpd);
525 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x0078, quirk_blacklist_vpd);
526 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x0079, quirk_blacklist_vpd);
527 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x0073, quirk_blacklist_vpd);
528 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x0071, quirk_blacklist_vpd);
529 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x005b, quirk_blacklist_vpd);
530 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x002f, quirk_blacklist_vpd);
531 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x005d, quirk_blacklist_vpd);
532 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_LSI_LOGIC, 0x005f, quirk_blacklist_vpd);
533 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ATTANSIC, PCI_ANY_ID, quirk_blacklist_vpd);
535 * The Amazon Annapurna Labs 0x0031 device id is reused for other non Root Port
536 * device types, so the quirk is registered for the PCI_CLASS_BRIDGE_PCI class.
538 DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_VENDOR_ID_AMAZON_ANNAPURNA_LABS, 0x0031,
539 PCI_CLASS_BRIDGE_PCI, 8, quirk_blacklist_vpd);
541 static void quirk_chelsio_extend_vpd(struct pci_dev *dev)
543 int chip = (dev->device & 0xf000) >> 12;
544 int func = (dev->device & 0x0f00) >> 8;
545 int prod = (dev->device & 0x00ff) >> 0;
548 * If this is a T3-based adapter, there's a 1KB VPD area at offset
549 * 0xc00 which contains the preferred VPD values. If this is a T4 or
550 * later based adapter, the special VPD is at offset 0x400 for the
551 * Physical Functions (the SR-IOV Virtual Functions have no VPD
552 * Capabilities). The PCI VPD Access core routines will normally
553 * compute the size of the VPD by parsing the VPD Data Structure at
554 * offset 0x000. This will result in silent failures when attempting
555 * to accesses these other VPD areas which are beyond those computed
558 if (chip == 0x0 && prod >= 0x20)
560 else if (chip >= 0x4 && func < 0x8)
564 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_CHELSIO, PCI_ANY_ID,
565 quirk_chelsio_extend_vpd);