1 // SPDX-License-Identifier: GPL-2.0
3 * System Trace Module (STM) master/channel allocation policy management
4 * Copyright (c) 2014, Intel Corporation.
6 * A master/channel allocation policy allows mapping string identifiers to
7 * master and channel ranges, where allocation can be done.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/configfs.h>
16 #include <linux/slab.h>
17 #include <linux/stm.h>
21 * STP Master/Channel allocation policy configfs layout.
25 struct config_group group
;
26 struct stm_device
*stm
;
29 struct stp_policy_node
{
30 struct config_group group
;
31 struct stp_policy
*policy
;
32 unsigned int first_master
;
33 unsigned int last_master
;
34 unsigned int first_channel
;
35 unsigned int last_channel
;
36 /* this is the one that's exposed to the attributes */
40 void *stp_policy_node_priv(struct stp_policy_node
*pn
)
48 static struct configfs_subsystem stp_policy_subsys
;
50 void stp_policy_node_get_ranges(struct stp_policy_node
*policy_node
,
51 unsigned int *mstart
, unsigned int *mend
,
52 unsigned int *cstart
, unsigned int *cend
)
54 *mstart
= policy_node
->first_master
;
55 *mend
= policy_node
->last_master
;
56 *cstart
= policy_node
->first_channel
;
57 *cend
= policy_node
->last_channel
;
60 static inline char *stp_policy_node_name(struct stp_policy_node
*policy_node
)
62 return policy_node
->group
.cg_item
.ci_name
? : "<none>";
65 static inline struct stp_policy
*to_stp_policy(struct config_item
*item
)
68 container_of(to_config_group(item
), struct stp_policy
, group
) :
72 static inline struct stp_policy_node
*
73 to_stp_policy_node(struct config_item
*item
)
76 container_of(to_config_group(item
), struct stp_policy_node
,
81 void *to_pdrv_policy_node(struct config_item
*item
)
83 struct stp_policy_node
*node
= to_stp_policy_node(item
);
85 return stp_policy_node_priv(node
);
87 EXPORT_SYMBOL_GPL(to_pdrv_policy_node
);
90 stp_policy_node_masters_show(struct config_item
*item
, char *page
)
92 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
95 count
= sprintf(page
, "%u %u\n", policy_node
->first_master
,
96 policy_node
->last_master
);
102 stp_policy_node_masters_store(struct config_item
*item
, const char *page
,
105 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
106 unsigned int first
, last
;
107 struct stm_device
*stm
;
108 char *p
= (char *)page
;
109 ssize_t ret
= -ENODEV
;
111 if (sscanf(p
, "%u %u", &first
, &last
) != 2)
114 mutex_lock(&stp_policy_subsys
.su_mutex
);
115 stm
= policy_node
->policy
->stm
;
119 /* must be within [sw_start..sw_end], which is an inclusive range */
120 if (first
> last
|| first
< stm
->data
->sw_start
||
121 last
> stm
->data
->sw_end
) {
127 policy_node
->first_master
= first
;
128 policy_node
->last_master
= last
;
131 mutex_unlock(&stp_policy_subsys
.su_mutex
);
137 stp_policy_node_channels_show(struct config_item
*item
, char *page
)
139 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
142 count
= sprintf(page
, "%u %u\n", policy_node
->first_channel
,
143 policy_node
->last_channel
);
149 stp_policy_node_channels_store(struct config_item
*item
, const char *page
,
152 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
153 unsigned int first
, last
;
154 struct stm_device
*stm
;
155 char *p
= (char *)page
;
156 ssize_t ret
= -ENODEV
;
158 if (sscanf(p
, "%u %u", &first
, &last
) != 2)
161 mutex_lock(&stp_policy_subsys
.su_mutex
);
162 stm
= policy_node
->policy
->stm
;
166 if (first
> INT_MAX
|| last
> INT_MAX
|| first
> last
||
167 last
>= stm
->data
->sw_nchannels
) {
173 policy_node
->first_channel
= first
;
174 policy_node
->last_channel
= last
;
177 mutex_unlock(&stp_policy_subsys
.su_mutex
);
182 static void stp_policy_node_release(struct config_item
*item
)
184 struct stp_policy_node
*node
= to_stp_policy_node(item
);
189 static struct configfs_item_operations stp_policy_node_item_ops
= {
190 .release
= stp_policy_node_release
,
193 CONFIGFS_ATTR(stp_policy_node_
, masters
);
194 CONFIGFS_ATTR(stp_policy_node_
, channels
);
196 static struct configfs_attribute
*stp_policy_node_attrs
[] = {
197 &stp_policy_node_attr_masters
,
198 &stp_policy_node_attr_channels
,
202 static const struct config_item_type stp_policy_type
;
203 static const struct config_item_type stp_policy_node_type
;
205 const struct config_item_type
*
206 get_policy_node_type(struct configfs_attribute
**attrs
)
208 struct config_item_type
*type
;
209 struct configfs_attribute
**merged
;
211 type
= kmemdup(&stp_policy_node_type
, sizeof(stp_policy_node_type
),
216 merged
= memcat_p(stp_policy_node_attrs
, attrs
);
222 type
->ct_attrs
= merged
;
227 static struct config_group
*
228 stp_policy_node_make(struct config_group
*group
, const char *name
)
230 const struct config_item_type
*type
= &stp_policy_node_type
;
231 struct stp_policy_node
*policy_node
, *parent_node
;
232 const struct stm_protocol_driver
*pdrv
;
233 struct stp_policy
*policy
;
235 if (group
->cg_item
.ci_type
== &stp_policy_type
) {
236 policy
= container_of(group
, struct stp_policy
, group
);
238 parent_node
= container_of(group
, struct stp_policy_node
,
240 policy
= parent_node
->policy
;
244 return ERR_PTR(-ENODEV
);
246 pdrv
= policy
->stm
->pdrv
;
248 kzalloc(offsetof(struct stp_policy_node
, priv
[pdrv
->priv_sz
]),
251 return ERR_PTR(-ENOMEM
);
253 if (pdrv
->policy_node_init
)
254 pdrv
->policy_node_init((void *)policy_node
->priv
);
256 if (policy
->stm
->pdrv_node_type
)
257 type
= policy
->stm
->pdrv_node_type
;
259 config_group_init_type_name(&policy_node
->group
, name
, type
);
261 policy_node
->policy
= policy
;
263 /* default values for the attributes */
264 policy_node
->first_master
= policy
->stm
->data
->sw_start
;
265 policy_node
->last_master
= policy
->stm
->data
->sw_end
;
266 policy_node
->first_channel
= 0;
267 policy_node
->last_channel
= policy
->stm
->data
->sw_nchannels
- 1;
269 return &policy_node
->group
;
273 stp_policy_node_drop(struct config_group
*group
, struct config_item
*item
)
275 config_item_put(item
);
278 static struct configfs_group_operations stp_policy_node_group_ops
= {
279 .make_group
= stp_policy_node_make
,
280 .drop_item
= stp_policy_node_drop
,
283 static const struct config_item_type stp_policy_node_type
= {
284 .ct_item_ops
= &stp_policy_node_item_ops
,
285 .ct_group_ops
= &stp_policy_node_group_ops
,
286 .ct_attrs
= stp_policy_node_attrs
,
287 .ct_owner
= THIS_MODULE
,
291 * Root group: policies.
293 static ssize_t
stp_policy_device_show(struct config_item
*item
,
296 struct stp_policy
*policy
= to_stp_policy(item
);
299 count
= sprintf(page
, "%s\n",
300 (policy
&& policy
->stm
) ?
301 policy
->stm
->data
->name
:
307 CONFIGFS_ATTR_RO(stp_policy_
, device
);
309 static ssize_t
stp_policy_protocol_show(struct config_item
*item
,
312 struct stp_policy
*policy
= to_stp_policy(item
);
315 count
= sprintf(page
, "%s\n",
316 (policy
&& policy
->stm
) ?
317 policy
->stm
->pdrv
->name
:
323 CONFIGFS_ATTR_RO(stp_policy_
, protocol
);
325 static struct configfs_attribute
*stp_policy_attrs
[] = {
326 &stp_policy_attr_device
,
327 &stp_policy_attr_protocol
,
331 void stp_policy_unbind(struct stp_policy
*policy
)
333 struct stm_device
*stm
= policy
->stm
;
336 * stp_policy_release() will not call here if the policy is already
337 * unbound; other users should not either, as no link exists between
338 * this policy and anything else in that case
340 if (WARN_ON_ONCE(!policy
->stm
))
343 lockdep_assert_held(&stm
->policy_mutex
);
349 * Drop the reference on the protocol driver and lose the link.
351 stm_put_protocol(stm
->pdrv
);
356 static void stp_policy_release(struct config_item
*item
)
358 struct stp_policy
*policy
= to_stp_policy(item
);
359 struct stm_device
*stm
= policy
->stm
;
361 /* a policy *can* be unbound and still exist in configfs tree */
365 mutex_lock(&stm
->policy_mutex
);
366 stp_policy_unbind(policy
);
367 mutex_unlock(&stm
->policy_mutex
);
372 static struct configfs_item_operations stp_policy_item_ops
= {
373 .release
= stp_policy_release
,
376 static struct configfs_group_operations stp_policy_group_ops
= {
377 .make_group
= stp_policy_node_make
,
380 static const struct config_item_type stp_policy_type
= {
381 .ct_item_ops
= &stp_policy_item_ops
,
382 .ct_group_ops
= &stp_policy_group_ops
,
383 .ct_attrs
= stp_policy_attrs
,
384 .ct_owner
= THIS_MODULE
,
387 static struct config_group
*
388 stp_policy_make(struct config_group
*group
, const char *name
)
390 const struct config_item_type
*pdrv_node_type
;
391 const struct stm_protocol_driver
*pdrv
;
392 char *devname
, *proto
, *p
;
393 struct config_group
*ret
;
394 struct stm_device
*stm
;
397 devname
= kasprintf(GFP_KERNEL
, "%s", name
);
399 return ERR_PTR(-ENOMEM
);
402 * node must look like <device_name>.<policy_name>, where
403 * <device_name> is the name of an existing stm device; may
405 * <policy_name> is an arbitrary string; may not contain dots
406 * <device_name>:<protocol_name>.<policy_name>
408 p
= strrchr(devname
, '.');
411 return ERR_PTR(-EINVAL
);
417 * look for ":<protocol_name>":
418 * + no protocol suffix: fall back to whatever is available;
419 * + unknown protocol: fail the whole thing
421 proto
= strrchr(devname
, ':');
425 stm
= stm_find_device(devname
);
428 return ERR_PTR(-ENODEV
);
431 err
= stm_lookup_protocol(proto
, &pdrv
, &pdrv_node_type
);
436 return ERR_PTR(-ENODEV
);
439 mutex_lock(&stm
->policy_mutex
);
441 ret
= ERR_PTR(-EBUSY
);
445 stm
->policy
= kzalloc(sizeof(*stm
->policy
), GFP_KERNEL
);
447 ret
= ERR_PTR(-ENOMEM
);
451 config_group_init_type_name(&stm
->policy
->group
, name
,
455 stm
->pdrv_node_type
= pdrv_node_type
;
456 stm
->policy
->stm
= stm
;
457 ret
= &stm
->policy
->group
;
460 mutex_unlock(&stm
->policy_mutex
);
464 * pdrv and stm->pdrv at this point can be quite different,
465 * and only one of them needs to be 'put'
467 stm_put_protocol(pdrv
);
474 static struct configfs_group_operations stp_policy_root_group_ops
= {
475 .make_group
= stp_policy_make
,
478 static const struct config_item_type stp_policy_root_type
= {
479 .ct_group_ops
= &stp_policy_root_group_ops
,
480 .ct_owner
= THIS_MODULE
,
483 static struct configfs_subsystem stp_policy_subsys
= {
486 .ci_namebuf
= "stp-policy",
487 .ci_type
= &stp_policy_root_type
,
493 * Lock the policy mutex from the outside
495 static struct stp_policy_node
*
496 __stp_policy_node_lookup(struct stp_policy
*policy
, char *s
)
498 struct stp_policy_node
*policy_node
, *ret
= NULL
;
499 struct list_head
*head
= &policy
->group
.cg_children
;
500 struct config_item
*item
;
501 char *start
, *end
= s
;
503 if (list_empty(head
))
508 start
= strsep(&end
, "/");
515 list_for_each_entry(item
, head
, ci_entry
) {
516 policy_node
= to_stp_policy_node(item
);
519 policy_node
->group
.cg_item
.ci_name
)) {
525 head
= &policy_node
->group
.cg_children
;
537 struct stp_policy_node
*
538 stp_policy_node_lookup(struct stm_device
*stm
, char *s
)
540 struct stp_policy_node
*policy_node
= NULL
;
542 mutex_lock(&stp_policy_subsys
.su_mutex
);
544 mutex_lock(&stm
->policy_mutex
);
546 policy_node
= __stp_policy_node_lookup(stm
->policy
, s
);
547 mutex_unlock(&stm
->policy_mutex
);
550 config_item_get(&policy_node
->group
.cg_item
);
552 mutex_unlock(&stp_policy_subsys
.su_mutex
);
557 void stp_policy_node_put(struct stp_policy_node
*policy_node
)
559 lockdep_assert_held(&stp_policy_subsys
.su_mutex
);
561 mutex_unlock(&stp_policy_subsys
.su_mutex
);
562 config_item_put(&policy_node
->group
.cg_item
);
565 int __init
stp_configfs_init(void)
567 config_group_init(&stp_policy_subsys
.su_group
);
568 mutex_init(&stp_policy_subsys
.su_mutex
);
569 return configfs_register_subsystem(&stp_policy_subsys
);
572 void __exit
stp_configfs_exit(void)
574 configfs_unregister_subsystem(&stp_policy_subsys
);