2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2020 Intel Corporation
6 Configure an entire Intel DSA instance, using idxd kernel driver, for DPDK use
16 "Used to read/write paths in a sysfs directory"
17 def __init__(self, path):
20 def read_int(self, filename):
21 "Return a value from sysfs file"
22 with open(os.path.join(self.path, filename)) as f:
23 return int(f.readline())
25 def write_values(self, values):
26 "write dictionary, where key is filename and value is value to write"
27 for filename, contents in values.items():
28 with open(os.path.join(self.path, filename), "w") as f:
29 f.write(str(contents))
32 def reset_device(dsa_id):
33 "Reset the DSA device and all its queues"
34 drv_dir = SysfsDir("/sys/bus/dsa/drivers/dsa")
35 drv_dir.write_values({"unbind": f"dsa{dsa_id}"})
39 "Search for the sysfs directory of the PCI device"
40 base_dir = '/sys/bus/pci/devices/'
41 for path, dirs, files in os.walk(base_dir):
44 return os.path.join(base_dir, dir)
45 sys.exit(f"Could not find sysfs directory for device {pci}")
49 "Get the DSA instance ID using the PCI address of the device"
50 pci_dir = get_pci_dir(pci)
51 for path, dirs, files in os.walk(pci_dir):
53 if dir.startswith('dsa') and 'wq' not in dir:
55 sys.exit(f"Could not get device ID for device {pci}")
58 def configure_dsa(dsa_id, queues, prefix):
59 "Configure the DSA instance with appropriate number of queues"
60 dsa_dir = SysfsDir(f"/sys/bus/dsa/devices/dsa{dsa_id}")
61 drv_dir = SysfsDir("/sys/bus/dsa/drivers/dsa")
63 max_groups = dsa_dir.read_int("max_groups")
64 max_engines = dsa_dir.read_int("max_engines")
65 max_queues = dsa_dir.read_int("max_work_queues")
66 max_work_queues_size = dsa_dir.read_int("max_work_queues_size")
68 nb_queues = min(queues, max_queues)
69 if queues > nb_queues:
70 print(f"Setting number of queues to max supported value: {max_queues}")
72 # we want one engine per group, and no more engines than queues
73 nb_groups = min(max_engines, max_groups, nb_queues)
74 for grp in range(nb_groups):
75 dsa_dir.write_values({f"engine{dsa_id}.{grp}/group_id": grp})
77 # configure each queue
78 for q in range(nb_queues):
79 wq_dir = SysfsDir(os.path.join(dsa_dir.path, f"wq{dsa_id}.{q}"))
80 wq_dir.write_values({"group_id": q % nb_groups,
83 "name": f"{prefix}_wq{dsa_id}.{q}",
85 "size": int(max_work_queues_size / nb_queues)})
87 # enable device and then queues
88 drv_dir.write_values({"bind": f"dsa{dsa_id}"})
89 for q in range(nb_queues):
90 drv_dir.write_values({"bind": f"wq{dsa_id}.{q}"})
94 "Main function, does arg parsing and calls config function"
95 arg_p = argparse.ArgumentParser(
96 description="Configure whole DSA device instance for DPDK use")
97 arg_p.add_argument('dsa_id',
98 help="Specify DSA instance either via DSA instance number or PCI address")
99 arg_p.add_argument('-q', metavar='queues', type=int, default=255,
100 help="Number of queues to set up")
101 arg_p.add_argument('--name-prefix', metavar='prefix', dest='prefix',
103 help="Prefix for workqueue name to mark for DPDK use [default: 'dpdk']")
104 arg_p.add_argument('--reset', action='store_true',
105 help="Reset DSA device and its queues")
106 parsed_args = arg_p.parse_args(args[1:])
108 dsa_id = parsed_args.dsa_id
109 dsa_id = get_dsa_id(dsa_id) if ':' in dsa_id else dsa_id
110 if parsed_args.reset:
113 configure_dsa(dsa_id, parsed_args.q, parsed_args.prefix)
116 if __name__ == "__main__":