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 * pci_read_vpd - Read one entry from Vital Product Data
19 * @dev: pci device struct
20 * @pos: offset in vpd space
21 * @count: number of bytes to read
22 * @buf: pointer to where to store result
24 ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
26 if (!dev->vpd || !dev->vpd->ops)
28 return dev->vpd->ops->read(dev, pos, count, buf);
30 EXPORT_SYMBOL(pci_read_vpd);
33 * pci_write_vpd - Write entry to Vital Product Data
34 * @dev: pci device struct
35 * @pos: offset in vpd space
36 * @count: number of bytes to write
37 * @buf: buffer containing write data
39 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
41 if (!dev->vpd || !dev->vpd->ops)
43 return dev->vpd->ops->write(dev, pos, count, buf);
45 EXPORT_SYMBOL(pci_write_vpd);
48 * pci_set_vpd_size - Set size of Vital Product Data space
49 * @dev: pci device struct
50 * @len: size of vpd space
52 int pci_set_vpd_size(struct pci_dev *dev, size_t len)
54 if (!dev->vpd || !dev->vpd->ops)
56 return dev->vpd->ops->set_size(dev, len);
58 EXPORT_SYMBOL(pci_set_vpd_size);
60 #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
63 * pci_vpd_size - determine actual size of Vital Product Data
64 * @dev: pci device struct
65 * @old_size: current assumed size, also maximum allowed size
67 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
70 unsigned char header[1+2]; /* 1 byte tag, 2 bytes length */
72 while (off < old_size &&
73 pci_read_vpd(dev, off, 1, header) == 1) {
76 if (header[0] & PCI_VPD_LRDT) {
77 /* Large Resource Data Type Tag */
78 tag = pci_vpd_lrdt_tag(header);
79 /* Only read length from known tag items */
80 if ((tag == PCI_VPD_LTIN_ID_STRING) ||
81 (tag == PCI_VPD_LTIN_RO_DATA) ||
82 (tag == PCI_VPD_LTIN_RW_DATA)) {
83 if (pci_read_vpd(dev, off+1, 2,
85 pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
89 off += PCI_VPD_LRDT_TAG_SIZE +
90 pci_vpd_lrdt_size(header);
93 /* Short Resource Data Type Tag */
94 off += PCI_VPD_SRDT_TAG_SIZE +
95 pci_vpd_srdt_size(header);
96 tag = pci_vpd_srdt_tag(header);
99 if (tag == PCI_VPD_STIN_END) /* End tag descriptor */
102 if ((tag != PCI_VPD_LTIN_ID_STRING) &&
103 (tag != PCI_VPD_LTIN_RO_DATA) &&
104 (tag != PCI_VPD_LTIN_RW_DATA)) {
105 pci_warn(dev, "invalid %s VPD tag %02x at offset %zu",
106 (header[0] & PCI_VPD_LRDT) ? "large" : "short",
115 * Wait for last operation to complete.
116 * This code has to spin since there is no other notification from the PCI
117 * hardware. Since the VPD is often implemented by serial attachment to an
118 * EEPROM, it may take many milliseconds to complete.
120 * Returns 0 on success, negative values indicate error.
122 static int pci_vpd_wait(struct pci_dev *dev)
124 struct pci_vpd *vpd = dev->vpd;
125 unsigned long timeout = jiffies + msecs_to_jiffies(125);
126 unsigned long max_sleep = 16;
133 while (time_before(jiffies, timeout)) {
134 ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
139 if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
144 if (fatal_signal_pending(current))
147 usleep_range(10, max_sleep);
148 if (max_sleep < 1024)
152 pci_warn(dev, "VPD access failed. This is likely a firmware bug on this device. Contact the card vendor for a firmware update\n");
156 static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
159 struct pci_vpd *vpd = dev->vpd;
161 loff_t end = pos + count;
169 vpd->len = pci_vpd_size(dev, vpd->len);
178 if (end > vpd->len) {
183 if (mutex_lock_killable(&vpd->lock))
186 ret = pci_vpd_wait(dev);
192 unsigned int i, skip;
194 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
199 vpd->flag = PCI_VPD_ADDR_F;
200 ret = pci_vpd_wait(dev);
204 ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
209 for (i = 0; i < sizeof(u32); i++) {
219 mutex_unlock(&vpd->lock);
220 return ret ? ret : count;
223 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
226 struct pci_vpd *vpd = dev->vpd;
228 loff_t end = pos + count;
231 if (pos < 0 || (pos & 3) || (count & 3))
236 vpd->len = pci_vpd_size(dev, vpd->len);
245 if (mutex_lock_killable(&vpd->lock))
248 ret = pci_vpd_wait(dev);
260 ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
263 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
264 pos | PCI_VPD_ADDR_F);
270 ret = pci_vpd_wait(dev);
277 mutex_unlock(&vpd->lock);
278 return ret ? ret : count;
281 static int pci_vpd_set_size(struct pci_dev *dev, size_t len)
283 struct pci_vpd *vpd = dev->vpd;
285 if (len == 0 || len > PCI_VPD_MAX_SIZE)
294 static const struct pci_vpd_ops pci_vpd_ops = {
295 .read = pci_vpd_read,
296 .write = pci_vpd_write,
297 .set_size = pci_vpd_set_size,
300 static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
303 struct pci_dev *tdev = pci_get_slot(dev->bus,
304 PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
310 ret = pci_read_vpd(tdev, pos, count, arg);
315 static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
318 struct pci_dev *tdev = pci_get_slot(dev->bus,
319 PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
325 ret = pci_write_vpd(tdev, pos, count, arg);
330 static int pci_vpd_f0_set_size(struct pci_dev *dev, size_t len)
332 struct pci_dev *tdev = pci_get_slot(dev->bus,
333 PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
339 ret = pci_set_vpd_size(tdev, len);
344 static const struct pci_vpd_ops pci_vpd_f0_ops = {
345 .read = pci_vpd_f0_read,
346 .write = pci_vpd_f0_write,
347 .set_size = pci_vpd_f0_set_size,
350 int pci_vpd_init(struct pci_dev *dev)
355 cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
359 vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
363 vpd->len = PCI_VPD_MAX_SIZE;
364 if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0)
365 vpd->ops = &pci_vpd_f0_ops;
367 vpd->ops = &pci_vpd_ops;
368 mutex_init(&vpd->lock);
376 void pci_vpd_release(struct pci_dev *dev)
381 int pci_vpd_find_tag(const u8 *buf, unsigned int off, unsigned int len, u8 rdt)
385 for (i = off; i < len; ) {
388 if (val & PCI_VPD_LRDT) {
389 /* Don't return success of the tag isn't complete */
390 if (i + PCI_VPD_LRDT_TAG_SIZE > len)
396 i += PCI_VPD_LRDT_TAG_SIZE +
397 pci_vpd_lrdt_size(&buf[i]);
399 u8 tag = val & ~PCI_VPD_SRDT_LEN_MASK;
404 if (tag == PCI_VPD_SRDT_END)
407 i += PCI_VPD_SRDT_TAG_SIZE +
408 pci_vpd_srdt_size(&buf[i]);
414 EXPORT_SYMBOL_GPL(pci_vpd_find_tag);
416 int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
417 unsigned int len, const char *kw)
421 for (i = off; i + PCI_VPD_INFO_FLD_HDR_SIZE <= off + len;) {
422 if (buf[i + 0] == kw[0] &&
426 i += PCI_VPD_INFO_FLD_HDR_SIZE +
427 pci_vpd_info_field_size(&buf[i]);
432 EXPORT_SYMBOL_GPL(pci_vpd_find_info_keyword);