1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Driver
4 * Copyright(c) 2013 - 2015 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 ******************************************************************************/
27 #include <linux/configfs.h>
30 #if IS_ENABLED(CONFIG_I40E_CONFIGFS_FS)
33 * configfs structure for i40e
35 * This file adds code for configfs support for the i40e driver. This sets
36 * up a filesystem under /sys/kernel/config in which configuration changes
37 * can be made for the driver's netdevs.
39 * The initialization in this code creates the "i40e" entry in the configfs
40 * system. After that, the user needs to use mkdir to create configurations
41 * for specific netdev ports; for example "mkdir eth3". This code will verify
42 * that such a netdev exists and that it is owned by i40e.
46 struct i40e_cfgfs_vsi {
47 struct config_item item;
51 static inline struct i40e_cfgfs_vsi *to_i40e_cfgfs_vsi(struct config_item *item)
53 return item ? container_of(item, struct i40e_cfgfs_vsi, item) : NULL;
56 static struct configfs_attribute i40e_cfgfs_vsi_attr_min_bw = {
57 .ca_owner = THIS_MODULE,
59 .ca_mode = S_IRUGO | S_IWUSR,
62 static struct configfs_attribute i40e_cfgfs_vsi_attr_max_bw = {
63 .ca_owner = THIS_MODULE,
65 .ca_mode = S_IRUGO | S_IWUSR,
68 static struct configfs_attribute i40e_cfgfs_vsi_attr_commit = {
69 .ca_owner = THIS_MODULE,
71 .ca_mode = S_IRUGO | S_IWUSR,
74 static struct configfs_attribute i40e_cfgfs_vsi_attr_port_count = {
75 .ca_owner = THIS_MODULE,
77 .ca_mode = S_IRUGO | S_IWUSR,
80 static struct configfs_attribute i40e_cfgfs_vsi_attr_part_count = {
81 .ca_owner = THIS_MODULE,
82 .ca_name = "partitions",
83 .ca_mode = S_IRUGO | S_IWUSR,
86 static struct configfs_attribute *i40e_cfgfs_vsi_attrs[] = {
87 &i40e_cfgfs_vsi_attr_min_bw,
88 &i40e_cfgfs_vsi_attr_max_bw,
89 &i40e_cfgfs_vsi_attr_commit,
90 &i40e_cfgfs_vsi_attr_port_count,
91 &i40e_cfgfs_vsi_attr_part_count,
96 * i40e_cfgfs_vsi_attr_show - Show a VSI's NPAR BW partition info
97 * @item: A pointer back to the configfs item created on driver load
98 * @attr: A pointer to this item's configuration attribute
99 * @page: A pointer to the output buffer
101 static ssize_t i40e_cfgfs_vsi_attr_show(struct config_item *item,
102 struct configfs_attribute *attr,
105 struct i40e_cfgfs_vsi *i40e_cfgfs_vsi = to_i40e_cfgfs_vsi(item);
106 struct i40e_pf *pf = i40e_cfgfs_vsi->vsi->back;
109 if (i40e_cfgfs_vsi->vsi != pf->vsi[pf->lan_vsi])
112 if (strncmp(attr->ca_name, "min_bw", 6) == 0)
113 count = sprintf(page, "%s %s %d%%\n",
114 i40e_cfgfs_vsi->vsi->netdev->name,
115 (pf->npar_min_bw & I40E_ALT_BW_RELATIVE_MASK) ?
116 "Relative Min BW" : "Absolute Min BW",
117 pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK);
118 else if (strncmp(attr->ca_name, "max_bw", 6) == 0)
119 count = sprintf(page, "%s %s %d%%\n",
120 i40e_cfgfs_vsi->vsi->netdev->name,
121 (pf->npar_max_bw & I40E_ALT_BW_RELATIVE_MASK) ?
122 "Relative Max BW" : "Absolute Max BW",
123 pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK);
124 else if (strncmp(attr->ca_name, "ports", 5) == 0)
125 count = sprintf(page, "%d\n",
127 else if (strncmp(attr->ca_name, "partitions", 10) == 0)
128 count = sprintf(page, "%d\n",
129 pf->hw.num_partitions);
137 * i40e_cfgfs_vsi_attr_store - Show a VSI's NPAR BW partition info
138 * @item: A pointer back to the configfs item created on driver load
139 * @attr: A pointer to this item's configuration attribute
140 * @page: A pointer to the user input buffer holding the user input values
142 static ssize_t i40e_cfgfs_vsi_attr_store(struct config_item *item,
143 struct configfs_attribute *attr,
144 const char *page, size_t count)
146 struct i40e_cfgfs_vsi *i40e_cfgfs_vsi = to_i40e_cfgfs_vsi(item);
147 struct i40e_pf *pf = i40e_cfgfs_vsi->vsi->back;
148 char *p = (char *)page;
152 if (i40e_cfgfs_vsi->vsi != pf->vsi[pf->lan_vsi])
155 if (!p || (*p && (*p == '\n')))
158 rc = kstrtoul(p, 10, &tmp);
164 if (strncmp(attr->ca_name, "min_bw", 6) == 0) {
165 if (tmp > (pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK))
167 /* Preserve the valid and relative BW bits - the rest is
170 pf->npar_min_bw &= (I40E_ALT_BW_RELATIVE_MASK |
171 I40E_ALT_BW_VALID_MASK);
172 pf->npar_min_bw |= (tmp & I40E_ALT_BW_VALUE_MASK);
173 i40e_set_npar_bw_setting(pf);
174 } else if (strncmp(attr->ca_name, "max_bw", 6) == 0) {
176 tmp < (pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK))
178 /* Preserve the valid and relative BW bits - the rest is
181 pf->npar_max_bw &= (I40E_ALT_BW_RELATIVE_MASK |
182 I40E_ALT_BW_VALID_MASK);
183 pf->npar_max_bw |= (tmp & I40E_ALT_BW_VALUE_MASK);
184 i40e_set_npar_bw_setting(pf);
185 } else if (strncmp(attr->ca_name, "commit", 6) == 0 && tmp == 1) {
186 if (i40e_commit_npar_bw_setting(pf))
194 * i40e_cfgfs_vsi_release - Free up the configuration item memory
195 * @item: A pointer back to the configfs item created on driver load
197 static void i40e_cfgfs_vsi_release(struct config_item *item)
199 kfree(to_i40e_cfgfs_vsi(item));
202 static struct configfs_item_operations i40e_cfgfs_vsi_item_ops = {
203 .release = i40e_cfgfs_vsi_release,
204 .show_attribute = i40e_cfgfs_vsi_attr_show,
205 .store_attribute = i40e_cfgfs_vsi_attr_store,
208 static struct config_item_type i40e_cfgfs_vsi_type = {
209 .ct_item_ops = &i40e_cfgfs_vsi_item_ops,
210 .ct_attrs = i40e_cfgfs_vsi_attrs,
211 .ct_owner = THIS_MODULE,
214 struct i40e_cfgfs_group {
215 struct config_group group;
219 * to_i40e_cfgfs_group - Get the group pointer from the config item
220 * @item: A pointer back to the configfs item created on driver load
222 static inline struct i40e_cfgfs_group *
223 to_i40e_cfgfs_group(struct config_item *item)
225 return item ? container_of(to_config_group(item),
226 struct i40e_cfgfs_group, group) : NULL;
230 * i40e_cfgfs_group_make_item - Create the configfs item with group container
231 * @group: A pointer to our configfs group
232 * @name: A pointer to the nume of the device we're looking for
234 static struct config_item *
235 i40e_cfgfs_group_make_item(struct config_group *group, const char *name)
237 struct i40e_cfgfs_vsi *i40e_cfgfs_vsi;
238 struct net_device *netdev;
239 struct i40e_netdev_priv *np;
241 read_lock(&dev_base_lock);
242 netdev = first_net_device(&init_net);
244 if (strncmp(netdev->name, name, sizeof(netdev->name)) == 0)
246 netdev = next_net_device(netdev);
248 read_unlock(&dev_base_lock);
251 return ERR_PTR(-ENODEV);
253 /* is this netdev owned by i40e? */
254 if (netdev->netdev_ops->ndo_open != i40e_open)
255 return ERR_PTR(-EACCES);
257 i40e_cfgfs_vsi = kzalloc(sizeof(*i40e_cfgfs_vsi), GFP_KERNEL);
259 return ERR_PTR(-ENOMEM);
261 np = netdev_priv(netdev);
262 i40e_cfgfs_vsi->vsi = np->vsi;
263 config_item_init_type_name(&i40e_cfgfs_vsi->item, name,
264 &i40e_cfgfs_vsi_type);
266 return &i40e_cfgfs_vsi->item;
269 static struct configfs_attribute i40e_cfgfs_group_attr_description = {
270 .ca_owner = THIS_MODULE,
271 .ca_name = "description",
275 static struct configfs_attribute *i40e_cfgfs_group_attrs[] = {
276 &i40e_cfgfs_group_attr_description,
280 static ssize_t i40e_cfgfs_group_attr_show(struct config_item *item,
281 struct configfs_attribute *attr,
287 "This subsystem allows the modification of network port configurations.\n"
288 "To start, use the name of the network port to be configured in a 'mkdir'\n"
289 "command, e.g. 'mkdir eth3'.\n");
292 static void i40e_cfgfs_group_release(struct config_item *item)
294 kfree(to_i40e_cfgfs_group(item));
297 static struct configfs_item_operations i40e_cfgfs_group_item_ops = {
298 .release = i40e_cfgfs_group_release,
299 .show_attribute = i40e_cfgfs_group_attr_show,
302 /* Note that, since no extra work is required on ->drop_item(),
303 * no ->drop_item() is provided.
305 static struct configfs_group_operations i40e_cfgfs_group_ops = {
306 .make_item = i40e_cfgfs_group_make_item,
309 static struct config_item_type i40e_cfgfs_group_type = {
310 .ct_item_ops = &i40e_cfgfs_group_item_ops,
311 .ct_group_ops = &i40e_cfgfs_group_ops,
312 .ct_attrs = i40e_cfgfs_group_attrs,
313 .ct_owner = THIS_MODULE,
316 static struct configfs_subsystem i40e_cfgfs_group_subsys = {
319 .ci_namebuf = "i40e",
320 .ci_type = &i40e_cfgfs_group_type,
326 * i40e_configfs_init - Initialize configfs support for our driver
328 int i40e_configfs_init(void)
331 struct configfs_subsystem *subsys;
333 subsys = &i40e_cfgfs_group_subsys;
335 config_group_init(&subsys->su_group);
336 mutex_init(&subsys->su_mutex);
337 ret = configfs_register_subsystem(subsys);
339 pr_err("Error %d while registering configfs subsystem %s\n",
340 ret, subsys->su_group.cg_item.ci_namebuf);
348 * i40e_configfs_init - Bail out - unregister configfs subsystem and release
350 void i40e_configfs_exit(void)
352 configfs_unregister_subsystem(&i40e_cfgfs_group_subsys);
354 #endif /* IS_ENABLED(CONFIG_I40E_CONFIGFS_FS) */