89ea86
From 912115ebf8ca6eb76dfdadbe8881b7c348743f27 Mon Sep 17 00:00:00 2001
89ea86
From: Ido Schimmel <idosch@nvidia.com>
89ea86
Date: Tue, 12 Oct 2021 16:25:13 +0300
89ea86
Subject: [PATCH 14/35] cmis: Initialize CMIS memory map
89ea86
89ea86
The CMIS memory map [1] consists of Lower Memory and Upper Memory.
89ea86
89ea86
The content of the Lower Memory is fixed and can be addressed using an
89ea86
offset between 0 and 127 (inclusive).
89ea86
89ea86
The Upper Memory is variable and optional and can be addressed by
89ea86
specifying a bank number, a page number and an offset between 128 and
89ea86
255 (inclusive).
89ea86
89ea86
Create a structure describing this memory map and initialize it with
89ea86
pointers to available pages.
89ea86
89ea86
In the IOCTL path, the structure holds pointers to regions of the
89ea86
continuous buffer passed to user space via the 'ETHTOOL_GMODULEEEPROM'
89ea86
command.
89ea86
89ea86
In the netlink path, the structure holds pointers to individual pages
89ea86
passed to user space via the 'MODULE_EEPROM_GET' message.
89ea86
89ea86
This structure will later allow us to consolidate the IOCTL and netlink
89ea86
parsing code paths and also easily support additional EEPROM pages.
89ea86
89ea86
[1] CMIS Rev. 5, pag. 97, section 8.1.1, Figure 8-1
89ea86
89ea86
Signed-off-by: Ido Schimmel <idosch@nvidia.com>
89ea86
---
89ea86
 cmis.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
89ea86
 cmis.h |  2 ++
89ea86
 2 files changed, 65 insertions(+)
89ea86
89ea86
diff --git a/cmis.c b/cmis.c
89ea86
index 68c5b2d3277b..8a6788416a00 100644
89ea86
--- a/cmis.c
89ea86
+++ b/cmis.c
89ea86
@@ -13,6 +13,15 @@
89ea86
 #include "sff-common.h"
89ea86
 #include "cmis.h"
89ea86
 
89ea86
+struct cmis_memory_map {
89ea86
+	const __u8 *lower_memory;
89ea86
+	const __u8 *upper_memory[1][2];	/* Bank, Page */
89ea86
+#define page_00h upper_memory[0x0][0x0]
89ea86
+#define page_01h upper_memory[0x0][0x1]
89ea86
+};
89ea86
+
89ea86
+#define CMIS_PAGE_SIZE		0x80
89ea86
+
89ea86
 static void cmis_show_identifier(const __u8 *id)
89ea86
 {
89ea86
 	sff8024_show_identifier(id, CMIS_ID_OFFSET);
89ea86
@@ -326,8 +335,34 @@ static void cmis_show_vendor_info(const __u8 *id)
89ea86
 			       "CLEI code");
89ea86
 }
89ea86
 
89ea86
+static void cmis_memory_map_init_buf(struct cmis_memory_map *map,
89ea86
+				     const __u8 *id)
89ea86
+{
89ea86
+	/* Lower Memory and Page 00h are always present.
89ea86
+	 *
89ea86
+	 * Offset into Upper Memory is between page size and twice the page
89ea86
+	 * size. Therefore, set the base address of each page to base address
89ea86
+	 * plus page size multiplied by the page number.
89ea86
+	 */
89ea86
+	map->lower_memory = id;
89ea86
+	map->page_00h = id;
89ea86
+
89ea86
+	/* Page 01h is only present when the module memory model is paged and
89ea86
+	 * not flat.
89ea86
+	 */
89ea86
+	if (map->lower_memory[CMIS_MEMORY_MODEL_OFFSET] &
89ea86
+	    CMIS_MEMORY_MODEL_MASK)
89ea86
+		return;
89ea86
+
89ea86
+	map->page_01h = id + CMIS_PAGE_SIZE;
89ea86
+}
89ea86
+
89ea86
 void cmis_show_all_ioctl(const __u8 *id)
89ea86
 {
89ea86
+	struct cmis_memory_map map = {};
89ea86
+
89ea86
+	cmis_memory_map_init_buf(&map, id);
89ea86
+
89ea86
 	cmis_show_identifier(id);
89ea86
 	cmis_show_power_info(id);
89ea86
 	cmis_show_connector(id);
89ea86
@@ -340,10 +375,38 @@ void cmis_show_all_ioctl(const __u8 *id)
89ea86
 	cmis_show_rev_compliance(id);
89ea86
 }
89ea86
 
89ea86
+static void
89ea86
+cmis_memory_map_init_pages(struct cmis_memory_map *map,
89ea86
+			   const struct ethtool_module_eeprom *page_zero,
89ea86
+			   const struct ethtool_module_eeprom *page_one)
89ea86
+{
89ea86
+	/* Lower Memory and Page 00h are always present.
89ea86
+	 *
89ea86
+	 * Offset into Upper Memory is between page size and twice the page
89ea86
+	 * size. Therefore, set the base address of each page to its base
89ea86
+	 * address minus page size. For Page 00h, this is the address of the
89ea86
+	 * Lower Memory.
89ea86
+	 */
89ea86
+	map->lower_memory = page_zero->data;
89ea86
+	map->page_00h = page_zero->data;
89ea86
+
89ea86
+	/* Page 01h is only present when the module memory model is paged and
89ea86
+	 * not flat.
89ea86
+	 */
89ea86
+	if (map->lower_memory[CMIS_MEMORY_MODEL_OFFSET] &
89ea86
+	    CMIS_MEMORY_MODEL_MASK)
89ea86
+		return;
89ea86
+
89ea86
+	map->page_01h = page_one->data - CMIS_PAGE_SIZE;
89ea86
+}
89ea86
+
89ea86
 void cmis_show_all_nl(const struct ethtool_module_eeprom *page_zero,
89ea86
 		      const struct ethtool_module_eeprom *page_one)
89ea86
 {
89ea86
 	const __u8 *page_zero_data = page_zero->data;
89ea86
+	struct cmis_memory_map map = {};
89ea86
+
89ea86
+	cmis_memory_map_init_pages(&map, page_zero, page_one);
89ea86
 
89ea86
 	cmis_show_identifier(page_zero_data);
89ea86
 	cmis_show_power_info(page_zero_data);
89ea86
diff --git a/cmis.h b/cmis.h
89ea86
index 734b90f4ddb4..53cbb5f57127 100644
89ea86
--- a/cmis.h
89ea86
+++ b/cmis.h
89ea86
@@ -4,6 +4,8 @@
89ea86
 /* Identifier and revision compliance (Page 0) */
89ea86
 #define CMIS_ID_OFFSET				0x00
89ea86
 #define CMIS_REV_COMPLIANCE_OFFSET		0x01
89ea86
+#define CMIS_MEMORY_MODEL_OFFSET		0x02
89ea86
+#define CMIS_MEMORY_MODEL_MASK			0x80
89ea86
 
89ea86
 #define CMIS_MODULE_TYPE_OFFSET			0x55
89ea86
 #define CMIS_MT_MMF				0x01
89ea86
-- 
89ea86
2.35.1
89ea86