Skip to content

Commit

Permalink
[202311] Support reading/writing module EEPROM data by page and offset (
Browse files Browse the repository at this point in the history
#3008) (#3073)

* Support reading/writing module EEPROM data by page and offset (#3008)
* Support reading/writing module EEPROM data by page and offset
  • Loading branch information
Junchao-Mellanox authored Jan 19, 2024
1 parent cb0fd42 commit 7a242ee
Show file tree
Hide file tree
Showing 3 changed files with 441 additions and 26 deletions.
7 changes: 4 additions & 3 deletions doc/Command-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -12942,7 +12942,8 @@ Clear MACsec counters which is to reset all MACsec counters to ZERO.
Go Back To [Beginning of the document](#) or [Beginning of this section](#macsec-commands)
# SFP Utilities Commands
This sub-section explains the list of commands available for SFP utilities feature.
This sub-section explains the list of commands available for SFP utilities feature.
## SFP Utilities show commands
Expand Down Expand Up @@ -13048,7 +13049,7 @@ EEPROM hexdump for port Ethernet8
000000f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
```
# SFP Utilities read command
## SFP Utilities read command
- Read SFP EEPROM data
Expand Down Expand Up @@ -13080,7 +13081,7 @@ admin@sonic:~$ sfputil read-eeprom --port Ethernet0 --page 0 --offset 100 --size
4a44
```
# SFP Utilities write command
## SFP Utilities write command
- Write SFP EEPROM data
Expand Down
193 changes: 172 additions & 21 deletions sfputil/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,27 +908,6 @@ def eeprom_dump_general(physical_port, page, flat_offset, size, page_offset, no_
return 0, ''.join('{:02x}'.format(x) for x in page_dump)


def eeprom_dump_general(physical_port, page, flat_offset, size, page_offset, no_format=False):
"""
Dump module EEPROM for given pages in hex format.
Args:
logical_port_name: logical port name
pages: a list of pages to be dumped. The list always include a default page list and the target_page input by
user
target_page: user input page number, optional. target_page is only for display purpose
Returns:
tuple(0, dump string) if success else tuple(error_code, error_message)
"""
sfp = platform_chassis.get_sfp(physical_port)
page_dump = sfp.read_eeprom(flat_offset, size)
if page_dump is None:
return ERROR_NOT_IMPLEMENTED, f'Error: Failed to read EEPROM for page {page:x}h, flat_offset {flat_offset}, page_offset {page_offset}, size {size}!'
if not no_format:
return 0, hexdump(EEPROM_DUMP_INDENT, page_dump, page_offset, start_newline=False)
else:
return 0, ''.join('{:02x}'.format(x) for x in page_dump)


def convert_byte_to_valid_ascii_char(byte):
if byte < 32 or 126 < byte:
return '.'
Expand Down Expand Up @@ -1712,5 +1691,177 @@ def target(port_name, target):
click.echo("Target Mode set failed!")
sys.exit(EXIT_FAIL)


# 'read-eeprom' subcommand
@cli.command()
@click.option('-p', '--port', metavar='<logical_port_name>', help="Logical port name", required=True)
@click.option('-n', '--page', metavar='<page>', type=click.IntRange(0, MAX_EEPROM_PAGE), help="EEPROM page number", required=True)
@click.option('-o', '--offset', metavar='<offset>', type=click.IntRange(0, MAX_EEPROM_OFFSET), help="EEPROM offset within the page", required=True)
@click.option('-s', '--size', metavar='<size>', type=click.IntRange(1, MAX_EEPROM_OFFSET + 1), help="Size of byte to be read", required=True)
@click.option('--no-format', is_flag=True, help="Display non formatted data")
@click.option('--wire-addr', help="Wire address of sff8472")
def read_eeprom(port, page, offset, size, no_format, wire_addr):
"""Read SFP EEPROM data
"""
try:
if platform_sfputil.is_logical_port(port) == 0:
click.echo("Error: invalid port {}".format(port))
print_all_valid_port_values()
sys.exit(ERROR_INVALID_PORT)

if is_port_type_rj45(port):
click.echo("This functionality is not applicable for RJ45 port {}.".format(port))
sys.exit(EXIT_FAIL)

physical_port = logical_port_to_physical_port_index(port)
sfp = platform_chassis.get_sfp(physical_port)
if not sfp.get_presence():
click.echo("{}: SFP EEPROM not detected\n".format(port))
sys.exit(EXIT_FAIL)

from sonic_platform_base.sonic_xcvr.api.public import sff8472
api = sfp.get_xcvr_api()
if api is None:
click.echo('Error: SFP EEPROM not detected!')
if not isinstance(api, sff8472.Sff8472Api):
overall_offset = get_overall_offset_general(api, page, offset, size)
else:
overall_offset = get_overall_offset_sff8472(api, page, offset, size, wire_addr)
return_code, output = eeprom_dump_general(physical_port, page, overall_offset, size, offset, no_format)
if return_code != 0:
click.echo("Error: Failed to read EEPROM!")
sys.exit(return_code)
click.echo(output)
except NotImplementedError:
click.echo("This functionality is currently not implemented for this platform")
sys.exit(ERROR_NOT_IMPLEMENTED)
except ValueError as e:
click.echo(f"Error: {e}")
sys.exit(EXIT_FAIL)


# 'write-eeprom' subcommand
@cli.command()
@click.option('-p', '--port', metavar='<logical_port_name>', help="Logical port name", required=True)
@click.option('-n', '--page', metavar='<page>', type=click.IntRange(0, MAX_EEPROM_PAGE), help="EEPROM page number", required=True)
@click.option('-o', '--offset', metavar='<offset>', type=click.IntRange(0, MAX_EEPROM_OFFSET), help="EEPROM offset within the page", required=True)
@click.option('-d', '--data', metavar='<data>', help="Hex string EEPROM data", required=True)
@click.option('--wire-addr', help="Wire address of sff8472")
@click.option('--verify', is_flag=True, help="Verify the data by reading back")
def write_eeprom(port, page, offset, data, wire_addr, verify):
"""Write SFP EEPROM data"""
try:
if platform_sfputil.is_logical_port(port) == 0:
click.echo("Error: invalid port {}".format(port))
print_all_valid_port_values()
sys.exit(ERROR_INVALID_PORT)

if is_port_type_rj45(port):
click.echo("This functionality is not applicable for RJ45 port {}.".format(port))
sys.exit(EXIT_FAIL)

physical_port = logical_port_to_physical_port_index(port)
sfp = platform_chassis.get_sfp(physical_port)
if not sfp.get_presence():
click.echo("{}: SFP EEPROM not detected\n".format(port))
sys.exit(EXIT_FAIL)

try:
bytes = bytearray.fromhex(data)
except ValueError:
click.echo("Error: Data must be a hex string of even length!")
sys.exit(EXIT_FAIL)

from sonic_platform_base.sonic_xcvr.api.public import sff8472
api = sfp.get_xcvr_api()
if api is None:
click.echo('Error: SFP EEPROM not detected!')
sys.exit(EXIT_FAIL)

if not isinstance(api, sff8472.Sff8472Api):
overall_offset = get_overall_offset_general(api, page, offset, len(bytes))
else:
overall_offset = get_overall_offset_sff8472(api, page, offset, len(bytes), wire_addr)
success = sfp.write_eeprom(overall_offset, len(bytes), bytes)
if not success:
click.echo("Error: Failed to write EEPROM!")
sys.exit(ERROR_NOT_IMPLEMENTED)
if verify:
read_data = sfp.read_eeprom(overall_offset, len(bytes))
if read_data != bytes:
click.echo(f"Error: Write data failed! Write: {''.join('{:02x}'.format(x) for x in bytes)}, read: {''.join('{:02x}'.format(x) for x in read_data)}")
sys.exit(EXIT_FAIL)
except NotImplementedError:
click.echo("This functionality is currently not implemented for this platform")
sys.exit(ERROR_NOT_IMPLEMENTED)
except ValueError as e:
click.echo("Error: {}".format(e))
sys.exit(EXIT_FAIL)


def get_overall_offset_general(api, page, offset, size):
"""
Validate input parameter page, offset, size and translate them to overall offset
Args:
api: cable API object
page: module EEPROM page number.
offset: module EEPROM page offset.
size: number bytes of the data to be read/write
Returns:
The overall offset
"""
if api.is_flat_memory():
if page != 0:
raise ValueError(f'Invalid page number {page}, only page 0 is supported')

if page != 0:
if offset < MIN_OFFSET_FOR_NON_PAGE0:
raise ValueError(f'Invalid offset {offset} for page {page}, valid range: [128, 255]')

if size + offset - 1 > MAX_EEPROM_OFFSET:
raise ValueError(f'Invalid size {size}, valid range: [1, {255 - offset + 1}]')

return page * PAGE_SIZE + offset


def get_overall_offset_sff8472(api, page, offset, size, wire_addr):
"""
Validate input parameter page, offset, size, wire_addr and translate them to overall offset
Args:
api: cable API object
page: module EEPROM page number.
offset: module EEPROM page offset.
size: number bytes of the data to be read/write
wire_addr: case-insensitive wire address string. Only valid for sff8472, a0h or a2h.
Returns:
The overall offset
"""
if not wire_addr:
raise ValueError("Invalid wire address for sff8472, must a0h or a2h")

is_active_cable = not api.is_copper()
valid_wire_address = ('a0h', 'a2h') if is_active_cable else ('a0h',)
wire_addr = wire_addr.lower()
if wire_addr not in valid_wire_address:
raise ValueError(f"Invalid wire address {wire_addr} for sff8472, must be {' or '.join(valid_wire_address)}")

if wire_addr == 'a0h':
if page != 0:
raise ValueError(f'Invalid page number {page} for wire address {wire_addr}, only page 0 is supported')
max_offset = MAX_OFFSET_FOR_A0H_UPPER_PAGE if is_active_cable else MAX_OFFSET_FOR_A0H_LOWER_PAGE
if offset > max_offset:
raise ValueError(f'Invalid offset {offset} for wire address {wire_addr}, valid range: [0, {max_offset}]')
if size + offset - 1 > max_offset:
raise ValueError(
f'Invalid size {size} for wire address {wire_addr}, valid range: [1, {max_offset - offset + 1}]')
return offset
else:
if size + offset - 1 > MAX_OFFSET_FOR_A2H:
raise ValueError(f'Invalid size {size} for wire address {wire_addr}, valid range: [1, {255 - offset + 1}]')
return page * PAGE_SIZE + offset + PAGE_SIZE_FOR_A0H


if __name__ == '__main__':
cli()
Loading

0 comments on commit 7a242ee

Please sign in to comment.