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 struct stp_policy
*to_stp_policy(struct config_item
*item
)
63 container_of(to_config_group(item
), struct stp_policy
, group
) :
67 static inline struct stp_policy_node
*
68 to_stp_policy_node(struct config_item
*item
)
71 container_of(to_config_group(item
), struct stp_policy_node
,
76 void *to_pdrv_policy_node(struct config_item
*item
)
78 struct stp_policy_node
*node
= to_stp_policy_node(item
);
80 return stp_policy_node_priv(node
);
82 EXPORT_SYMBOL_GPL(to_pdrv_policy_node
);
85 stp_policy_node_masters_show(struct config_item
*item
, char *page
)
87 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
90 count
= sprintf(page
, "%u %u\n", policy_node
->first_master
,
91 policy_node
->last_master
);
97 stp_policy_node_masters_store(struct config_item
*item
, const char *page
,
100 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
101 unsigned int first
, last
;
102 struct stm_device
*stm
;
103 char *p
= (char *)page
;
104 ssize_t ret
= -ENODEV
;
106 if (sscanf(p
, "%u %u", &first
, &last
) != 2)
109 mutex_lock(&stp_policy_subsys
.su_mutex
);
110 stm
= policy_node
->policy
->stm
;
114 /* must be within [sw_start..sw_end], which is an inclusive range */
115 if (first
> last
|| first
< stm
->data
->sw_start
||
116 last
> stm
->data
->sw_end
) {
122 policy_node
->first_master
= first
;
123 policy_node
->last_master
= last
;
126 mutex_unlock(&stp_policy_subsys
.su_mutex
);
132 stp_policy_node_channels_show(struct config_item
*item
, char *page
)
134 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
137 count
= sprintf(page
, "%u %u\n", policy_node
->first_channel
,
138 policy_node
->last_channel
);
144 stp_policy_node_channels_store(struct config_item
*item
, const char *page
,
147 struct stp_policy_node
*policy_node
= to_stp_policy_node(item
);
148 unsigned int first
, last
;
149 struct stm_device
*stm
;
150 char *p
= (char *)page
;
151 ssize_t ret
= -ENODEV
;
153 if (sscanf(p
, "%u %u", &first
, &last
) != 2)
156 mutex_lock(&stp_policy_subsys
.su_mutex
);
157 stm
= policy_node
->policy
->stm
;
161 if (first
> INT_MAX
|| last
> INT_MAX
|| first
> last
||
162 last
>= stm
->data
->sw_nchannels
) {
168 policy_node
->first_channel
= first
;
169 policy_node
->last_channel
= last
;
172 mutex_unlock(&stp_policy_subsys
.su_mutex
);
177 static void stp_policy_node_release(struct config_item
*item
)
179 struct stp_policy_node
*node
= to_stp_policy_node(item
);
184 static struct configfs_item_operations stp_policy_node_item_ops
= {
185 .release
= stp_policy_node_release
,
188 CONFIGFS_ATTR(stp_policy_node_
, masters
);
189 CONFIGFS_ATTR(stp_policy_node_
, channels
);
191 static struct configfs_attribute
*stp_policy_node_attrs
[] = {
192 &stp_policy_node_attr_masters
,
193 &stp_policy_node_attr_channels
,
197 static const struct config_item_type stp_policy_type
;
198 static const struct config_item_type stp_policy_node_type
;
200 const struct config_item_type
*
201 get_policy_node_type(struct configfs_attribute
**attrs
)
203 struct config_item_type
*type
;
204 struct configfs_attribute
**merged
;
206 type
= kmemdup(&stp_policy_node_type
, sizeof(stp_policy_node_type
),
211 merged
= memcat_p(stp_policy_node_attrs
, attrs
);
217 type
->ct_attrs
= merged
;
222 static struct config_group
*
223 stp_policy_node_make(struct config_group
*group
, const char *name
)
225 const struct config_item_type
*type
= &stp_policy_node_type
;
226 struct stp_policy_node
*policy_node
, *parent_node
;
227 const struct stm_protocol_driver
*pdrv
;
228 struct stp_policy
*policy
;
230 if (group
->cg_item
.ci_type
== &stp_policy_type
) {
231 policy
= container_of(group
, struct stp_policy
, group
);
233 parent_node
= container_of(group
, struct stp_policy_node
,
235 policy
= parent_node
->policy
;
239 return ERR_PTR(-ENODEV
);
241 pdrv
= policy
->stm
->pdrv
;
243 kzalloc(offsetof(struct stp_policy_node
, priv
[pdrv
->priv_sz
]),
246 return ERR_PTR(-ENOMEM
);
248 if (pdrv
->policy_node_init
)
249 pdrv
->policy_node_init((void *)policy_node
->priv
);
251 if (policy
->stm
->pdrv_node_type
)
252 type
= policy
->stm
->pdrv_node_type
;
254 config_group_init_type_name(&policy_node
->group
, name
, type
);
256 policy_node
->policy
= policy
;
258 /* default values for the attributes */
259 policy_node
->first_master
= policy
->stm
->data
->sw_start
;
260 policy_node
->last_master
= policy
->stm
->data
->sw_end
;
261 policy_node
->first_channel
= 0;
262 policy_node
->last_channel
= policy
->stm
->data
->sw_nchannels
- 1;
264 return &policy_node
->group
;
268 stp_policy_node_drop(struct config_group
*group
, struct config_item
*item
)
270 config_item_put(item
);
273 static struct configfs_group_operations stp_policy_node_group_ops
= {
274 .make_group
= stp_policy_node_make
,
275 .drop_item
= stp_policy_node_drop
,
278 static const struct config_item_type stp_policy_node_type
= {
279 .ct_item_ops
= &stp_policy_node_item_ops
,
280 .ct_group_ops
= &stp_policy_node_group_ops
,
281 .ct_attrs
= stp_policy_node_attrs
,
282 .ct_owner
= THIS_MODULE
,
286 * Root group: policies.
288 static ssize_t
stp_policy_device_show(struct config_item
*item
,
291 struct stp_policy
*policy
= to_stp_policy(item
);
294 count
= sprintf(page
, "%s\n",
295 (policy
&& policy
->stm
) ?
296 policy
->stm
->data
->name
:
302 CONFIGFS_ATTR_RO(stp_policy_
, device
);
304 static ssize_t
stp_policy_protocol_show(struct config_item
*item
,
307 struct stp_policy
*policy
= to_stp_policy(item
);
310 count
= sprintf(page
, "%s\n",
311 (policy
&& policy
->stm
) ?
312 policy
->stm
->pdrv
->name
:
318 CONFIGFS_ATTR_RO(stp_policy_
, protocol
);
320 static struct configfs_attribute
*stp_policy_attrs
[] = {
321 &stp_policy_attr_device
,
322 &stp_policy_attr_protocol
,
326 void stp_policy_unbind(struct stp_policy
*policy
)
328 struct stm_device
*stm
= policy
->stm
;
331 * stp_policy_release() will not call here if the policy is already
332 * unbound; other users should not either, as no link exists between
333 * this policy and anything else in that case
335 if (WARN_ON_ONCE(!policy
->stm
))
338 lockdep_assert_held(&stm
->policy_mutex
);
344 * Drop the reference on the protocol driver and lose the link.
346 stm_put_protocol(stm
->pdrv
);
351 static void stp_policy_release(struct config_item
*item
)
353 struct stp_policy
*policy
= to_stp_policy(item
);
354 struct stm_device
*stm
= policy
->stm
;
356 /* a policy *can* be unbound and still exist in configfs tree */
360 mutex_lock(&stm
->policy_mutex
);
361 stp_policy_unbind(policy
);
362 mutex_unlock(&stm
->policy_mutex
);
367 static struct configfs_item_operations stp_policy_item_ops
= {
368 .release
= stp_policy_release
,
371 static struct configfs_group_operations stp_policy_group_ops
= {
372 .make_group
= stp_policy_node_make
,
375 static const struct config_item_type stp_policy_type
= {
376 .ct_item_ops
= &stp_policy_item_ops
,
377 .ct_group_ops
= &stp_policy_group_ops
,
378 .ct_attrs
= stp_policy_attrs
,
379 .ct_owner
= THIS_MODULE
,
382 static struct config_group
*
383 stp_policy_make(struct config_group
*group
, const char *name
)
385 const struct config_item_type
*pdrv_node_type
;
386 const struct stm_protocol_driver
*pdrv
;
387 char *devname
, *proto
, *p
;
388 struct config_group
*ret
;
389 struct stm_device
*stm
;
392 devname
= kasprintf(GFP_KERNEL
, "%s", name
);
394 return ERR_PTR(-ENOMEM
);
397 * node must look like <device_name>.<policy_name>, where
398 * <device_name> is the name of an existing stm device; may
400 * <policy_name> is an arbitrary string; may not contain dots
401 * <device_name>:<protocol_name>.<policy_name>
403 p
= strrchr(devname
, '.');
406 return ERR_PTR(-EINVAL
);
412 * look for ":<protocol_name>":
413 * + no protocol suffix: fall back to whatever is available;
414 * + unknown protocol: fail the whole thing
416 proto
= strrchr(devname
, ':');
420 stm
= stm_find_device(devname
);
423 return ERR_PTR(-ENODEV
);
426 err
= stm_lookup_protocol(proto
, &pdrv
, &pdrv_node_type
);
431 return ERR_PTR(-ENODEV
);
434 mutex_lock(&stm
->policy_mutex
);
436 ret
= ERR_PTR(-EBUSY
);
440 stm
->policy
= kzalloc(sizeof(*stm
->policy
), GFP_KERNEL
);
442 ret
= ERR_PTR(-ENOMEM
);
446 config_group_init_type_name(&stm
->policy
->group
, name
,
450 stm
->pdrv_node_type
= pdrv_node_type
;
451 stm
->policy
->stm
= stm
;
452 ret
= &stm
->policy
->group
;
455 mutex_unlock(&stm
->policy_mutex
);
459 * pdrv and stm->pdrv at this point can be quite different,
460 * and only one of them needs to be 'put'
462 stm_put_protocol(pdrv
);
469 static struct configfs_group_operations stp_policy_root_group_ops
= {
470 .make_group
= stp_policy_make
,
473 static const struct config_item_type stp_policy_root_type
= {
474 .ct_group_ops
= &stp_policy_root_group_ops
,
475 .ct_owner
= THIS_MODULE
,
478 static struct configfs_subsystem stp_policy_subsys
= {
481 .ci_namebuf
= "stp-policy",
482 .ci_type
= &stp_policy_root_type
,
488 * Lock the policy mutex from the outside
490 static struct stp_policy_node
*
491 __stp_policy_node_lookup(struct stp_policy
*policy
, char *s
)
493 struct stp_policy_node
*policy_node
, *ret
= NULL
;
494 struct list_head
*head
= &policy
->group
.cg_children
;
495 struct config_item
*item
;
496 char *start
, *end
= s
;
498 if (list_empty(head
))
503 start
= strsep(&end
, "/");
510 list_for_each_entry(item
, head
, ci_entry
) {
511 policy_node
= to_stp_policy_node(item
);
514 policy_node
->group
.cg_item
.ci_name
)) {
520 head
= &policy_node
->group
.cg_children
;
532 struct stp_policy_node
*
533 stp_policy_node_lookup(struct stm_device
*stm
, char *s
)
535 struct stp_policy_node
*policy_node
= NULL
;
537 mutex_lock(&stp_policy_subsys
.su_mutex
);
539 mutex_lock(&stm
->policy_mutex
);
541 policy_node
= __stp_policy_node_lookup(stm
->policy
, s
);
542 mutex_unlock(&stm
->policy_mutex
);
545 config_item_get(&policy_node
->group
.cg_item
);
547 mutex_unlock(&stp_policy_subsys
.su_mutex
);
552 void stp_policy_node_put(struct stp_policy_node
*policy_node
)
554 lockdep_assert_held(&stp_policy_subsys
.su_mutex
);
556 mutex_unlock(&stp_policy_subsys
.su_mutex
);
557 config_item_put(&policy_node
->group
.cg_item
);
560 int __init
stp_configfs_init(void)
562 config_group_init(&stp_policy_subsys
.su_group
);
563 mutex_init(&stp_policy_subsys
.su_mutex
);
564 return configfs_register_subsystem(&stp_policy_subsys
);
567 void __exit
stp_configfs_exit(void)
569 configfs_unregister_subsystem(&stp_policy_subsys
);