1{ config, lib, pkgs, ... }:
2
3let
4 cfg = config.services.rshim;
5
6 rshimCommand = [ "${cfg.package}/bin/rshim" ]
7 ++ lib.optionals (cfg.backend != null) [ "--backend ${cfg.backend}" ]
8 ++ lib.optionals (cfg.device != null) [ "--device ${cfg.device}" ]
9 ++ lib.optionals (cfg.index != null) [ "--index ${builtins.toString cfg.index}" ]
10 ++ [ "--log-level ${builtins.toString cfg.log-level}" ]
11 ;
12in
13{
14 options.services.rshim = {
15 enable = lib.mkEnableOption (lib.mdDoc "User-space rshim driver for the BlueField SoC");
16
17 package = lib.mkPackageOptionMD pkgs "rshim-user-space" { };
18
19 backend = lib.mkOption {
20 type = with lib.types; nullOr (enum [ "usb" "pcie" "pcie_lf" ]);
21 description = lib.mdDoc ''
22 Specify the backend to attach. If not specified, the driver will scan
23 all rshim backends unless the `device` option is given with a device
24 name specified.
25 '';
26 default = null;
27 example = "pcie";
28 };
29
30 device = lib.mkOption {
31 type = with lib.types; nullOr str;
32 description = lib.mdDoc ''
33 Specify the device name to attach. The backend driver can be deduced
34 from the device name, thus the `backend` option is not needed.
35 '';
36 default = null;
37 example = "pcie-04:00.2";
38 };
39
40 index = lib.mkOption {
41 type = with lib.types; nullOr int;
42 description = lib.mdDoc ''
43 Specify the index to create device path `/dev/rshim<index>`. It's also
44 used to create network interface name `tmfifo_net<index>`. This option
45 is needed when multiple rshim instances are running.
46 '';
47 default = null;
48 example = 1;
49 };
50
51 log-level = lib.mkOption {
52 type = lib.types.int;
53 description = lib.mdDoc ''
54 Specify the log level (0:none, 1:error, 2:warning, 3:notice, 4:debug).
55 '';
56 default = 2;
57 example = 4;
58 };
59
60 config = lib.mkOption {
61 type = with lib.types; attrsOf (oneOf [ int str ]);
62 description = lib.mdDoc ''
63 Structural setting for the rshim configuration file
64 (`/etc/rshim.conf`). It can be used to specify the static mapping
65 between rshim devices and rshim names. It can also be used to ignore
66 some rshim devices.
67 '';
68 default = { };
69 example = {
70 DISPLAY_LEVEL = 0;
71 rshim0 = "usb-2-1.7";
72 none = "usb-1-1.4";
73 };
74 };
75 };
76
77 config = lib.mkIf cfg.enable {
78 environment.etc = lib.mkIf (cfg.config != { }) {
79 "rshim.conf".text = lib.generators.toKeyValue
80 { mkKeyValue = lib.generators.mkKeyValueDefault { } " "; }
81 cfg.config;
82 };
83
84 systemd.services.rshim = {
85 after = [ "network.target" ];
86 serviceConfig = {
87 Restart = "always";
88 Type = "forking";
89 ExecStart = [
90 (lib.concatStringsSep " \\\n" rshimCommand)
91 ];
92 KillMode = "control-group";
93 };
94 wantedBy = [ "multi-user.target" ];
95 };
96 };
97
98 meta.maintainers = with lib.maintainers; [ nikstur ];
99}