vpd.c 15 KB

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