• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
3  * Copyright 2020 NXP Semiconductors
4  */
5 #include "sja1105.h"
6 
7 /* Since devlink regions have a fixed size and the static config has a variable
8  * size, we need to calculate the maximum possible static config size by
9  * creating a dummy config with all table entries populated to the max, and get
10  * its packed length. This is done dynamically as opposed to simply hardcoding
11  * a number, since currently not all static config tables are implemented, so
12  * we are avoiding a possible code desynchronization.
13  */
sja1105_static_config_get_max_size(struct sja1105_private * priv)14 static size_t sja1105_static_config_get_max_size(struct sja1105_private *priv)
15 {
16 	struct sja1105_static_config config;
17 	enum sja1105_blk_idx blk_idx;
18 	int rc;
19 
20 	rc = sja1105_static_config_init(&config,
21 					priv->info->static_ops,
22 					priv->info->device_id);
23 	if (rc)
24 		return 0;
25 
26 	for (blk_idx = 0; blk_idx < BLK_IDX_MAX; blk_idx++) {
27 		struct sja1105_table *table = &config.tables[blk_idx];
28 
29 		table->entry_count = table->ops->max_entry_count;
30 	}
31 
32 	return sja1105_static_config_get_length(&config);
33 }
34 
35 static int
sja1105_region_static_config_snapshot(struct devlink * dl,const struct devlink_region_ops * ops,struct netlink_ext_ack * extack,u8 ** data)36 sja1105_region_static_config_snapshot(struct devlink *dl,
37 				      const struct devlink_region_ops *ops,
38 				      struct netlink_ext_ack *extack,
39 				      u8 **data)
40 {
41 	struct dsa_switch *ds = dsa_devlink_to_ds(dl);
42 	struct sja1105_private *priv = ds->priv;
43 	size_t max_len, len;
44 
45 	len = sja1105_static_config_get_length(&priv->static_config);
46 	max_len = sja1105_static_config_get_max_size(priv);
47 
48 	*data = kcalloc(max_len, sizeof(u8), GFP_KERNEL);
49 	if (!*data)
50 		return -ENOMEM;
51 
52 	return static_config_buf_prepare_for_upload(priv, *data, len);
53 }
54 
55 static struct devlink_region_ops sja1105_region_static_config_ops = {
56 	.name = "static-config",
57 	.snapshot = sja1105_region_static_config_snapshot,
58 	.destructor = kfree,
59 };
60 
61 enum sja1105_region_id {
62 	SJA1105_REGION_STATIC_CONFIG = 0,
63 };
64 
65 struct sja1105_region {
66 	const struct devlink_region_ops *ops;
67 	size_t (*get_size)(struct sja1105_private *priv);
68 };
69 
70 static struct sja1105_region sja1105_regions[] = {
71 	[SJA1105_REGION_STATIC_CONFIG] = {
72 		.ops = &sja1105_region_static_config_ops,
73 		.get_size = sja1105_static_config_get_max_size,
74 	},
75 };
76 
sja1105_setup_devlink_regions(struct dsa_switch * ds)77 static int sja1105_setup_devlink_regions(struct dsa_switch *ds)
78 {
79 	int i, num_regions = ARRAY_SIZE(sja1105_regions);
80 	struct sja1105_private *priv = ds->priv;
81 	const struct devlink_region_ops *ops;
82 	struct devlink_region *region;
83 	u64 size;
84 
85 	priv->regions = kcalloc(num_regions, sizeof(struct devlink_region *),
86 				GFP_KERNEL);
87 	if (!priv->regions)
88 		return -ENOMEM;
89 
90 	for (i = 0; i < num_regions; i++) {
91 		size = sja1105_regions[i].get_size(priv);
92 		ops = sja1105_regions[i].ops;
93 
94 		region = dsa_devlink_region_create(ds, ops, 1, size);
95 		if (IS_ERR(region)) {
96 			while (--i >= 0)
97 				dsa_devlink_region_destroy(priv->regions[i]);
98 
99 			kfree(priv->regions);
100 			return PTR_ERR(region);
101 		}
102 
103 		priv->regions[i] = region;
104 	}
105 
106 	return 0;
107 }
108 
sja1105_teardown_devlink_regions(struct dsa_switch * ds)109 static void sja1105_teardown_devlink_regions(struct dsa_switch *ds)
110 {
111 	int i, num_regions = ARRAY_SIZE(sja1105_regions);
112 	struct sja1105_private *priv = ds->priv;
113 
114 	for (i = 0; i < num_regions; i++)
115 		dsa_devlink_region_destroy(priv->regions[i]);
116 
117 	kfree(priv->regions);
118 }
119 
sja1105_best_effort_vlan_filtering_get(struct sja1105_private * priv,bool * be_vlan)120 static int sja1105_best_effort_vlan_filtering_get(struct sja1105_private *priv,
121 						  bool *be_vlan)
122 {
123 	*be_vlan = priv->best_effort_vlan_filtering;
124 
125 	return 0;
126 }
127 
sja1105_best_effort_vlan_filtering_set(struct sja1105_private * priv,bool be_vlan)128 static int sja1105_best_effort_vlan_filtering_set(struct sja1105_private *priv,
129 						  bool be_vlan)
130 {
131 	struct dsa_switch *ds = priv->ds;
132 	bool vlan_filtering;
133 	int port;
134 	int rc;
135 
136 	priv->best_effort_vlan_filtering = be_vlan;
137 
138 	rtnl_lock();
139 	for (port = 0; port < ds->num_ports; port++) {
140 		struct switchdev_trans trans;
141 		struct dsa_port *dp;
142 
143 		if (!dsa_is_user_port(ds, port))
144 			continue;
145 
146 		dp = dsa_to_port(ds, port);
147 		vlan_filtering = dsa_port_is_vlan_filtering(dp);
148 
149 		trans.ph_prepare = true;
150 		rc = sja1105_vlan_filtering(ds, port, vlan_filtering, &trans);
151 		if (rc)
152 			break;
153 
154 		trans.ph_prepare = false;
155 		rc = sja1105_vlan_filtering(ds, port, vlan_filtering, &trans);
156 		if (rc)
157 			break;
158 	}
159 	rtnl_unlock();
160 
161 	return rc;
162 }
163 
164 enum sja1105_devlink_param_id {
165 	SJA1105_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
166 	SJA1105_DEVLINK_PARAM_ID_BEST_EFFORT_VLAN_FILTERING,
167 };
168 
sja1105_devlink_param_get(struct dsa_switch * ds,u32 id,struct devlink_param_gset_ctx * ctx)169 int sja1105_devlink_param_get(struct dsa_switch *ds, u32 id,
170 			      struct devlink_param_gset_ctx *ctx)
171 {
172 	struct sja1105_private *priv = ds->priv;
173 	int err;
174 
175 	switch (id) {
176 	case SJA1105_DEVLINK_PARAM_ID_BEST_EFFORT_VLAN_FILTERING:
177 		err = sja1105_best_effort_vlan_filtering_get(priv,
178 							     &ctx->val.vbool);
179 		break;
180 	default:
181 		err = -EOPNOTSUPP;
182 		break;
183 	}
184 
185 	return err;
186 }
187 
sja1105_devlink_param_set(struct dsa_switch * ds,u32 id,struct devlink_param_gset_ctx * ctx)188 int sja1105_devlink_param_set(struct dsa_switch *ds, u32 id,
189 			      struct devlink_param_gset_ctx *ctx)
190 {
191 	struct sja1105_private *priv = ds->priv;
192 	int err;
193 
194 	switch (id) {
195 	case SJA1105_DEVLINK_PARAM_ID_BEST_EFFORT_VLAN_FILTERING:
196 		err = sja1105_best_effort_vlan_filtering_set(priv,
197 							     ctx->val.vbool);
198 		break;
199 	default:
200 		err = -EOPNOTSUPP;
201 		break;
202 	}
203 
204 	return err;
205 }
206 
207 static const struct devlink_param sja1105_devlink_params[] = {
208 	DSA_DEVLINK_PARAM_DRIVER(SJA1105_DEVLINK_PARAM_ID_BEST_EFFORT_VLAN_FILTERING,
209 				 "best_effort_vlan_filtering",
210 				 DEVLINK_PARAM_TYPE_BOOL,
211 				 BIT(DEVLINK_PARAM_CMODE_RUNTIME)),
212 };
213 
sja1105_setup_devlink_params(struct dsa_switch * ds)214 static int sja1105_setup_devlink_params(struct dsa_switch *ds)
215 {
216 	return dsa_devlink_params_register(ds, sja1105_devlink_params,
217 					   ARRAY_SIZE(sja1105_devlink_params));
218 }
219 
sja1105_teardown_devlink_params(struct dsa_switch * ds)220 static void sja1105_teardown_devlink_params(struct dsa_switch *ds)
221 {
222 	dsa_devlink_params_unregister(ds, sja1105_devlink_params,
223 				      ARRAY_SIZE(sja1105_devlink_params));
224 }
225 
sja1105_devlink_info_get(struct dsa_switch * ds,struct devlink_info_req * req,struct netlink_ext_ack * extack)226 int sja1105_devlink_info_get(struct dsa_switch *ds,
227 			     struct devlink_info_req *req,
228 			     struct netlink_ext_ack *extack)
229 {
230 	struct sja1105_private *priv = ds->priv;
231 	int rc;
232 
233 	rc = devlink_info_driver_name_put(req, "sja1105");
234 	if (rc)
235 		return rc;
236 
237 	rc = devlink_info_version_fixed_put(req,
238 					    DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
239 					    priv->info->name);
240 	return rc;
241 }
242 
sja1105_devlink_setup(struct dsa_switch * ds)243 int sja1105_devlink_setup(struct dsa_switch *ds)
244 {
245 	int rc;
246 
247 	rc = sja1105_setup_devlink_params(ds);
248 	if (rc)
249 		return rc;
250 
251 	rc = sja1105_setup_devlink_regions(ds);
252 	if (rc < 0) {
253 		sja1105_teardown_devlink_params(ds);
254 		return rc;
255 	}
256 
257 	return 0;
258 }
259 
sja1105_devlink_teardown(struct dsa_switch * ds)260 void sja1105_devlink_teardown(struct dsa_switch *ds)
261 {
262 	sja1105_teardown_devlink_params(ds);
263 	sja1105_teardown_devlink_regions(ds);
264 }
265