^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * System Trace Module (STM) master/channel allocation policy management
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2014, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * A master/channel allocation policy allows mapping string identifiers to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * master and channel ranges, where allocation can be done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/configfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/stm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "stm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * STP Master/Channel allocation policy configfs layout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct stp_policy {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct config_group group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct stm_device *stm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct stp_policy_node {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct config_group group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct stp_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int first_master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned int last_master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned int first_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned int last_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* this is the one that's exposed to the attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned char priv[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void *stp_policy_node_priv(struct stp_policy_node *pn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!pn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return pn->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static struct configfs_subsystem stp_policy_subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned int *mstart, unsigned int *mend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int *cstart, unsigned int *cend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *mstart = policy_node->first_master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) *mend = policy_node->last_master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) *cstart = policy_node->first_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *cend = policy_node->last_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static inline char *stp_policy_node_name(struct stp_policy_node *policy_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return policy_node->group.cg_item.ci_name ? : "<none>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static inline struct stp_policy *to_stp_policy(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return item ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) container_of(to_config_group(item), struct stp_policy, group) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static inline struct stp_policy_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) to_stp_policy_node(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return item ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) container_of(to_config_group(item), struct stp_policy_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) group) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) void *to_pdrv_policy_node(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct stp_policy_node *node = to_stp_policy_node(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return stp_policy_node_priv(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) EXPORT_SYMBOL_GPL(to_pdrv_policy_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) stp_policy_node_masters_show(struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct stp_policy_node *policy_node = to_stp_policy_node(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) count = sprintf(page, "%u %u\n", policy_node->first_master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) policy_node->last_master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) stp_policy_node_masters_store(struct config_item *item, const char *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct stp_policy_node *policy_node = to_stp_policy_node(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) unsigned int first, last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct stm_device *stm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) char *p = (char *)page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ssize_t ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (sscanf(p, "%u %u", &first, &last) != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) mutex_lock(&stp_policy_subsys.su_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) stm = policy_node->policy->stm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!stm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* must be within [sw_start..sw_end], which is an inclusive range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (first > last || first < stm->data->sw_start ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) last > stm->data->sw_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ret = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) policy_node->first_master = first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) policy_node->last_master = last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) mutex_unlock(&stp_policy_subsys.su_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) stp_policy_node_channels_show(struct config_item *item, char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct stp_policy_node *policy_node = to_stp_policy_node(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) count = sprintf(page, "%u %u\n", policy_node->first_channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) policy_node->last_channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) stp_policy_node_channels_store(struct config_item *item, const char *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct stp_policy_node *policy_node = to_stp_policy_node(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned int first, last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct stm_device *stm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) char *p = (char *)page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ssize_t ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (sscanf(p, "%u %u", &first, &last) != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) mutex_lock(&stp_policy_subsys.su_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) stm = policy_node->policy->stm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!stm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (first > INT_MAX || last > INT_MAX || first > last ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) last >= stm->data->sw_nchannels) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ret = -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) policy_node->first_channel = first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) policy_node->last_channel = last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) mutex_unlock(&stp_policy_subsys.su_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static void stp_policy_node_release(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct stp_policy_node *node = to_stp_policy_node(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) kfree(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) static struct configfs_item_operations stp_policy_node_item_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .release = stp_policy_node_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) CONFIGFS_ATTR(stp_policy_node_, masters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) CONFIGFS_ATTR(stp_policy_node_, channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static struct configfs_attribute *stp_policy_node_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) &stp_policy_node_attr_masters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) &stp_policy_node_attr_channels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static const struct config_item_type stp_policy_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static const struct config_item_type stp_policy_node_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) const struct config_item_type *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) get_policy_node_type(struct configfs_attribute **attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct config_item_type *type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct configfs_attribute **merged;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) type = kmemdup(&stp_policy_node_type, sizeof(stp_policy_node_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (!type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) merged = memcat_p(stp_policy_node_attrs, attrs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (!merged) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) kfree(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) type->ct_attrs = merged;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static struct config_group *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) stp_policy_node_make(struct config_group *group, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) const struct config_item_type *type = &stp_policy_node_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct stp_policy_node *policy_node, *parent_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) const struct stm_protocol_driver *pdrv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct stp_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (group->cg_item.ci_type == &stp_policy_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) policy = container_of(group, struct stp_policy, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) parent_node = container_of(group, struct stp_policy_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) policy = parent_node->policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (!policy->stm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) pdrv = policy->stm->pdrv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) policy_node =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) kzalloc(offsetof(struct stp_policy_node, priv[pdrv->priv_sz]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!policy_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (pdrv->policy_node_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) pdrv->policy_node_init((void *)policy_node->priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (policy->stm->pdrv_node_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) type = policy->stm->pdrv_node_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) config_group_init_type_name(&policy_node->group, name, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) policy_node->policy = policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) /* default values for the attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) policy_node->first_master = policy->stm->data->sw_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) policy_node->last_master = policy->stm->data->sw_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) policy_node->first_channel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) policy_node->last_channel = policy->stm->data->sw_nchannels - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return &policy_node->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) stp_policy_node_drop(struct config_group *group, struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) config_item_put(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static struct configfs_group_operations stp_policy_node_group_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) .make_group = stp_policy_node_make,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) .drop_item = stp_policy_node_drop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static const struct config_item_type stp_policy_node_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) .ct_item_ops = &stp_policy_node_item_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .ct_group_ops = &stp_policy_node_group_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .ct_attrs = stp_policy_node_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Root group: policies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static ssize_t stp_policy_device_show(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct stp_policy *policy = to_stp_policy(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) count = sprintf(page, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) (policy && policy->stm) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) policy->stm->data->name :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) "<none>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) CONFIGFS_ATTR_RO(stp_policy_, device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static ssize_t stp_policy_protocol_show(struct config_item *item,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) char *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct stp_policy *policy = to_stp_policy(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ssize_t count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) count = sprintf(page, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) (policy && policy->stm) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) policy->stm->pdrv->name :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) "<none>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) CONFIGFS_ATTR_RO(stp_policy_, protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static struct configfs_attribute *stp_policy_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) &stp_policy_attr_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) &stp_policy_attr_protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) void stp_policy_unbind(struct stp_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct stm_device *stm = policy->stm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * stp_policy_release() will not call here if the policy is already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * unbound; other users should not either, as no link exists between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * this policy and anything else in that case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (WARN_ON_ONCE(!policy->stm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) lockdep_assert_held(&stm->policy_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) stm->policy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) policy->stm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * Drop the reference on the protocol driver and lose the link.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) stm_put_protocol(stm->pdrv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) stm->pdrv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) stm_put_device(stm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static void stp_policy_release(struct config_item *item)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) struct stp_policy *policy = to_stp_policy(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct stm_device *stm = policy->stm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /* a policy *can* be unbound and still exist in configfs tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (!stm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) mutex_lock(&stm->policy_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) stp_policy_unbind(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) mutex_unlock(&stm->policy_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) kfree(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static struct configfs_item_operations stp_policy_item_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) .release = stp_policy_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static struct configfs_group_operations stp_policy_group_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) .make_group = stp_policy_node_make,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static const struct config_item_type stp_policy_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) .ct_item_ops = &stp_policy_item_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) .ct_group_ops = &stp_policy_group_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) .ct_attrs = stp_policy_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static struct config_group *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) stp_policy_make(struct config_group *group, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) const struct config_item_type *pdrv_node_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) const struct stm_protocol_driver *pdrv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) char *devname, *proto, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct config_group *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct stm_device *stm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) devname = kasprintf(GFP_KERNEL, "%s", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (!devname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * node must look like <device_name>.<policy_name>, where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * <device_name> is the name of an existing stm device; may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * contain dots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * <policy_name> is an arbitrary string; may not contain dots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * <device_name>:<protocol_name>.<policy_name>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) p = strrchr(devname, '.');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) kfree(devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * look for ":<protocol_name>":
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * + no protocol suffix: fall back to whatever is available;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * + unknown protocol: fail the whole thing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) proto = strrchr(devname, ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) *proto++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) stm = stm_find_device(devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (!stm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) kfree(devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) err = stm_lookup_protocol(proto, &pdrv, &pdrv_node_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) kfree(devname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) stm_put_device(stm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) mutex_lock(&stm->policy_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (stm->policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) ret = ERR_PTR(-EBUSY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) goto unlock_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) stm->policy = kzalloc(sizeof(*stm->policy), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (!stm->policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) ret = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) goto unlock_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) config_group_init_type_name(&stm->policy->group, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) &stp_policy_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) stm->pdrv = pdrv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) stm->pdrv_node_type = pdrv_node_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) stm->policy->stm = stm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) ret = &stm->policy->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) unlock_policy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) mutex_unlock(&stm->policy_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (IS_ERR(ret)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * pdrv and stm->pdrv at this point can be quite different,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * and only one of them needs to be 'put'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) stm_put_protocol(pdrv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) stm_put_device(stm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static struct configfs_group_operations stp_policy_root_group_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) .make_group = stp_policy_make,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static const struct config_item_type stp_policy_root_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) .ct_group_ops = &stp_policy_root_group_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) .ct_owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static struct configfs_subsystem stp_policy_subsys = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) .su_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) .cg_item = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) .ci_namebuf = "stp-policy",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) .ci_type = &stp_policy_root_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) * Lock the policy mutex from the outside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) static struct stp_policy_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) __stp_policy_node_lookup(struct stp_policy *policy, char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) struct stp_policy_node *policy_node, *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct list_head *head = &policy->group.cg_children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct config_item *item;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) char *start, *end = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (list_empty(head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) start = strsep(&end, "/");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) if (!start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (!*start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) list_for_each_entry(item, head, ci_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) policy_node = to_stp_policy_node(item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (!strcmp(start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) policy_node->group.cg_item.ci_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) ret = policy_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (!end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) head = &policy_node->group.cg_children;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) struct stp_policy_node *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) stp_policy_node_lookup(struct stm_device *stm, char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) struct stp_policy_node *policy_node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) mutex_lock(&stp_policy_subsys.su_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) mutex_lock(&stm->policy_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (stm->policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) policy_node = __stp_policy_node_lookup(stm->policy, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) mutex_unlock(&stm->policy_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (policy_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) config_item_get(&policy_node->group.cg_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) mutex_unlock(&stp_policy_subsys.su_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return policy_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) void stp_policy_node_put(struct stp_policy_node *policy_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) lockdep_assert_held(&stp_policy_subsys.su_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) mutex_unlock(&stp_policy_subsys.su_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) config_item_put(&policy_node->group.cg_item);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) int __init stp_configfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) config_group_init(&stp_policy_subsys.su_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) mutex_init(&stp_policy_subsys.su_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return configfs_register_subsystem(&stp_policy_subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) void __exit stp_configfs_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) configfs_unregister_subsystem(&stp_policy_subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }