Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mellanox][pcied] Ignore bus on pcie.yaml for Mellanox switches #8063

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions platform/mellanox/mlnx-platform-api/sonic_platform/pcie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
########################################################################
#
# Copyright (C) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Module contains a platform specific implementation of SONiC Platform
# Base PCIe class
#
########################################################################
import os
import re

try:
from sonic_platform_base.sonic_pcie.pcie_common import PcieUtil
except ImportError as e:
raise ImportError(str(e) + "- required module not found")

SYSFS_PCI_DEVICE_PATH = '/sys/bus/pci/devices/'


class Pcie(PcieUtil):
# check the current PCIe device with config file and return the result
# use bus from _device_id_to_bus_map instead of from yaml file
def get_pcie_check(self):
self.load_config_file()
for item_conf in self.confInfo:
id_conf = item_conf["id"]
dev_conf = item_conf["dev"]
fn_conf = item_conf["fn"]
bus_conf = self._device_id_to_bus_map.get(id_conf)
if bus_conf and self.check_pcie_sysfs(bus=int(bus_conf, base=16), device=int(dev_conf, base=16),
func=int(fn_conf, base=16)):
item_conf["result"] = "Passed"
else:
item_conf["result"] = "Failed"
return self.confInfo

# Create
def _create_device_id_to_bus_map(self):
self._device_id_to_bus_map = {}
self.load_config_file()
device_folders = os.listdir(SYSFS_PCI_DEVICE_PATH)
for folder in device_folders:
# For each folder in the sysfs tree we check if it matches the normal PCIe device folder pattern,
# If match we add the device id from the device file and the bus from the folder name to the map
#
# Example for device folder name: 0000:ff:0b.1
#
# The folder name is built from:
# 4 hex digit of domain
# colon ':'
# 2 hex digit of bus - this is what we are looking for
# colon ':'
# 2 hex digit of id
# dot '.'
# 1 digit of fn
pattern_for_device_folder = re.search('....:(..):..\..', folder)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

....:(..):....

This regex is hard to read. Could you give some examples and explanations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example for a dir name is: 0000:ff:0b.1
The name of the folder is created from: {4 digit of domain - always 0}:{2 hex digit of bus}:{2 hex digit - dev}.{fn}

You can see this pattern of the folder name also here:
https://github.com/Azure/sonic-platform-common/blob/87c81de7193ee68568543d982fb8ba38f5e4ad07/sonic_platform_base/sonic_pcie/pcie_common.py#L106

I use this regex to look for the bus (in the above example 'ff').
The dots match any character, so I am checking if the folder that starts with 4 characters (domain), colon, 2 characters of bus (they are in parentheses to create the group I am looking for), a colon, 2 characters of dev, a dot, and the fn.

I tried to create the simplest regex, but I would be glad to hear if you have a different suggestion

if pattern_for_device_folder:
bus = pattern_for_device_folder.group(1)
with open(os.path.join('/sys/bus/pci/devices', folder, 'device'), 'r') as device_file:
# The 'device' file contain an hex repesantaion of the id key in the yaml file.
# Example of the file contact:
# 0x6fe2
# We will strip the new line character, and remove the 0x prefix that is not needed.
device_id = device_file.read().strip().replace('0x', '')
self._device_id_to_bus_map[device_id] = bus

def __init__(self, platform_path):
PcieUtil.__init__(self, platform_path)
self._create_device_id_to_bus_map()