PCI/VPD: Remove struct pci_vpd.valid member
[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 {
17         struct mutex    lock;
18         unsigned int    len;
19         u8              cap;
20 };
21
22 static struct pci_dev *pci_get_func0_dev(struct pci_dev *dev)
23 {
24         return pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
25 }
26
27 #define PCI_VPD_MAX_SIZE        (PCI_VPD_ADDR_MASK + 1)
28 #define PCI_VPD_SZ_INVALID      UINT_MAX
29
30 /**
31  * pci_vpd_size - determine actual size of Vital Product Data
32  * @dev:        pci device struct
33  */
34 static size_t pci_vpd_size(struct pci_dev *dev)
35 {
36         size_t off = 0, size;
37         unsigned char tag, header[1+2]; /* 1 byte tag, 2 bytes length */
38
39         /* Otherwise the following reads would fail. */
40         dev->vpd->len = PCI_VPD_MAX_SIZE;
41
42         while (pci_read_vpd(dev, off, 1, header) == 1) {
43                 size = 0;
44
45                 if (off == 0 && (header[0] == 0x00 || header[0] == 0xff))
46                         goto error;
47
48                 if (header[0] & PCI_VPD_LRDT) {
49                         /* Large Resource Data Type Tag */
50                         if (pci_read_vpd(dev, off + 1, 2, &header[1]) != 2) {
51                                 pci_warn(dev, "failed VPD read at offset %zu\n",
52                                          off + 1);
53                                 return off ?: PCI_VPD_SZ_INVALID;
54                         }
55                         size = pci_vpd_lrdt_size(header);
56                         if (off + size > PCI_VPD_MAX_SIZE)
57                                 goto error;
58
59                         off += PCI_VPD_LRDT_TAG_SIZE + size;
60                 } else {
61                         /* Short Resource Data Type Tag */
62                         tag = pci_vpd_srdt_tag(header);
63                         size = pci_vpd_srdt_size(header);
64                         if (off + size > PCI_VPD_MAX_SIZE)
65                                 goto error;
66
67                         off += PCI_VPD_SRDT_TAG_SIZE + size;
68                         if (tag == PCI_VPD_STIN_END)    /* End tag descriptor */
69                                 return off;
70                 }
71         }
72         return off;
73
74 error:
75         pci_info(dev, "invalid VPD tag %#04x (size %zu) at offset %zu%s\n",
76                  header[0], size, off, off == 0 ?
77                  "; assume missing optional EEPROM" : "");
78         return off ?: PCI_VPD_SZ_INVALID;
79 }
80
81 /*
82  * Wait for last operation to complete.
83  * This code has to spin since there is no other notification from the PCI
84  * hardware. Since the VPD is often implemented by serial attachment to an
85  * EEPROM, it may take many milliseconds to complete.
86  * @set: if true wait for flag to be set, else wait for it to be cleared
87  *
88  * Returns 0 on success, negative values indicate error.
89  */
90 static int pci_vpd_wait(struct pci_dev *dev, bool set)
91 {
92         struct pci_vpd *vpd = dev->vpd;
93         unsigned long timeout = jiffies + msecs_to_jiffies(125);
94         unsigned long max_sleep = 16;
95         u16 status;
96         int ret;
97
98         do {
99                 ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
100                                                 &status);
101                 if (ret < 0)
102                         return ret;
103
104                 if (!!(status & PCI_VPD_ADDR_F) == set)
105                         return 0;
106
107                 if (time_after(jiffies, timeout))
108                         break;
109
110                 usleep_range(10, max_sleep);
111                 if (max_sleep < 1024)
112                         max_sleep *= 2;
113         } while (true);
114
115         pci_warn(dev, "VPD access failed.  This is likely a firmware bug on this device.  Contact the card vendor for a firmware update\n");
116         return -ETIMEDOUT;
117 }
118
119 static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
120                             void *arg)
121 {
122         struct pci_vpd *vpd = dev->vpd;
123         int ret = 0;
124         loff_t end = pos + count;
125         u8 *buf = arg;
126
127         if (!vpd)
128                 return -ENODEV;
129
130         if (pos < 0)
131                 return -EINVAL;
132
133         if (!vpd->len)
134                 vpd->len = pci_vpd_size(dev);
135
136         if (vpd->len == PCI_VPD_SZ_INVALID)
137                 return -EIO;
138
139         if (pos > vpd->len)
140                 return 0;
141
142         if (end > vpd->len) {
143                 end = vpd->len;
144                 count = end - pos;
145         }
146
147         if (mutex_lock_killable(&vpd->lock))
148                 return -EINTR;
149
150         while (pos < end) {
151                 u32 val;
152                 unsigned int i, skip;
153
154                 if (fatal_signal_pending(current)) {
155                         ret = -EINTR;
156                         break;
157                 }
158
159                 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
160                                                  pos & ~3);
161                 if (ret < 0)
162                         break;
163                 ret = pci_vpd_wait(dev, true);
164                 if (ret < 0)
165                         break;
166
167                 ret = pci_user_read_config_dword(dev, vpd->cap + PCI_VPD_DATA, &val);
168                 if (ret < 0)
169                         break;
170
171                 skip = pos & 3;
172                 for (i = 0;  i < sizeof(u32); i++) {
173                         if (i >= skip) {
174                                 *buf++ = val;
175                                 if (++pos == end)
176                                         break;
177                         }
178                         val >>= 8;
179                 }
180         }
181
182         mutex_unlock(&vpd->lock);
183         return ret ? ret : count;
184 }
185
186 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
187                              const void *arg)
188 {
189         struct pci_vpd *vpd = dev->vpd;
190         const u8 *buf = arg;
191         loff_t end = pos + count;
192         int ret = 0;
193
194         if (!vpd)
195                 return -ENODEV;
196
197         if (pos < 0 || (pos & 3) || (count & 3))
198                 return -EINVAL;
199
200         if (!vpd->len)
201                 vpd->len = pci_vpd_size(dev);
202
203         if (vpd->len == PCI_VPD_SZ_INVALID)
204                 return -EIO;
205
206         if (end > vpd->len)
207                 return -EINVAL;
208
209         if (mutex_lock_killable(&vpd->lock))
210                 return -EINTR;
211
212         while (pos < end) {
213                 u32 val;
214
215                 val = *buf++;
216                 val |= *buf++ << 8;
217                 val |= *buf++ << 16;
218                 val |= *buf++ << 24;
219
220                 ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
221                 if (ret < 0)
222                         break;
223                 ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
224                                                  pos | PCI_VPD_ADDR_F);
225                 if (ret < 0)
226                         break;
227
228                 ret = pci_vpd_wait(dev, false);
229                 if (ret < 0)
230                         break;
231
232                 pos += sizeof(u32);
233         }
234
235         mutex_unlock(&vpd->lock);
236         return ret ? ret : count;
237 }
238
239 void pci_vpd_init(struct pci_dev *dev)
240 {
241         struct pci_vpd *vpd;
242         u8 cap;
243
244         cap = pci_find_capability(dev, PCI_CAP_ID_VPD);
245         if (!cap)
246                 return;
247
248         vpd = kzalloc(sizeof(*vpd), GFP_ATOMIC);
249         if (!vpd)
250                 return;
251
252         mutex_init(&vpd->lock);
253         vpd->cap = cap;
254         dev->vpd = vpd;
255 }
256
257 void pci_vpd_release(struct pci_dev *dev)
258 {
259         kfree(dev->vpd);
260 }
261
262 static ssize_t vpd_read(struct file *filp, struct kobject *kobj,
263                         struct bin_attribute *bin_attr, char *buf, loff_t off,
264                         size_t count)
265 {
266         struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
267
268         return pci_read_vpd(dev, off, count, buf);
269 }
270
271 static ssize_t vpd_write(struct file *filp, struct kobject *kobj,
272                          struct bin_attribute *bin_attr, char *buf, loff_t off,
273                          size_t count)
274 {
275         struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj));
276
277         return pci_write_vpd(dev, off, count, buf);
278 }
279 static BIN_ATTR(vpd, 0600, vpd_read, vpd_write, 0);
280
281 static struct bin_attribute *vpd_attrs[] = {
282         &bin_attr_vpd,
283         NULL,
284 };
285
286 static umode_t vpd_attr_is_visible(struct kobject *kobj,
287                                    struct bin_attribute *a, int n)
288 {
289         struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
290
291         if (!pdev->vpd)
292                 return 0;
293
294         return a->attr.mode;
295 }
296
297 const struct attribute_group pci_dev_vpd_attr_group = {
298         .bin_attrs = vpd_attrs,
299         .is_bin_visible = vpd_attr_is_visible,
300 };
301
302 int pci_vpd_find_tag(const u8 *buf, unsigned int len, u8 rdt)
303 {
304         int i = 0;
305
306         /* look for LRDT tags only, end tag is the only SRDT tag */
307         while (i + PCI_VPD_LRDT_TAG_SIZE <= len && buf[i] & PCI_VPD_LRDT) {
308                 if (buf[i] == rdt)
309                         return i;
310
311                 i += PCI_VPD_LRDT_TAG_SIZE + pci_vpd_lrdt_size(buf + i);
312         }
313
314         return -ENOENT;
315 }
316 EXPORT_SYMBOL_GPL(pci_vpd_find_tag);
317
318 int pci_vpd_find_info_keyword(const u8 *buf, unsigned int off,
319                               unsigned int len, const char *kw)
320 {
321         int i;
322
323         for (i = off; i + PCI_VPD_INFO_FLD_HDR_SIZE <= off + len;) {
324                 if (buf[i + 0] == kw[0] &&
325                     buf[i + 1] == kw[1])
326                         return i;
327
328                 i += PCI_VPD_INFO_FLD_HDR_SIZE +
329                      pci_vpd_info_field_size(&buf[i]);
330         }
331
332         return -ENOENT;
333 }
334 EXPORT_SYMBOL_GPL(pci_vpd_find_info_keyword);
335
336 /**
337  * pci_read_vpd - Read one entry from Vital Product Data
338  * @dev:        PCI device struct
339  * @pos:        offset in VPD space
340  * @count:      number of bytes to read
341  * @buf:        pointer to where to store result
342  */
343 ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf)
344 {
345         ssize_t ret;
346
347         if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) {
348                 dev = pci_get_func0_dev(dev);
349                 if (!dev)
350                         return -ENODEV;
351
352                 ret = pci_vpd_read(dev, pos, count, buf);
353                 pci_dev_put(dev);
354                 return ret;
355         }
356
357         return pci_vpd_read(dev, pos, count, buf);
358 }
359 EXPORT_SYMBOL(pci_read_vpd);
360
361 /**
362  * pci_write_vpd - Write entry to Vital Product Data
363  * @dev:        PCI device struct
364  * @pos:        offset in VPD space
365  * @count:      number of bytes to write
366  * @buf:        buffer containing write data
367  */
368 ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf)
369 {
370         ssize_t ret;
371
372         if (dev->dev_flags & PCI_DEV_FLAGS_VPD_REF_F0) {
373                 dev = pci_get_func0_dev(dev);
374                 if (!dev)
375                         return -ENODEV;
376
377                 ret = pci_vpd_write(dev, pos, count, buf);
378                 pci_dev_put(dev);
379                 return ret;
380         }
381
382         return pci_vpd_write(dev, pos, count, buf);
383 }
384 EXPORT_SYMBOL(pci_write_vpd);
385
386 #ifdef CONFIG_PCI_QUIRKS
387 /*
388  * Quirk non-zero PCI functions to route VPD access through function 0 for
389  * devices that share VPD resources between functions.  The functions are
390  * expected to be identical devices.
391  */
392 static void quirk_f0_vpd_link(struct pci_dev *dev)
393 {
394         struct pci_dev *f0;
395
396         if (!PCI_FUNC(dev->devfn))
397                 return;
398
399         f0 = pci_get_func0_dev(dev);
400         if (!f0)
401                 return;
402
403         if (f0->vpd && dev->class == f0->class &&
404             dev->vendor == f0->vendor && dev->device == f0->device)
405                 dev->dev_flags |= PCI_DEV_FLAGS_VPD_REF_F0;
406
407         pci_dev_put(f0);
408 }
409 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
410                               PCI_CLASS_NETWORK_ETHERNET, 8, quirk_f0_vpd_link);
411
412 /*
413  * If a device follows the VPD format spec, the PCI core will not read or
414  * write past the VPD End Tag.  But some vendors do not follow the VPD
415  * format spec, so we can't tell how much data is safe to access.  Devices
416  * may behave unpredictably if we access too much.  Blacklist these devices
417  * so we don't touch VPD at all.
418  */
419 static void quirk_blacklist_vpd(struct pci_dev *dev)
420 {
421         if (dev->vpd) {
422                 dev->vpd->len = PCI_VPD_SZ_INVALID;
423                 pci_warn(dev, FW_BUG "disabling VPD access (can't determine size of non-standard VPD format)\n");
424         }
425 }
426 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0060, quirk_blacklist_vpd);
427 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x007c, quirk_blacklist_vpd);
428 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0413, quirk_blacklist_vpd);
429 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0078, quirk_blacklist_vpd);
430 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0079, quirk_blacklist_vpd);
431 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0073, quirk_blacklist_vpd);
432 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x0071, quirk_blacklist_vpd);
433 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005b, quirk_blacklist_vpd);
434 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x002f, quirk_blacklist_vpd);
435 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005d, quirk_blacklist_vpd);
436 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_LSI_LOGIC, 0x005f, quirk_blacklist_vpd);
437 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATTANSIC, PCI_ANY_ID,
438                 quirk_blacklist_vpd);
439 /*
440  * The Amazon Annapurna Labs 0x0031 device id is reused for other non Root Port
441  * device types, so the quirk is registered for the PCI_CLASS_BRIDGE_PCI class.
442  */
443 DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_AMAZON_ANNAPURNA_LABS, 0x0031,
444                               PCI_CLASS_BRIDGE_PCI, 8, quirk_blacklist_vpd);
445
446 static void pci_vpd_set_size(struct pci_dev *dev, size_t len)
447 {
448         struct pci_vpd *vpd = dev->vpd;
449
450         if (!vpd || len == 0 || len > PCI_VPD_MAX_SIZE)
451                 return;
452
453         vpd->len = len;
454 }
455
456 static void quirk_chelsio_extend_vpd(struct pci_dev *dev)
457 {
458         int chip = (dev->device & 0xf000) >> 12;
459         int func = (dev->device & 0x0f00) >>  8;
460         int prod = (dev->device & 0x00ff) >>  0;
461
462         /*
463          * If this is a T3-based adapter, there's a 1KB VPD area at offset
464          * 0xc00 which contains the preferred VPD values.  If this is a T4 or
465          * later based adapter, the special VPD is at offset 0x400 for the
466          * Physical Functions (the SR-IOV Virtual Functions have no VPD
467          * Capabilities).  The PCI VPD Access core routines will normally
468          * compute the size of the VPD by parsing the VPD Data Structure at
469          * offset 0x000.  This will result in silent failures when attempting
470          * to accesses these other VPD areas which are beyond those computed
471          * limits.
472          */
473         if (chip == 0x0 && prod >= 0x20)
474                 pci_vpd_set_size(dev, 8192);
475         else if (chip >= 0x4 && func < 0x8)
476                 pci_vpd_set_size(dev, 2048);
477 }
478
479 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_CHELSIO, PCI_ANY_ID,
480                         quirk_chelsio_extend_vpd);
481
482 #endif