PCI/VPD: Don't check Large Resource Item Names for validity
[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 mutex    lock;
24         unsigned int    len;
25         u16             flag;
26         u8              cap;
27         unsigned int    busy:1;
28         unsigned int    valid:1;
29 };
30
31 static struct pci_dev *pci_get_func0_dev(struct pci_dev *dev)
32 {
33         return pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
34 }
35
36 /**
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
42  */
43 ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
44 {
45         if (!dev->vpd || !dev->vpd->ops)
46                 return -ENODEV;
47         return dev->vpd->ops->read(dev, pos, count, buf);
48 }
49 EXPORT_SYMBOL(pci_read_vpd);
50
51 /**
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
57  */
58 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
59 {
60         if (!dev->vpd || !dev->vpd->ops)
61                 return -ENODEV;
62         return dev->vpd->ops->write(dev, pos, count, buf);
63 }
64 EXPORT_SYMBOL(pci_write_vpd);
65
66 #define PCI_VPD_MAX_SIZE (PCI_VPD_ADDR_MASK + 1)
67
68 /**
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
72  */
73 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
74 {
75         size_t off = 0, size;
76         unsigned char tag, header[1+2]; /* 1 byte tag, 2 bytes length */
77
78         while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
79                 size = 0;
80
81                 if (off == 0 && (header[0] == 0x00 || header[0] == 0xff))
82                         goto error;
83
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",
88                                          off + 1);
89                                 return 0;
90                         }
91                         size = pci_vpd_lrdt_size(header);
92                         if (off + size > PCI_VPD_MAX_SIZE)
93                                 goto error;
94
95                         off += PCI_VPD_LRDT_TAG_SIZE + size;
96                 } else {
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)
101                                 goto error;
102
103                         off += PCI_VPD_SRDT_TAG_SIZE + size;
104                         if (tag == PCI_VPD_STIN_END)    /* End tag descriptor */
105                                 return off;
106                 }
107         }
108         return 0;
109
110 error:
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" : "");
114         return 0;
115 }
116
117 /*
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.
122  *
123  * Returns 0 on success, negative values indicate error.
124  */
125 static int pci_vpd_wait(struct pci_dev *dev)
126 {
127         struct pci_vpd *vpd = dev->vpd;
128         unsigned long timeout = jiffies + msecs_to_jiffies(125);
129         unsigned long max_sleep = 16;
130         u16 status;
131         int ret;
132
133         if (!vpd->busy)
134                 return 0;
135
136         do {
137                 ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
138                                                 &status);
139                 if (ret < 0)
140                         return ret;
141
142                 if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
143                         vpd->busy = 0;
144                         return 0;
145                 }
146
147                 if (fatal_signal_pending(current))
148                         return -EINTR;
149
150                 if (time_after(jiffies, timeout))
151                         break;
152
153                 usleep_range(10, max_sleep);
154                 if (max_sleep < 1024)
155                         max_sleep *= 2;
156         } while (true);
157
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");
159         return -ETIMEDOUT;
160 }
161
162 static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
163                             void *arg)
164 {
165         struct pci_vpd *vpd = dev->vpd;
166         int ret;
167         loff_t end = pos + count;
168         u8 *buf = arg;
169
170         if (pos < 0)
171                 return -EINVAL;
172
173         if (!vpd->valid) {
174                 vpd->valid = 1;
175                 vpd->len = pci_vpd_size(dev, vpd->len);
176         }
177
178         if (vpd->len == 0)
179                 return -EIO;
180
181         if (pos > vpd->len)
182                 return 0;
183
184         if (end > vpd->len) {
185                 end = vpd->len;
186                 count = end - pos;
187         }
188
189         if (mutex_lock_killable(&vpd->lock))
190                 return -EINTR;
191
192         ret = pci_vpd_wait(dev);
193         if (ret < 0)
194                 goto out;
195
196         while (pos < end) {
197                 u32 val;
198                 unsigned int i, skip;
199
200                 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
201                                                  pos & ~3);
202                 if (ret < 0)
203                         break;
204                 vpd->busy = 1;
205                 vpd->flag = PCI_VPD_ADDR_F;
206                 ret = pci_vpd_wait(dev);
207                 if (ret < 0)
208                         break;
209
210                 ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
211                 if (ret < 0)
212                         break;
213
214                 skip = pos & 3;
215                 for (i = 0;  i < sizeof(u32); i++) {
216                         if (i >= skip) {
217                                 *buf++ = val;
218                                 if (++pos == end)
219                                         break;
220                         }
221                         val >>= 8;
222                 }
223         }
224 out:
225         mutex_unlock(&vpd->lock);
226         return ret ? ret : count;
227 }
228
229 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
230                              const void *arg)
231 {
232         struct pci_vpd *vpd = dev->vpd;
233         const u8 *buf = arg;
234         loff_t end = pos + count;
235         int ret = 0;
236
237         if (pos < 0 || (pos & 3) || (count & 3))
238                 return -EINVAL;
239
240         if (!vpd->valid) {
241                 vpd->valid = 1;
242                 vpd->len = pci_vpd_size(dev, vpd->len);
243         }
244
245         if (vpd->len == 0)
246                 return -EIO;
247
248         if (end > vpd->len)
249                 return -EINVAL;
250
251         if (mutex_lock_killable(&vpd->lock))
252                 return -EINTR;
253
254         ret = pci_vpd_wait(dev);
255         if (ret < 0)
256                 goto out;
257
258         while (pos < end) {
259                 u32 val;
260
261                 val = *buf++;
262                 val |= *buf++ << 8;
263                 val |= *buf++ << 16;
264                 val |= *buf++ << 24;
265
266                 ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
267                 if (ret < 0)
268                         break;
269                 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
270                                                  pos | PCI_VPD_ADDR_F);
271                 if (ret < 0)
272                         break;
273
274                 vpd->busy = 1;
275                 vpd->flag = 0;
276                 ret = pci_vpd_wait(dev);
277                 if (ret < 0)
278                         break;
279
280                 pos += sizeof(u32);
281         }
282 out:
283         mutex_unlock(&vpd->lock);
284         return ret ? ret : count;
285 }
286
287 static const struct pci_vpd_ops pci_vpd_ops = {
288         .read = pci_vpd_read,
289         .write = pci_vpd_write,
290 };
291
292 static ssize_t pci_vpd_f0_read(struct pci_dev *dev, loff_t pos, size_t count,
293                                void *arg)
294 {
295         struct pci_dev *tdev = pci_get_func0_dev(dev);
296         ssize_t ret;
297
298         if (!tdev)
299                 return -ENODEV;
300
301         ret = pci_read_vpd(tdev, pos, count, arg);
302         pci_dev_put(tdev);
303         return ret;
304 }
305
306 static ssize_t pci_vpd_f0_write(struct pci_dev *dev, loff_t pos, size_t count,
307                                 const void *arg)
308 {
309         struct pci_dev *tdev = pci_get_func0_dev(dev);
310         ssize_t ret;
311
312         if (!tdev)
313                 return -ENODEV;
314
315         ret = pci_write_vpd(tdev, pos, count, arg);
316         pci_dev_put(tdev);
317         return ret;
318 }
319
320 static const struct pci_vpd_ops pci_vpd_f0_ops = {
321         .read = pci_vpd_f0_read,
322         .write = pci_vpd_f0_write,
323 };
324
325 void pci_vpd_init(struct pci_dev *dev)
326 {
327         struct pci_vpd *vpd;
328         u8 cap;
329
330         cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
331         if (!cap)
332                 return;
333
334         vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
335         if (!vpd)
336                 return;
337
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;
341         else
342                 vpd->ops = &pci_vpd_ops;
343         mutex_init(&vpd->lock);
344         vpd->cap = cap;
345         vpd->busy = 0;
346         vpd->valid = 0;
347         dev->vpd = vpd;
348 }
349
350 void pci_vpd_release(struct pci_dev *dev)
351 {
352         kfree(dev->vpd);
353 }
354
355 static ssize_t vpd_read(struct file *filp, struct kobject *kobj,
356                         struct bin_attribute *bin_attr, char *buf, loff_t off,
357                         size_t count)
358 {
359         struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
360
361         return pci_read_vpd(dev, off, count, buf);
362 }
363
364 static ssize_t vpd_write(struct file *filp, struct kobject *kobj,
365                          struct bin_attribute *bin_attr, char *buf, loff_t off,
366                          size_t count)
367 {
368         struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
369
370         return pci_write_vpd(dev, off, count, buf);
371 }
372 static BIN_ATTR(vpd, 0600, vpd_read, vpd_write, 0);
373
374 static struct bin_attribute *vpd_attrs[] = {
375         &bin_attr_vpd,
376         NULL,
377 };
378
379 static umode_t vpd_attr_is_visible(struct kobject *kobj,
380                                    struct bin_attribute *a, int n)
381 {
382         struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
383
384         if (!pdev->vpd)
385                 return 0;
386
387         return a->attr.mode;
388 }
389
390 const struct attribute_group pci_dev_vpd_attr_group = {
391         .bin_attrs = vpd_attrs,
392         .is_bin_visible = vpd_attr_is_visible,
393 };
394
395 int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt)
396 {
397         int i = 0;
398
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) {
401                 if (buf[i] == rdt)
402                         return i;
403
404                 i += PCI_VPD_LRDT_TAG_SIZE + pci_vpd_lrdt_size(buf + i);
405         }
406
407         return -ENOENT;
408 }
409 EXPORT_SYMBOL_GPL(pci_vpd_find_tag);
410
411 int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
412                               unsigned int len, const char *kw)
413 {
414         int i;
415
416         for (i = off; i + PCI_VPD_INFO_FLD_HDR_SIZE <= off + len;) {
417                 if (buf[i + 0] == kw[0] &&
418                     buf[i + 1] == kw[1])
419                         return i;
420
421                 i += PCI_VPD_INFO_FLD_HDR_SIZE +
422                      pci_vpd_info_field_size(&buf[i]);
423         }
424
425         return -ENOENT;
426 }
427 EXPORT_SYMBOL_GPL(pci_vpd_find_info_keyword);
428
429 #ifdef CONFIG_PCI_QUIRKS
430 /*
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.
434  */
435 static void quirk_f0_vpd_link(struct pci_dev *dev)
436 {
437         struct pci_dev *f0;
438
439         if (!PCI_FUNC(dev->devfn))
440                 return;
441
442         f0 = pci_get_func0_dev(dev);
443         if (!f0)
444                 return;
445
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;
449
450         pci_dev_put(f0);
451 }
452 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
453                               PCI_CLASS_NETWORK_ETHERNET, 8, quirk_f0_vpd_link);
454
455 /*
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.
461  */
462 static void quirk_blacklist_vpd(struct pci_dev *dev)
463 {
464         if (dev->vpd) {
465                 dev->vpd->len = 0;
466                 pci_warn(dev, FW_BUG "disabling VPD access (can't determine size of non-standard VPD format)\n");
467         }
468 }
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);
482 /*
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.
485  */
486 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_AMAZON_ANNAPURNA_LABS, 0x0031,
487                               PCI_CLASS_BRIDGE_PCI, 8, quirk_blacklist_vpd);
488
489 static void pci_vpd_set_size(struct pci_dev *dev, size_t len)
490 {
491         struct pci_vpd *vpd = dev->vpd;
492
493         if (!vpd || len == 0 || len > PCI_VPD_MAX_SIZE)
494                 return;
495
496         vpd->valid = 1;
497         vpd->len = len;
498 }
499
500 static void quirk_chelsio_extend_vpd(struct pci_dev *dev)
501 {
502         int chip = (dev->device & 0xf000) >> 12;
503         int func = (dev->device & 0x0f00) >>  8;
504         int prod = (dev->device & 0x00ff) >>  0;
505
506         /*
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
515          * limits.
516          */
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);
521 }
522
523 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, PCI_ANY_ID,
524                         quirk_chelsio_extend_vpd);
525
526 #endif