89ea86
From c77b5adfb3f94762a08554d1b4d75f6cbd8a6abe Mon Sep 17 00:00:00 2001
89ea86
From: Ido Schimmel <idosch@nvidia.com>
89ea86
Date: Tue, 12 Oct 2021 16:25:17 +0300
89ea86
Subject: [PATCH 18/35] sff-8636: Initialize SFF-8636 memory map
89ea86
89ea86
The SFF-8636 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 page number and an offset between 128 and 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, when
89ea86
needed.
89ea86
89ea86
[1] SFF-8636 Rev. 2.10a, pag. 30, section 6.1, Figure 6-1
89ea86
89ea86
Signed-off-by: Ido Schimmel <idosch@nvidia.com>
89ea86
---
89ea86
 qsfp.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
89ea86
 1 file changed, 65 insertions(+)
89ea86
89ea86
diff --git a/qsfp.c b/qsfp.c
89ea86
index dc6407d3ef6f..80000d40f6e8 100644
89ea86
--- a/qsfp.c
89ea86
+++ b/qsfp.c
89ea86
@@ -60,6 +60,15 @@
89ea86
 #include "qsfp.h"
89ea86
 #include "cmis.h"
89ea86
 
89ea86
+struct sff8636_memory_map {
89ea86
+	const __u8 *lower_memory;
89ea86
+	const __u8 *upper_memory[4];
89ea86
+#define page_00h upper_memory[0x0]
89ea86
+#define page_03h upper_memory[0x3]
89ea86
+};
89ea86
+
89ea86
+#define SFF8636_PAGE_SIZE	0x80
89ea86
+
89ea86
 #define MAX_DESC_SIZE	42
89ea86
 
89ea86
 static struct sff8636_aw_flags {
89ea86
@@ -853,13 +862,40 @@ static void sff8636_show_page_zero(const __u8 *id)
89ea86
 
89ea86
 }
89ea86
 
89ea86
+static void sff8636_memory_map_init_buf(struct sff8636_memory_map *map,
89ea86
+					const __u8 *id, __u32 eeprom_len)
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 03h is only present when the module memory model is paged and
89ea86
+	 * not flat and when we got a big enough buffer from the kernel.
89ea86
+	 */
89ea86
+	if (map->lower_memory[SFF8636_STATUS_2_OFFSET] &
89ea86
+	    SFF8636_STATUS_PAGE_3_PRESENT ||
89ea86
+	    eeprom_len != ETH_MODULE_SFF_8636_MAX_LEN)
89ea86
+		return;
89ea86
+
89ea86
+	map->page_03h = id + 3 * SFF8636_PAGE_SIZE;
89ea86
+}
89ea86
+
89ea86
 void sff8636_show_all_ioctl(const __u8 *id, __u32 eeprom_len)
89ea86
 {
89ea86
+	struct sff8636_memory_map map = {};
89ea86
+
89ea86
 	if (id[SFF8636_ID_OFFSET] == SFF8024_ID_QSFP_DD) {
89ea86
 		cmis_show_all_ioctl(id);
89ea86
 		return;
89ea86
 	}
89ea86
 
89ea86
+	sff8636_memory_map_init_buf(&map, id, eeprom_len);
89ea86
+
89ea86
 	sff8636_show_identifier(id);
89ea86
 	switch (id[SFF8636_ID_OFFSET]) {
89ea86
 	case SFF8024_ID_QSFP:
89ea86
@@ -871,9 +907,38 @@ void sff8636_show_all_ioctl(const __u8 *id, __u32 eeprom_len)
89ea86
 	}
89ea86
 }
89ea86
 
89ea86
+static void
89ea86
+sff8636_memory_map_init_pages(struct sff8636_memory_map *map,
89ea86
+			      const struct ethtool_module_eeprom *page_zero,
89ea86
+			      const struct ethtool_module_eeprom *page_three)
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 03h is only present when the module memory model is paged and
89ea86
+	 * not flat.
89ea86
+	 */
89ea86
+	if (map->lower_memory[SFF8636_STATUS_2_OFFSET] &
89ea86
+	    SFF8636_STATUS_PAGE_3_PRESENT)
89ea86
+		return;
89ea86
+
89ea86
+	map->page_03h = page_three->data - SFF8636_PAGE_SIZE;
89ea86
+}
89ea86
+
89ea86
 void sff8636_show_all_nl(const struct ethtool_module_eeprom *page_zero,
89ea86
 			 const struct ethtool_module_eeprom *page_three)
89ea86
 {
89ea86
+	struct sff8636_memory_map map = {};
89ea86
+
89ea86
+	sff8636_memory_map_init_pages(&map, page_zero, page_three);
89ea86
+
89ea86
 	sff8636_show_identifier(page_zero->data);
89ea86
 	sff8636_show_page_zero(page_zero->data);
89ea86
 	if (page_three)
89ea86
-- 
89ea86
2.35.1
89ea86