Re: [PATCH 2/2] nvmem: add ONIE NVMEM cells support

2020-05-29 Thread kbuild test robot
Hi Vadym,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on linux/master v5.7-rc7 next-20200529]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:
https://github.com/0day-ci/linux/commits/Vadym-Kochan/nvmem-add-ONIE-NVMEM-cells-provider/20200528-041903
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 
b0c3ba31be3e45a130e13b278cf3b90f69bda6f6
config: x86_64-randconfig-r024-20200529 (attached as .config)
compiler: clang version 11.0.0 (https://github.com/llvm/llvm-project 
2d068e534f1671459e1b135852c1b3c10502e929)
reproduce (this is a W=1 build):
wget 
https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O 
~/bin/make.cross
chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kbuild test robot 

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> drivers/nvmem/onie-cells.c:56:57: warning: size argument in 'memcmp' call is 
>> a comparison [-Wmemsize-comparison]
if (memcmp(hdr->id, ONIE_NVMEM_HDR_ID, sizeof(hdr->id) != 0))
^~~~
drivers/nvmem/onie-cells.c:56:6: note: did you mean to compare the result of 
'memcmp' instead?
if (memcmp(hdr->id, ONIE_NVMEM_HDR_ID, sizeof(hdr->id) != 0))
^  ~
)
drivers/nvmem/onie-cells.c:56:41: note: explicitly cast the argument to size_t 
to silence this warning
if (memcmp(hdr->id, ONIE_NVMEM_HDR_ID, sizeof(hdr->id) != 0))
^
(size_t)(   )
1 warning generated.

vim +/memcmp +56 drivers/nvmem/onie-cells.c

53  
54  static bool onie_nvmem_hdr_is_valid(struct onie_nvmem_hdr *hdr)
55  {
  > 56  if (memcmp(hdr->id, ONIE_NVMEM_HDR_ID, sizeof(hdr->id) != 0))
57  return false;
58  if (hdr->version != 0x1)
59  return false;
60  
61  return true;
62  }
63  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-...@lists.01.org


.config.gz
Description: application/gzip


[PATCH 2/2] nvmem: add ONIE NVMEM cells support

2020-05-27 Thread Vadym Kochan
ONIE is a small operating system, pre-installed on bare metal network
switches, that provides an environment for automated provisioning.

This system requires that NVMEM (EEPROM) device holds various system
information (mac address, platform name, etc) in a special TLV layout.

The driver registers ONIE TLV attributes as NVMEM cells which can be
accessed by other platform driver. Also it allows to use
of_get_mac_address() to retrieve mac address for the netdev.

Signed-off-by: Vadym Kochan 
---
 drivers/nvmem/Kconfig  |   9 +
 drivers/nvmem/Makefile |   3 +
 drivers/nvmem/onie-cells.c | 332 +
 3 files changed, 344 insertions(+)
 create mode 100644 drivers/nvmem/onie-cells.c

diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
index d7b7f6d688e7..dd9298487992 100644
--- a/drivers/nvmem/Kconfig
+++ b/drivers/nvmem/Kconfig
@@ -273,4 +273,13 @@ config SPRD_EFUSE
  This driver can also be built as a module. If so, the module
  will be called nvmem-sprd-efuse.
 
+config NVMEM_ONIE_CELLS
+   tristate "ONIE TLV cells support"
+   help
+ This is a driver to provide cells from ONIE TLV structure stored
+ on NVME device.
+
+ This driver can also be built as a module. If so, the module
+ will be called nvmem-onie-cells.
+
 endif
diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
index a7c377218341..2199784a489f 100644
--- a/drivers/nvmem/Makefile
+++ b/drivers/nvmem/Makefile
@@ -55,3 +55,6 @@ obj-$(CONFIG_NVMEM_ZYNQMP)+= nvmem_zynqmp_nvmem.o
 nvmem_zynqmp_nvmem-y   := zynqmp_nvmem.o
 obj-$(CONFIG_SPRD_EFUSE)   += nvmem_sprd_efuse.o
 nvmem_sprd_efuse-y := sprd-efuse.o
