net/virtio: fix incorrect cast of void *
[dpdk.git] / ioat / dpdk_idxd_cfg.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2020 Intel Corporation
4
5 """
6 Configure an entire Intel DSA instance, using idxd kernel driver, for DPDK use
7 """
8
9 import sys
10 import argparse
11 import os
12 import os.path
13
14
15 class SysfsDir:
16     "Used to read/write paths in a sysfs directory"
17     def __init__(self, path):
18         self.path = path
19
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())
24
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))
30
31
32 def configure_dsa(dsa_id, queues, prefix):
33     "Configure the DSA instance with appropriate number of queues"
34     dsa_dir = SysfsDir(f"/sys/bus/dsa/devices/dsa{dsa_id}")
35     drv_dir = SysfsDir("/sys/bus/dsa/drivers/dsa")
36
37     max_groups = dsa_dir.read_int("max_groups")
38     max_engines = dsa_dir.read_int("max_engines")
39     max_queues = dsa_dir.read_int("max_work_queues")
40     max_tokens = dsa_dir.read_int("max_tokens")
41
42     nb_queues = min(queues, max_queues)
43     if queues > nb_queues:
44         print(f"Setting number of queues to max supported value: {max_queues}")
45
46     # we want one engine per group, and no more engines than queues
47     nb_groups = min(max_engines, max_groups, nb_queues)
48     for grp in range(nb_groups):
49         dsa_dir.write_values({f"engine{dsa_id}.{grp}/group_id": grp})
50
51     # configure each queue
52     for q in range(nb_queues):
53         wq_dir = SysfsDir(os.path.join(dsa_dir.path, f"wq{dsa_id}.{q}"))
54         wq_dir.write_values({"group_id": q % nb_groups,
55                              "type": "user",
56                              "mode": "dedicated",
57                              "name": f"{prefix}_wq{dsa_id}.{q}",
58                              "priority": 1,
59                              "size": int(max_tokens / nb_queues)})
60
61     # enable device and then queues
62     drv_dir.write_values({"bind": f"dsa{dsa_id}"})
63     for q in range(nb_queues):
64         drv_dir.write_values({"bind": f"wq{dsa_id}.{q}"})
65
66
67 def main(args):
68     "Main function, does arg parsing and calls config function"
69     arg_p = argparse.ArgumentParser(
70         description="Configure whole DSA device instance for DPDK use")
71     arg_p.add_argument('dsa_id', type=int, help="DSA instance number")
72     arg_p.add_argument('-q', metavar='queues', type=int, default=255,
73                        help="Number of queues to set up")
74     arg_p.add_argument('--name-prefix', metavar='prefix', dest='prefix',
75                        default="dpdk",
76                        help="Prefix for workqueue name to mark for DPDK use [default: 'dpdk']")
77     parsed_args = arg_p.parse_args(args[1:])
78     configure_dsa(parsed_args.dsa_id, parsed_args.q, parsed_args.prefix)
79
80
81 if __name__ == "__main__":
82     main(sys.argv)