1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU PF/VF Netdev Devlink
3 *
4 * Copyright (C) 2021 Marvell.
5 */
6
7 #include "otx2_common.h"
8
9 /* Devlink Params APIs */
otx2_dl_mcam_count_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)10 static int otx2_dl_mcam_count_validate(struct devlink *devlink, u32 id,
11 union devlink_param_value val,
12 struct netlink_ext_ack *extack)
13 {
14 struct otx2_devlink *otx2_dl = devlink_priv(devlink);
15 struct otx2_nic *pfvf = otx2_dl->pfvf;
16 struct otx2_flow_config *flow_cfg;
17
18 if (!pfvf->flow_cfg) {
19 NL_SET_ERR_MSG_MOD(extack,
20 "pfvf->flow_cfg not initialized");
21 return -EINVAL;
22 }
23
24 flow_cfg = pfvf->flow_cfg;
25 if (flow_cfg && flow_cfg->nr_flows) {
26 NL_SET_ERR_MSG_MOD(extack,
27 "Cannot modify count when there are active rules");
28 return -EINVAL;
29 }
30
31 return 0;
32 }
33
otx2_dl_mcam_count_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx,struct netlink_ext_ack * extack)34 static int otx2_dl_mcam_count_set(struct devlink *devlink, u32 id,
35 struct devlink_param_gset_ctx *ctx,
36 struct netlink_ext_ack *extack)
37 {
38 struct otx2_devlink *otx2_dl = devlink_priv(devlink);
39 struct otx2_nic *pfvf = otx2_dl->pfvf;
40
41 if (!pfvf->flow_cfg)
42 return 0;
43
44 otx2_alloc_mcam_entries(pfvf, ctx->val.vu16);
45
46 return 0;
47 }
48
otx2_dl_mcam_count_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)49 static int otx2_dl_mcam_count_get(struct devlink *devlink, u32 id,
50 struct devlink_param_gset_ctx *ctx)
51 {
52 struct otx2_devlink *otx2_dl = devlink_priv(devlink);
53 struct otx2_nic *pfvf = otx2_dl->pfvf;
54 struct otx2_flow_config *flow_cfg;
55
56 if (!pfvf->flow_cfg) {
57 ctx->val.vu16 = 0;
58 return 0;
59 }
60
61 flow_cfg = pfvf->flow_cfg;
62 ctx->val.vu16 = flow_cfg->max_flows;
63
64 return 0;
65 }
66
otx2_dl_ucast_flt_cnt_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx,struct netlink_ext_ack * extack)67 static int otx2_dl_ucast_flt_cnt_set(struct devlink *devlink, u32 id,
68 struct devlink_param_gset_ctx *ctx,
69 struct netlink_ext_ack *extack)
70 {
71 struct otx2_devlink *otx2_dl = devlink_priv(devlink);
72 struct otx2_nic *pfvf = otx2_dl->pfvf;
73 int err;
74
75 pfvf->flow_cfg->ucast_flt_cnt = ctx->val.vu8;
76
77 otx2_mcam_flow_del(pfvf);
78 err = otx2_mcam_entry_init(pfvf);
79 if (err)
80 return err;
81
82 return 0;
83 }
84
otx2_dl_ucast_flt_cnt_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)85 static int otx2_dl_ucast_flt_cnt_get(struct devlink *devlink, u32 id,
86 struct devlink_param_gset_ctx *ctx)
87 {
88 struct otx2_devlink *otx2_dl = devlink_priv(devlink);
89 struct otx2_nic *pfvf = otx2_dl->pfvf;
90
91 ctx->val.vu8 = pfvf->flow_cfg ? pfvf->flow_cfg->ucast_flt_cnt : 0;
92
93 return 0;
94 }
95
otx2_dl_ucast_flt_cnt_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)96 static int otx2_dl_ucast_flt_cnt_validate(struct devlink *devlink, u32 id,
97 union devlink_param_value val,
98 struct netlink_ext_ack *extack)
99 {
100 struct otx2_devlink *otx2_dl = devlink_priv(devlink);
101 struct otx2_nic *pfvf = otx2_dl->pfvf;
102
103 /* Check for UNICAST filter support*/
104 if (!(pfvf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT)) {
105 NL_SET_ERR_MSG_MOD(extack,
106 "Unicast filter not enabled");
107 return -EINVAL;
108 }
109
110 if (!pfvf->flow_cfg) {
111 NL_SET_ERR_MSG_MOD(extack,
112 "pfvf->flow_cfg not initialized");
113 return -EINVAL;
114 }
115
116 if (pfvf->flow_cfg->nr_flows) {
117 NL_SET_ERR_MSG_MOD(extack,
118 "Cannot modify count when there are active rules");
119 return -EINVAL;
120 }
121
122 return 0;
123 }
124
125 enum otx2_dl_param_id {
126 OTX2_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
127 OTX2_DEVLINK_PARAM_ID_MCAM_COUNT,
128 OTX2_DEVLINK_PARAM_ID_UCAST_FLT_CNT,
129 };
130
131 static const struct devlink_param otx2_dl_params[] = {
132 DEVLINK_PARAM_DRIVER(OTX2_DEVLINK_PARAM_ID_MCAM_COUNT,
133 "mcam_count", DEVLINK_PARAM_TYPE_U16,
134 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
135 otx2_dl_mcam_count_get, otx2_dl_mcam_count_set,
136 otx2_dl_mcam_count_validate),
137 DEVLINK_PARAM_DRIVER(OTX2_DEVLINK_PARAM_ID_UCAST_FLT_CNT,
138 "unicast_filter_count", DEVLINK_PARAM_TYPE_U8,
139 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
140 otx2_dl_ucast_flt_cnt_get, otx2_dl_ucast_flt_cnt_set,
141 otx2_dl_ucast_flt_cnt_validate),
142 };
143
144 static const struct devlink_ops otx2_devlink_ops = {
145 };
146
otx2_register_dl(struct otx2_nic * pfvf)147 int otx2_register_dl(struct otx2_nic *pfvf)
148 {
149 struct otx2_devlink *otx2_dl;
150 struct devlink *dl;
151 int err;
152
153 dl = devlink_alloc(&otx2_devlink_ops,
154 sizeof(struct otx2_devlink), pfvf->dev);
155 if (!dl) {
156 dev_warn(pfvf->dev, "devlink_alloc failed\n");
157 return -ENOMEM;
158 }
159
160 otx2_dl = devlink_priv(dl);
161 otx2_dl->dl = dl;
162 otx2_dl->pfvf = pfvf;
163 pfvf->dl = otx2_dl;
164
165 err = devlink_params_register(dl, otx2_dl_params,
166 ARRAY_SIZE(otx2_dl_params));
167 if (err) {
168 dev_err(pfvf->dev,
169 "devlink params register failed with error %d", err);
170 goto err_dl;
171 }
172
173 devlink_register(dl);
174 return 0;
175
176 err_dl:
177 devlink_free(dl);
178 return err;
179 }
180 EXPORT_SYMBOL(otx2_register_dl);
181
otx2_unregister_dl(struct otx2_nic * pfvf)182 void otx2_unregister_dl(struct otx2_nic *pfvf)
183 {
184 struct otx2_devlink *otx2_dl = pfvf->dl;
185 struct devlink *dl = otx2_dl->dl;
186
187 devlink_unregister(dl);
188 devlink_params_unregister(dl, otx2_dl_params,
189 ARRAY_SIZE(otx2_dl_params));
190 devlink_free(dl);
191 }
192 EXPORT_SYMBOL(otx2_unregister_dl);
193