2 * System Trace Module (STM) master/channel allocation policy management
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * A master/channel allocation policy allows mapping string identifiers to
15 * master and channel ranges, where allocation can be done.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/types.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/configfs.h>
24 #include <linux/slab.h>
25 #include <linux/stm.h>
29 * STP Master/Channel allocation policy configfs layout.
33 struct config_group group
;
34 struct stm_device
*stm
;
37 struct stp_policy_node
{
38 struct config_group group
;
39 struct stp_policy
*policy
;
40 unsigned int first_master
;
41 unsigned int last_master
;
42 unsigned int first_channel
;
43 unsigned int last_channel
;
46 static struct configfs_subsystem stp_policy_subsys
;
48 void stp_policy_node_get_ranges(struct stp_policy_node
*policy_node
,
49 unsigned int *mstart
, unsigned int *mend
,
50 unsigned int *cstart
, unsigned int *cend
)
52 *mstart
= policy_node
->first_master
;
53 *mend
= policy_node
->last_master
;
54 *cstart
= policy_node
->first_channel
;
55 *cend
= policy_node
->last_channel
;
58 static inline char *stp_policy_node_name(struct stp_policy_node
*policy_node
)
60 return policy_node
->group
.cg_item
.ci_name
? : "<none>";
63 static inline struct stp_policy
*to_stp_policy(struct config_item
*item
)
66 container_of(to_config_group(item
), struct stp_policy
, group
) :
70 static inline struct stp_policy_node
*
71 to_stp_policy_node(struct config_item
*item
)
74 container_of(to_config_group(item
), struct stp_policy_node
,
80 stp_policy_node_masters_show(struct config_item
*item
, char *page
)
82 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
85 count
= sprintf(page
, "%u %u\n", policy_node
->first_master
,
86 policy_node
->last_master
);
92 stp_policy_node_masters_store(struct config_item
*item
, const char *page
,
95 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
96 unsigned int first
, last
;
97 struct stm_device
*stm
;
98 char *p
= (char *)page
;
99 ssize_t ret
= -ENODEV
;
101 if (sscanf(p
, "%u %u", &first
, &last
) != 2)
104 mutex_lock(&stp_policy_subsys
.su_mutex
);
105 stm
= policy_node
->policy
->stm
;
109 /* must be within [sw_start..sw_end], which is an inclusive range */
110 if (first
> INT_MAX
|| last
> INT_MAX
|| first
> last
||
111 first
< stm
->data
->sw_start
||
112 last
> stm
->data
->sw_end
) {
118 policy_node
->first_master
= first
;
119 policy_node
->last_master
= last
;
122 mutex_unlock(&stp_policy_subsys
.su_mutex
);
128 stp_policy_node_channels_show(struct config_item
*item
, char *page
)
130 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
133 count
= sprintf(page
, "%u %u\n", policy_node
->first_channel
,
134 policy_node
->last_channel
);
140 stp_policy_node_channels_store(struct config_item
*item
, const char *page
,
143 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
144 unsigned int first
, last
;
145 struct stm_device
*stm
;
146 char *p
= (char *)page
;
147 ssize_t ret
= -ENODEV
;
149 if (sscanf(p
, "%u %u", &first
, &last
) != 2)
152 mutex_lock(&stp_policy_subsys
.su_mutex
);
153 stm
= policy_node
->policy
->stm
;
157 if (first
> INT_MAX
|| last
> INT_MAX
|| first
> last
||
158 last
>= stm
->data
->sw_nchannels
) {
164 policy_node
->first_channel
= first
;
165 policy_node
->last_channel
= last
;
168 mutex_unlock(&stp_policy_subsys
.su_mutex
);
173 static void stp_policy_node_release(struct config_item
*item
)
175 kfree(to_stp_policy_node(item
));
178 static struct configfs_item_operations stp_policy_node_item_ops
= {
179 .release
= stp_policy_node_release
,
182 CONFIGFS_ATTR(stp_policy_node_
, masters
);
183 CONFIGFS_ATTR(stp_policy_node_
, channels
);
185 static struct configfs_attribute
*stp_policy_node_attrs
[] = {
186 &stp_policy_node_attr_masters
,
187 &stp_policy_node_attr_channels
,
191 static struct config_item_type stp_policy_type
;
192 static struct config_item_type stp_policy_node_type
;
194 static struct config_group
*
195 stp_policy_node_make(struct config_group
*group
, const char *name
)
197 struct stp_policy_node
*policy_node
, *parent_node
;
198 struct stp_policy
*policy
;
200 if (group
->cg_item
.ci_type
== &stp_policy_type
) {
201 policy
= container_of(group
, struct stp_policy
, group
);
203 parent_node
= container_of(group
, struct stp_policy_node
,
205 policy
= parent_node
->policy
;
209 return ERR_PTR(-ENODEV
);
211 policy_node
= kzalloc(sizeof(struct stp_policy_node
), GFP_KERNEL
);
213 return ERR_PTR(-ENOMEM
);
215 config_group_init_type_name(&policy_node
->group
, name
,
216 &stp_policy_node_type
);
218 policy_node
->policy
= policy
;
220 /* default values for the attributes */
221 policy_node
->first_master
= policy
->stm
->data
->sw_start
;
222 policy_node
->last_master
= policy
->stm
->data
->sw_end
;
223 policy_node
->first_channel
= 0;
224 policy_node
->last_channel
= policy
->stm
->data
->sw_nchannels
- 1;
226 return &policy_node
->group
;
230 stp_policy_node_drop(struct config_group
*group
, struct config_item
*item
)
232 config_item_put(item
);
235 static struct configfs_group_operations stp_policy_node_group_ops
= {
236 .make_group
= stp_policy_node_make
,
237 .drop_item
= stp_policy_node_drop
,
240 static struct config_item_type stp_policy_node_type
= {
241 .ct_item_ops
= &stp_policy_node_item_ops
,
242 .ct_group_ops
= &stp_policy_node_group_ops
,
243 .ct_attrs
= stp_policy_node_attrs
,
244 .ct_owner
= THIS_MODULE
,
248 * Root group: policies.
250 static ssize_t
stp_policy_device_show(struct config_item
*item
,
253 struct stp_policy
*policy
= to_stp_policy(item
);
256 count
= sprintf(page
, "%s\n",
257 (policy
&& policy
->stm
) ?
258 policy
->stm
->data
->name
:
264 CONFIGFS_ATTR_RO(stp_policy_
, device
);
266 static struct configfs_attribute
*stp_policy_attrs
[] = {
267 &stp_policy_attr_device
,
271 void stp_policy_unbind(struct stp_policy
*policy
)
273 struct stm_device
*stm
= policy
->stm
;
276 * stp_policy_release() will not call here if the policy is already
277 * unbound; other users should not either, as no link exists between
278 * this policy and anything else in that case
280 if (WARN_ON_ONCE(!policy
->stm
))
283 lockdep_assert_held(&stm
->policy_mutex
);
291 static void stp_policy_release(struct config_item
*item
)
293 struct stp_policy
*policy
= to_stp_policy(item
);
294 struct stm_device
*stm
= policy
->stm
;
296 /* a policy *can* be unbound and still exist in configfs tree */
300 mutex_lock(&stm
->policy_mutex
);
301 stp_policy_unbind(policy
);
302 mutex_unlock(&stm
->policy_mutex
);
307 static struct configfs_item_operations stp_policy_item_ops
= {
308 .release
= stp_policy_release
,
311 static struct configfs_group_operations stp_policy_group_ops
= {
312 .make_group
= stp_policy_node_make
,
315 static struct config_item_type stp_policy_type
= {
316 .ct_item_ops
= &stp_policy_item_ops
,
317 .ct_group_ops
= &stp_policy_group_ops
,
318 .ct_attrs
= stp_policy_attrs
,
319 .ct_owner
= THIS_MODULE
,
322 static struct config_group
*
323 stp_policies_make(struct config_group
*group
, const char *name
)
325 struct config_group
*ret
;
326 struct stm_device
*stm
;
329 devname
= kasprintf(GFP_KERNEL
, "%s", name
);
331 return ERR_PTR(-ENOMEM
);
334 * node must look like <device_name>.<policy_name>, where
335 * <device_name> is the name of an existing stm device; may
337 * <policy_name> is an arbitrary string; may not contain dots
339 p
= strrchr(devname
, '.');
342 return ERR_PTR(-EINVAL
);
347 stm
= stm_find_device(devname
);
351 return ERR_PTR(-ENODEV
);
353 mutex_lock(&stm
->policy_mutex
);
355 ret
= ERR_PTR(-EBUSY
);
359 stm
->policy
= kzalloc(sizeof(*stm
->policy
), GFP_KERNEL
);
361 ret
= ERR_PTR(-ENOMEM
);
365 config_group_init_type_name(&stm
->policy
->group
, name
,
367 stm
->policy
->stm
= stm
;
369 ret
= &stm
->policy
->group
;
372 mutex_unlock(&stm
->policy_mutex
);
380 static struct configfs_group_operations stp_policies_group_ops
= {
381 .make_group
= stp_policies_make
,
384 static struct config_item_type stp_policies_type
= {
385 .ct_group_ops
= &stp_policies_group_ops
,
386 .ct_owner
= THIS_MODULE
,
389 static struct configfs_subsystem stp_policy_subsys
= {
392 .ci_namebuf
= "stp-policy",
393 .ci_type
= &stp_policies_type
,
399 * Lock the policy mutex from the outside
401 static struct stp_policy_node
*
402 __stp_policy_node_lookup(struct stp_policy
*policy
, char *s
)
404 struct stp_policy_node
*policy_node
, *ret
;
405 struct list_head
*head
= &policy
->group
.cg_children
;
406 struct config_item
*item
;
407 char *start
, *end
= s
;
409 if (list_empty(head
))
412 /* return the first entry if everything else fails */
413 item
= list_entry(head
->next
, struct config_item
, ci_entry
);
414 ret
= to_stp_policy_node(item
);
418 start
= strsep(&end
, "/");
425 list_for_each_entry(item
, head
, ci_entry
) {
426 policy_node
= to_stp_policy_node(item
);
429 policy_node
->group
.cg_item
.ci_name
)) {
435 head
= &policy_node
->group
.cg_children
;
447 struct stp_policy_node
*
448 stp_policy_node_lookup(struct stm_device
*stm
, char *s
)
450 struct stp_policy_node
*policy_node
= NULL
;
452 mutex_lock(&stp_policy_subsys
.su_mutex
);
454 mutex_lock(&stm
->policy_mutex
);
456 policy_node
= __stp_policy_node_lookup(stm
->policy
, s
);
457 mutex_unlock(&stm
->policy_mutex
);
460 config_item_get(&policy_node
->group
.cg_item
);
461 mutex_unlock(&stp_policy_subsys
.su_mutex
);
466 void stp_policy_node_put(struct stp_policy_node
*policy_node
)
468 config_item_put(&policy_node
->group
.cg_item
);
471 int __init
stp_configfs_init(void)
475 config_group_init(&stp_policy_subsys
.su_group
);
476 mutex_init(&stp_policy_subsys
.su_mutex
);
477 err
= configfs_register_subsystem(&stp_policy_subsys
);
482 void __exit
stp_configfs_exit(void)
484 configfs_unregister_subsystem(&stp_policy_subsys
);