PCI/VPD: Make missing VPD message less alarming
[linux-2.6-microblaze.git] / drivers / pci / vpd.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCI VPD support
4  *
5  * Copyright (C) 2010 Broadcom Corporation.
6  */
7
8 #include <linux/pci.h>
9 #include <linux/delay.h>
10 #include <linux/export.h>
11 #include <linux/sched/signal.h>
12 #include "pci.h"
13
14 /* VPD access through PCI 2.2+ VPD capability */
15
16 struct pci_vpd_ops {
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 };
20
21 struct pci_vpd {
22         const struct pci_vpd_ops *ops;
23         struct bin_attribute *attr;     /* Descriptor for sysfs VPD entry */
24         struct mutex    lock;
25         unsigned int    len;
26         u16             flag;
27         u8              cap;
28         unsigned int    busy:1;
29         unsigned int    valid:1;
30 };
31
32 /**
33  * pci_read_vpd - Read one entry from Vital Product Data
34  * @dev:        pci device struct
35  * @pos:        offset in vpd space
36  * @count:      number of bytes to read
37  * @buf:        pointer to where to store result
38  */
39 ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
40 {
41         if (!dev->vpd || !dev->vpd->ops)
42                 return -ENODEV;
43         return dev->vpd->ops->read(dev, pos, count, buf);
44 }
45 EXPORT_SYMBOL(pci_read_vpd);
46
47 /**
48  * pci_write_vpd - Write entry to Vital Product Data
49  * @dev:        pci device struct
50  * @pos:        offset in vpd space
51  * @count:      number of bytes to write
52  * @buf:        buffer containing write data
53  */
54 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
55 {
56         if (!dev->vpd || !dev->vpd->ops)
57                 return -ENODEV;
58         return dev->vpd->ops->write(dev, pos, count, buf);
59 }
60 EXPORT_SYMBOL(pci_write_vpd);
61
62 #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
63
64 /**
65  * pci_vpd_size - determine actual size of Vital Product Data
66  * @dev:        pci device struct
67  * @old_size:   current assumed size, also maximum allowed size
68  */
69 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
70 {
71         size_t off = 0;
72         unsigned char header[1+2];      /* 1 byte tag, 2 bytes length */
73
74         while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
75                 unsigned char tag;
76
77                 if (!header[0] && !off) {
78                         pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
79                         return 0;
80                 }
81
82                 if (header[0] & PCI_VPD_LRDT) {
83                         /* Large Resource Data Type Tag */
84                         tag = pci_vpd_lrdt_tag(header);
85                         /* Only read length from known tag items */
86                         if ((tag == PCI_VPD_LTIN_ID_STRING) ||
87                             (tag == PCI_VPD_LTIN_RO_DATA) ||
88                             (tag == PCI_VPD_LTIN_RW_DATA)) {
89                                 if (pci_read_vpd(dev, off+1, 2,
90                                                  &header[1]) != 2) {
91                                         pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
92                                                  tag, off + 1);
93                                         return 0;
94                                 }
95                                 off += PCI_VPD_LRDT_TAG_SIZE +
96                                         pci_vpd_lrdt_size(header);
97                         }
98                 } else {
99                         /* Short Resource Data Type Tag */
100                         off += PCI_VPD_SRDT_TAG_SIZE +
101                                 pci_vpd_srdt_size(header);
102                         tag = pci_vpd_srdt_tag(header);
103                 }
104
105                 if (tag == PCI_VPD_STIN_END)    /* End tag descriptor */
106                         return off;
107
108                 if ((tag != PCI_VPD_LTIN_ID_STRING) &&
109                     (tag != PCI_VPD_LTIN_RO_DATA) &&
110                     (tag != PCI_VPD_LTIN_RW_DATA)) {
111                         pci_warn(dev, "invalid %s VPD tag %02x at offset %zu",
112                                  (header[0] & PCI_VPD_LRDT) ? "large" : "short",
113                                  tag, off);
114                         return 0;
115                 }
116         }
117         return 0;
118 }
119
120 /*
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  *
126  * Returns 0 on success, negative values indicate error.
127  */
128 static int pci_vpd_wait(struct pci_dev *dev)
129 {
130         struct pci_vpd *vpd = dev->vpd;
131         unsigned long timeout = jiffies + msecs_to_jiffies(125);
132         unsigned long max_sleep = 16;
133         u16 status;
134         int ret;
135
136         if (!vpd->busy)
137                 return 0;
138
139         do {
140                 ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
141                                                 &status);
142                 if (ret < 0)
143                         return ret;
144
145                 if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
146                         vpd->busy = 0;
147                         return 0;
148                 }
149
150                 if (fatal_signal_pending(current))
151                         return -EINTR;
152
153                 if (time_after(jiffies, timeout))
154                         break;
155
156                 usleep_range(10, max_sleep);
157                 if (max_sleep < 1024)
158                         max_sleep *= 2;
159         } while (true);
160
161         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         return -ETIMEDOUT;
163 }
164
165 static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
166                             void *arg)
167 {
168         struct pci_vpd *vpd = dev->vpd;
169         int ret;
170         loff_t end = pos + count;
171         u8 *buf = arg;
172
173         if (pos < 0)
174                 return -EINVAL;
175
176         if (!vpd->valid) {
177                 vpd->valid = 1;
178                 vpd->len = pci_vpd_size(dev, vpd->len);
179         }
180
181         if (vpd->len == 0)
182                 return -EIO;
183
184         if (pos > vpd->len)
185                 return 0;
186
187         if (end > vpd->len) {
188                 end = vpd->len;
189                 count = end - pos;
190         }
191
192         if (mutex_lock_killable(&vpd->lock))
193                 return -EINTR;
194
195         ret = pci_vpd_wait(dev);
196         if (ret < 0)
197                 goto out;
198
199         while (pos < end) {
200                 u32 val;
201                 unsigned int i, skip;
202
203                 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
204                                                  pos & ~3);
205                 if (ret < 0)
206                         break;
207                 vpd->busy = 1;
208                 vpd->flag = PCI_VPD_ADDR_F;
209                 ret = pci_vpd_wait(dev);
210                 if (ret < 0)
211                         break;
212
213                 ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
214                 if (ret < 0)
215                         break;
216
217                 skip = pos & 3;
218                 for (i = 0;  i < sizeof(u32); i++) {
219                         if (i >= skip) {
220                                 *buf++ = val;
221                                 if (++pos == end)
222                                         break;
223                         }
224                         val >>= 8;
225                 }
226         }
227 out:
228         mutex_unlock(&vpd->lock);
229         return ret ? ret : count;
230 }
231
232 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
233                              const void *arg)
234 {
235         struct pci_vpd *vpd = dev->vpd;
236         const u8 *buf = arg;
237         loff_t end = pos + count;
238         int ret = 0;
239
240         if (pos < 0 || (pos & 3) || (count & 3))
241                 return -EINVAL;
242
243         if (!vpd->valid) {
244                 vpd->valid = 1;
245                 vpd->len = pci_vpd_size(dev, vpd->len);
246         }
247
248         if (vpd->len == 0)
249                 return -EIO;
250
251         if (end > vpd->len)
252                 return -EINVAL;
253
254         if (mutex_lock_killable(&vpd->lock))
255                 return -EINTR;
256
257         ret = pci_vpd_wait(dev);
258         if (ret < 0)
259                 goto out;
260
261         while (pos < end) {
262                 u32 val;
263
264                 val = *buf++;
265                 val |= *buf++ << 8;
266                 val |= *buf++ << 16;
267                 val |= *buf++ << 24;
268
269                 ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
270                 if (ret < 0)
271                         break;
272                 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
273                                                  pos | PCI_VPD_ADDR_F);
274                 if (ret < 0)
275                         break;
276
277                 vpd->busy = 1;
278                 vpd->flag = 0;
279                 ret = pci_vpd_wait(dev);
280                 if (ret < 0)
281                         break;
282
283                 pos += sizeof(u32);
284         }
285 out:
286         mutex_unlock(&vpd->lock);
287         return ret ? ret : count;
288 }
289
290 static const struct pci_vpd_ops pci_vpd_ops = {
291         .read = pci_vpd_read,
292         .write = pci_vpd_write,
293 };
294
295 static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
296                                void *arg)
297 {
298         struct pci_dev *tdev = pci_get_slot(dev->bus,
299                                             PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
300         ssize_t ret;
301
302         if (!tdev)
303                 return -ENODEV;
304
305         ret = pci_read_vpd(tdev, pos, count, arg);
306         pci_dev_put(tdev);
307         return ret;
308 }
309
310 static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
311                                 const void *arg)
312 {
313         struct pci_dev *tdev = pci_get_slot(dev->bus,
314                                             PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
315         ssize_t ret;
316
317         if (!tdev)
318                 return -ENODEV;
319
320         ret = pci_write_vpd(tdev, pos, count, arg);
321         pci_dev_put(tdev);
322         return ret;
323 }
324
325 static const struct pci_vpd_ops pci_vpd_f0_ops = {
326         .read = pci_vpd_f0_read,
327         .write = pci_vpd_f0_write,
328 };
329
330 int pci_vpd_init(struct pci_dev *dev)
331 {
332         struct pci_vpd *vpd;
333         u8 cap;
334
335         cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
336         if (!cap)
337                 return -ENODEV;
338
339         vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
340         if (!vpd)
341                 return -ENOMEM;
342
343         vpd->len = PCI_VPD_MAX_SIZE;
344         if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0)
345                 vpd->ops = &pci_vpd_f0_ops;
346         else
347                 vpd->ops = &pci_vpd_ops;
348         mutex_init(&vpd->lock);
349         vpd->cap = cap;
350         vpd->busy = 0;
351         vpd->valid = 0;
352         dev->vpd = vpd;
353         return 0;
354 }
355
356 void pci_vpd_release(struct pci_dev *dev)
357 {
358         kfree(dev->vpd);
359 }
360
361 static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj,
362                              struct bin_attribute *bin_attr, char *buf,
363                              loff_t off, size_t count)
364 {
365         struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
366
367         return pci_read_vpd(dev, off, count, buf);
368 }
369
370 static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj,
371                               struct bin_attribute *bin_attr, char *buf,
372                               loff_t off, size_t count)
373 {
374         struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
375
376         return pci_write_vpd(dev, off, count, buf);
377 }
378
379 void pcie_vpd_create_sysfs_dev_files(struct pci_dev *dev)
380 {
381         int retval;
382         struct bin_attribute *attr;
383
384         if (!dev->vpd)
385                 return;
386
387         attr = kzalloc(sizeof(*attr), GFP_ATOMIC);
388         if (!attr)
389                 return;
390
391         sysfs_bin_attr_init(attr);
392         attr->size = 0;
393         attr->attr.name = "vpd";
394         attr->attr.mode = S_IRUSR | S_IWUSR;
395         attr->read = read_vpd_attr;
396         attr->write = write_vpd_attr;
397         retval = sysfs_create_bin_file(&dev->dev.kobj, attr);
398         if (retval) {
399                 kfree(attr);
400                 return;
401         }
402
403         dev->vpd->attr = attr;
404 }
405
406 void pcie_vpd_remove_sysfs_dev_files(struct pci_dev *dev)
407 {
408         if (dev->vpd && dev->vpd->attr) {
409                 sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr);
410                 kfree(dev->vpd->attr);
411         }
412 }
413
414 int pci_vpd_find_tag(const u8 *buf, unsigned int off, unsigned int len, u8 rdt)
415 {
416         int i;
417
418         for (i = off; i < len; ) {
419                 u8 val = buf[i];
420
421                 if (val & PCI_VPD_LRDT) {
422                         /* Don't return success of the tag isn't complete */
423                         if (i + PCI_VPD_LRDT_TAG_SIZE > len)
424                                 break;
425
426                         if (val == rdt)
427                                 return i;
428
429                         i += PCI_VPD_LRDT_TAG_SIZE +
430                              pci_vpd_lrdt_size(&buf[i]);
431                 } else {
432                         u8 tag = val & ~PCI_VPD_SRDT_LEN_MASK;
433
434                         if (tag == rdt)
435                                 return i;
436
437                         if (tag == PCI_VPD_SRDT_END)
438                                 break;
439
440                         i += PCI_VPD_SRDT_TAG_SIZE +
441                              pci_vpd_srdt_size(&buf[i]);
442                 }
443         }
444
445         return -ENOENT;
446 }
447 EXPORT_SYMBOL_GPL(pci_vpd_find_tag);
448
449 int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
450                               unsigned int len, const char *kw)
451 {
452         int i;
453
454         for (i = off; i + PCI_VPD_INFO_FLD_HDR_SIZE <= off + len;) {
455                 if (buf[i + 0] == kw[0] &&
456                     buf[i + 1] == kw[1])
457                         return i;
458
459                 i += PCI_VPD_INFO_FLD_HDR_SIZE +
460                      pci_vpd_info_field_size(&buf[i]);
461         }
462
463         return -ENOENT;
464 }
465 EXPORT_SYMBOL_GPL(pci_vpd_find_info_keyword);
466
467 #ifdef CONFIG_PCI_QUIRKS
468 /*
469  * Quirk non-zero PCI functions to route VPD access through function 0 for
470  * devices that share VPD resources between functions.  The functions are
471  * expected to be identical devices.
472  */
473 static void quirk_f0_vpd_link(struct pci_dev *dev)
474 {
475         struct pci_dev *f0;
476
477         if (!PCI_FUNC(dev->devfn))
478                 return;
479
480         f0 = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
481         if (!f0)
482                 return;
483
484         if (f0->vpd && dev->class == f0->class &&
485             dev->vendor == f0->vendor && dev->device == f0->device)
486                 dev->dev_flags |= PCI_DEV_FLAGS_VPD_REF_F0;
487
488         pci_dev_put(f0);
489 }
490 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
491                               PCI_CLASS_NETWORK_ETHERNET, 8, quirk_f0_vpd_link);
492
493 /*
494  * If a device follows the VPD format spec, the PCI core will not read or
495  * write past the VPD End Tag.  But some vendors do not follow the VPD
496  * format spec, so we can't tell how much data is safe to access.  Devices
497  * may behave unpredictably if we access too much.  Blacklist these devices
498  * so we don't touch VPD at all.
499  */
500 static void quirk_blacklist_vpd(struct pci_dev *dev)
501 {
502         if (dev->vpd) {
503                 dev->vpd->len = 0;
504                 pci_warn(dev, FW_BUG "disabling VPD access (can't determine size of non-standard VPD format)\n");
505         }
506 }
507 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0060, quirk_blacklist_vpd);
508 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x007c, quirk_blacklist_vpd);
509 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0413, quirk_blacklist_vpd);
510 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0078, quirk_blacklist_vpd);
511 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0079, quirk_blacklist_vpd);
512 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0073, quirk_blacklist_vpd);
513 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0071, quirk_blacklist_vpd);
514 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005b, quirk_blacklist_vpd);
515 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x002f, quirk_blacklist_vpd);
516 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005d, quirk_blacklist_vpd);
517 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005f, quirk_blacklist_vpd);
518 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, PCI_ANY_ID,
519                 quirk_blacklist_vpd);
520 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_QLOGIC, 0x2261, quirk_blacklist_vpd);
521 /*
522  * The Amazon Annapurna Labs 0x0031 device id is reused for other non Root Port
523  * device types, so the quirk is registered for the PCI_CLASS_BRIDGE_PCI class.
524  */
525 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_AMAZON_ANNAPURNA_LABS, 0x0031,
526                               PCI_CLASS_BRIDGE_PCI, 8, quirk_blacklist_vpd);
527
528 static void pci_vpd_set_size(struct pci_dev *dev, size_t len)
529 {
530         struct pci_vpd *vpd = dev->vpd;
531
532         if (!vpd || len == 0 || len > PCI_VPD_MAX_SIZE)
533                 return;
534
535         vpd->valid = 1;
536         vpd->len = len;
537 }
538
539 static void quirk_chelsio_extend_vpd(struct pci_dev *dev)
540 {
541         int chip = (dev->device & 0xf000) >> 12;
542         int func = (dev->device & 0x0f00) >>  8;
543         int prod = (dev->device & 0x00ff) >>  0;
544
545         /*
546          * If this is a T3-based adapter, there's a 1KB VPD area at offset
547          * 0xc00 which contains the preferred VPD values.  If this is a T4 or
548          * later based adapter, the special VPD is at offset 0x400 for the
549          * Physical Functions (the SR-IOV Virtual Functions have no VPD
550          * Capabilities).  The PCI VPD Access core routines will normally
551          * compute the size of the VPD by parsing the VPD Data Structure at
552          * offset 0x000.  This will result in silent failures when attempting
553          * to accesses these other VPD areas which are beyond those computed
554          * limits.
555          */
556         if (chip == 0x0 && prod >= 0x20)
557                 pci_vpd_set_size(dev, 8192);
558         else if (chip >= 0x4 && func < 0x8)
559                 pci_vpd_set_size(dev, 2048);
560 }
561
562 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, PCI_ANY_ID,
563                         quirk_chelsio_extend_vpd);
564
565 #endif