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