1 From: Carl Huang <cjhuang@codeaurora.org>
2 Date: Thu, 3 Dec 2020 05:37:26 -0500
3 Subject: [PATCH] nl80211: add common API to configure SAR power limitations
5 NL80211_CMD_SET_SAR_SPECS is added to configure SAR from
6 user space. NL80211_ATTR_SAR_SPEC is used to pass the SAR
7 power specification when used with NL80211_CMD_SET_SAR_SPECS.
9 Wireless driver needs to register SAR type, supported frequency
10 ranges to wiphy, so user space can query it. The index in
11 frequency range is used to specify which sub band the power
12 limitation applies to. The SAR type is for compatibility, so later
13 other SAR mechanism can be implemented without breaking the user
14 space SAR applications.
16 Normal process is user space queries the SAR capability, and
17 gets the index of supported frequency ranges and associates the
18 power limitation with this index and sends to kernel.
20 Here is an example of message send to kernel:
21 8c 00 00 00 08 00 01 00 00 00 00 00 38 00 2b 81
22 08 00 01 00 00 00 00 00 2c 00 02 80 14 00 00 80
23 08 00 02 00 00 00 00 00 08 00 01 00 38 00 00 00
24 14 00 01 80 08 00 02 00 01 00 00 00 08 00 01 00
27 NL80211_CMD_SET_SAR_SPECS: 0x8c
28 NL80211_ATTR_WIPHY: 0x01(phy idx is 0)
29 NL80211_ATTR_SAR_SPEC: 0x812b (NLA_NESTED)
30 NL80211_SAR_ATTR_TYPE: 0x00 (NL80211_SAR_TYPE_POWER)
31 NL80211_SAR_ATTR_SPECS: 0x8002 (NLA_NESTED)
32 freq range 0 power: 0x38 in 0.25dbm unit (14dbm)
33 freq range 1 power: 0x48 in 0.25dbm unit (18dbm)
35 Signed-off-by: Carl Huang <cjhuang@codeaurora.org>
36 Reviewed-by: Brian Norris <briannorris@chromium.org>
37 Reviewed-by: Abhishek Kumar <kuabhs@chromium.org>
38 Link: https://lore.kernel.org/r/20201203103728.3034-2-cjhuang@codeaurora.org
39 [minor edits, NLA parse cleanups]
40 Signed-off-by: Johannes Berg <johannes.berg@intel.com>
43 --- a/include/net/cfg80211.h
44 +++ b/include/net/cfg80211.h
45 @@ -1737,6 +1737,54 @@ struct station_info {
50 + * struct cfg80211_sar_sub_specs - sub specs limit
51 + * @power: power limitation in 0.25dbm
52 + * @freq_range_index: index the power limitation applies to
54 +struct cfg80211_sar_sub_specs {
56 + u32 freq_range_index;
60 + * struct cfg80211_sar_specs - sar limit specs
61 + * @type: it's set with power in 0.25dbm or other types
62 + * @num_sub_specs: number of sar sub specs
63 + * @sub_specs: memory to hold the sar sub specs
65 +struct cfg80211_sar_specs {
66 + enum nl80211_sar_type type;
68 + struct cfg80211_sar_sub_specs sub_specs[];
73 + * @struct cfg80211_sar_chan_ranges - sar frequency ranges
74 + * @start_freq: start range edge frequency
75 + * @end_freq: end range edge frequency
77 +struct cfg80211_sar_freq_ranges {
83 + * struct cfg80211_sar_capa - sar limit capability
84 + * @type: it's set via power in 0.25dbm or other types
85 + * @num_freq_ranges: number of frequency ranges
86 + * @freq_ranges: memory to hold the freq ranges.
88 + * Note: WLAN driver may append new ranges or split an existing
89 + * range to small ones and then append them.
91 +struct cfg80211_sar_capa {
92 + enum nl80211_sar_type type;
93 + u32 num_freq_ranges;
94 + const struct cfg80211_sar_freq_ranges *freq_ranges;
97 #if IS_ENABLED(CPTCFG_CFG80211)
99 * cfg80211_get_station - retrieve information about a given station
100 @@ -4259,6 +4307,8 @@ struct cfg80211_ops {
101 struct cfg80211_tid_config *tid_conf);
102 int (*reset_tid_config)(struct wiphy *wiphy, struct net_device *dev,
103 const u8 *peer, u8 tids);
104 + int (*set_sar_specs)(struct wiphy *wiphy,
105 + struct cfg80211_sar_specs *sar);
109 @@ -5030,6 +5080,8 @@ struct wiphy {
111 u8 max_data_retry_count;
113 + const struct cfg80211_sar_capa *sar_capa;
115 char priv[] __aligned(NETDEV_ALIGN);
118 --- a/net/wireless/nl80211.c
119 +++ b/net/wireless/nl80211.c
120 @@ -405,6 +405,18 @@ nl80211_unsol_bcast_probe_resp_policy[NL
121 .len = IEEE80211_MAX_DATA_LEN }
124 +static const struct nla_policy
125 +sar_specs_policy[NL80211_SAR_ATTR_SPECS_MAX + 1] = {
126 + [NL80211_SAR_ATTR_SPECS_POWER] = { .type = NLA_S32 },
127 + [NL80211_SAR_ATTR_SPECS_RANGE_INDEX] = {.type = NLA_U32 },
130 +static const struct nla_policy
131 +sar_policy[NL80211_SAR_ATTR_MAX + 1] = {
132 + [NL80211_SAR_ATTR_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_SAR_TYPE),
133 + [NL80211_SAR_ATTR_SPECS] = NLA_POLICY_NESTED_ARRAY(sar_specs_policy),
136 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
137 [0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
138 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
139 @@ -739,6 +751,7 @@ static const struct nla_policy nl80211_p
140 [NL80211_ATTR_SAE_PWE] =
141 NLA_POLICY_RANGE(NLA_U8, NL80211_SAE_PWE_HUNT_AND_PECK,
142 NL80211_SAE_PWE_BOTH),
143 + [NL80211_ATTR_SAR_SPEC] = NLA_POLICY_NESTED(sar_policy),
144 [NL80211_ATTR_RECONNECT_REQUESTED] = { .type = NLA_REJECT },
147 @@ -2117,6 +2130,56 @@ fail:
152 +nl80211_put_sar_specs(struct cfg80211_registered_device *rdev,
153 + struct sk_buff *msg)
155 + struct nlattr *sar_capa, *specs, *sub_freq_range;
156 + u8 num_freq_ranges;
159 + if (!rdev->wiphy.sar_capa)
162 + num_freq_ranges = rdev->wiphy.sar_capa->num_freq_ranges;
164 + sar_capa = nla_nest_start(msg, NL80211_ATTR_SAR_SPEC);
168 + if (nla_put_u32(msg, NL80211_SAR_ATTR_TYPE, rdev->wiphy.sar_capa->type))
171 + specs = nla_nest_start(msg, NL80211_SAR_ATTR_SPECS);
175 + /* report supported freq_ranges */
176 + for (i = 0; i < num_freq_ranges; i++) {
177 + sub_freq_range = nla_nest_start(msg, i + 1);
178 + if (!sub_freq_range)
181 + if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_START_FREQ,
182 + rdev->wiphy.sar_capa->freq_ranges[i].start_freq))
185 + if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_END_FREQ,
186 + rdev->wiphy.sar_capa->freq_ranges[i].end_freq))
189 + nla_nest_end(msg, sub_freq_range);
192 + nla_nest_end(msg, specs);
193 + nla_nest_end(msg, sar_capa);
197 + nla_nest_cancel(msg, sar_capa);
201 struct nl80211_dump_wiphy_state {
204 @@ -2366,6 +2429,8 @@ static int nl80211_send_wiphy(struct cfg
205 CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
206 CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
207 CMD(update_ft_ies, UPDATE_FT_IES);
208 + if (rdev->wiphy.sar_capa)
209 + CMD(set_sar_specs, SET_SAR_SPECS);
213 @@ -2691,6 +2756,11 @@ static int nl80211_send_wiphy(struct cfg
215 if (nl80211_put_tid_config_support(rdev, msg))
216 goto nla_put_failure;
217 + state->split_start++;
220 + if (nl80211_put_sar_specs(rdev, msg))
221 + goto nla_put_failure;
224 state->split_start = 0;
225 @@ -14724,6 +14794,111 @@ static void nl80211_post_doit(__genl_con
229 +static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
230 + struct cfg80211_sar_specs *sar_specs,
231 + struct nlattr *spec[], int index)
233 + u32 range_index, i;
235 + if (!sar_specs || !spec)
238 + if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
239 + !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
242 + range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
244 + /* check if range_index exceeds num_freq_ranges */
245 + if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
248 + /* check if range_index duplicates */
249 + for (i = 0; i < index; i++) {
250 + if (sar_specs->sub_specs[i].freq_range_index == range_index)
254 + sar_specs->sub_specs[index].power =
255 + nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
257 + sar_specs->sub_specs[index].freq_range_index = range_index;
262 +static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
264 + struct cfg80211_registered_device *rdev = info->user_ptr[0];
265 + struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
266 + struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
267 + struct cfg80211_sar_specs *sar_spec;
268 + enum nl80211_sar_type type;
269 + struct nlattr *spec_list;
273 + if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
274 + return -EOPNOTSUPP;
276 + if (!info->attrs[NL80211_ATTR_SAR_SPEC])
279 + nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
280 + info->attrs[NL80211_ATTR_SAR_SPEC],
283 + if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
286 + type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
287 + if (type != rdev->wiphy.sar_capa->type)
291 + nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
294 + if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
297 + sar_spec = kzalloc(sizeof(*sar_spec) +
298 + specs * sizeof(struct cfg80211_sar_sub_specs),
303 + sar_spec->type = type;
305 + nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
306 + nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
307 + spec_list, NULL, NULL);
310 + case NL80211_SAR_TYPE_POWER:
311 + if (nl80211_set_sar_sub_specs(rdev, sar_spec,
324 + sar_spec->num_sub_specs = specs;
326 + rdev->cur_cmd_info = info;
327 + err = rdev_set_sar_specs(rdev, sar_spec);
328 + rdev->cur_cmd_info = NULL;
334 static __genl_const struct genl_ops nl80211_ops[] = {
336 .cmd = NL80211_CMD_GET_WIPHY,
337 @@ -15587,6 +15762,14 @@ static const struct genl_small_ops nl802
338 .internal_flags = NL80211_FLAG_NEED_NETDEV |
339 NL80211_FLAG_NEED_RTNL,
342 + .cmd = NL80211_CMD_SET_SAR_SPECS,
343 + .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
344 + .doit = nl80211_set_sar_specs,
345 + .flags = GENL_UNS_ADMIN_PERM,
346 + .internal_flags = NL80211_FLAG_NEED_WIPHY |
347 + NL80211_FLAG_NEED_RTNL,
351 static struct genl_family nl80211_fam __genl_ro_after_init = {
352 --- a/net/wireless/rdev-ops.h
353 +++ b/net/wireless/rdev-ops.h
354 @@ -1356,4 +1356,16 @@ static inline int rdev_reset_tid_config(
358 +static inline int rdev_set_sar_specs(struct cfg80211_registered_device *rdev,
359 + struct cfg80211_sar_specs *sar)
363 + trace_rdev_set_sar_specs(&rdev->wiphy, sar);
364 + ret = rdev->ops->set_sar_specs(&rdev->wiphy, sar);
365 + trace_rdev_return_int(&rdev->wiphy, ret);
370 #endif /* __CFG80211_RDEV_OPS */
371 --- a/net/wireless/trace.h
372 +++ b/net/wireless/trace.h
373 @@ -3551,6 +3551,25 @@ TRACE_EVENT(rdev_reset_tid_config,
374 TP_printk(WIPHY_PR_FMT ", " NETDEV_PR_FMT ", peer: " MAC_PR_FMT ", tids: 0x%x",
375 WIPHY_PR_ARG, NETDEV_PR_ARG, MAC_PR_ARG(peer), __entry->tids)
378 +TRACE_EVENT(rdev_set_sar_specs,
379 + TP_PROTO(struct wiphy *wiphy, struct cfg80211_sar_specs *sar),
380 + TP_ARGS(wiphy, sar),
388 + __entry->type = sar->type;
389 + __entry->num = sar->num_sub_specs;
392 + TP_printk(WIPHY_PR_FMT ", Set type:%d, num_specs:%d",
393 + WIPHY_PR_ARG, __entry->type, __entry->num)
396 #endif /* !__RDEV_OPS_TRACE || TRACE_HEADER_MULTI_READ */
398 #undef TRACE_INCLUDE_PATH