+
+obj-$(CONFIG_NVMEM_ONIE_CELLS) += nvmem-onie-cells.o
+nvmem-onie-cells-y := onie-cells.o
diff --git a/drivers/nvmem/onie-cells.c b/drivers/nvmem/onie-cells.c
new file mode 100644
index ..d5afd4a955db
--- /dev/null
+++ b/drivers/nvmem/onie-cells.c
@@ -0,0 +1,332 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * ONIE NVMEM cells provider
+ *
+ * Author: Vadym Kochan 
+ */
+
+#define ONIE_NVMEM_DRVNAME "onie-nvmem-cells"
+
+#define pr_fmt(fmt) ONIE_NVMEM_DRVNAME ": " fmt
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define ONIE_NVMEM_TLV_MAX_LEN 2048
+
+#define ONIE_NVMEM_HDR_ID  "TlvInfo"
+
+struct onie_nvmem_hdr {
+   u8 id[8];
+   u8 version;
+   __be16 data_len;
+} __packed;
+
+struct onie_nvmem_tlv {
+   u8 type;
+   u8 len;
+   u8 val[0];
+} __packed;
+
+struct onie_nvmem_attr {
+   struct list_head head;
+   const char *name;
+   unsigned int offset;
+   unsigned int len;
+};
+
+struct onie_nvmem {
+   struct nvmem_device *nvmem;
+   unsigned int attr_count;
+   struct list_head attrs;
+
+   struct nvmem_cell_lookup *cell_lookup;
+   struct nvmem_cell_table cell_tbl;
+};
+
+static bool onie_nvmem_hdr_is_valid(struct onie_nvmem_hdr *hdr)
+{
+   if (memcmp(hdr->id, ONIE_NVMEM_HDR_ID, sizeof(hdr->id) != 0))
+   return false;
+   if (hdr->version != 0x1)
+   return false;
+
+   return true;
+}
+
+static void onie_nvmem_attrs_free(struct onie_nvmem *onie)
+{
+   struct onie_nvmem_attr *attr, *tmp;
+
+   list_for_each_entry_safe(attr, tmp, >attrs, head) {
+   list_del(>head);
+   kfree(attr);
+   }
+}
+
+static const char *onie_nvmem_attr_name(u8 type)
+{
+   switch (type) {
+   case 0x21: return "product-name";
+   case 0x22: return "part-number";
+   case 0x23: return "serial-number";
+   case 0x24: return "mac-address";
+   case 0x25: return "manufacture-date";
+   case 0x26: return "device-version";
+   case 0x27: return "label-revision";
+   case 0x28: return "platforn-name";
+   case 0x29: return "onie-version";
+   case 0x2A: return "num-macs";
+   case 0x2B: return "manufacturer";
+   case 0x2C: return "country-code";
+   case 0x2D: return "vendor";
+   case 0x2E: return "diag-version";
+   case 0x2F: return "service-tag";
+   case 0xFD: return "vendor-extension";
+   case 0xFE: return "crc32";
+
+   default: return "unknown";
+   }
+}
+
+static int onie_nvmem_tlv_parse(struct onie_nvmem *onie, u8 *data, u16 len)
+{
+   unsigned int hlen = sizeof(struct onie_nvmem_hdr);
+   unsigned int offset = 0;
+   int err;
+
+   while (offset < len) {
+   struct onie_nvmem_attr *attr;
+   struct onie_nvmem_tlv *tlv;
+
+   tlv = (struct onie_nvmem_tlv *)(data + offset);
+
+   if (offset + tlv->len >= len) {
+   struct nvmem_device *nvmem = onie->nvmem;
+
+   pr_err("%s: TLV len is too big(0x%x) at 0x%x\n",
+  nvmem_dev_name(nvmem), tlv->len, hlen + offset);
+
+   /*