1 /*
2  * NXP Wireless LAN device driver: 802.11h
3  *
4  * Copyright 2011-2020 NXP
5  *
6  * This software file (the "File") is distributed by NXP
7  * under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
17  * this warranty disclaimer.
18  */
19 
20 #include "main.h"
21 #include "fw.h"
22 
23 
mwifiex_init_11h_params(struct mwifiex_private * priv)24 void mwifiex_init_11h_params(struct mwifiex_private *priv)
25 {
26 	priv->state_11h.is_11h_enabled = true;
27 	priv->state_11h.is_11h_active = false;
28 }
29 
mwifiex_is_11h_active(struct mwifiex_private * priv)30 inline int mwifiex_is_11h_active(struct mwifiex_private *priv)
31 {
32 	return priv->state_11h.is_11h_active;
33 }
34 /* This function appends 11h info to a buffer while joining an
35  * infrastructure BSS
36  */
37 static void
mwifiex_11h_process_infra_join(struct mwifiex_private * priv,u8 ** buffer,struct mwifiex_bssdescriptor * bss_desc)38 mwifiex_11h_process_infra_join(struct mwifiex_private *priv, u8 **buffer,
39 			       struct mwifiex_bssdescriptor *bss_desc)
40 {
41 	struct mwifiex_ie_types_header *ie_header;
42 	struct mwifiex_ie_types_pwr_capability *cap;
43 	struct mwifiex_ie_types_local_pwr_constraint *constraint;
44 	struct ieee80211_supported_band *sband;
45 	u8 radio_type;
46 	int i;
47 
48 	if (!buffer || !(*buffer))
49 		return;
50 
51 	radio_type = mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
52 	sband = priv->wdev.wiphy->bands[radio_type];
53 
54 	cap = (struct mwifiex_ie_types_pwr_capability *)*buffer;
55 	cap->header.type = cpu_to_le16(WLAN_EID_PWR_CAPABILITY);
56 	cap->header.len = cpu_to_le16(2);
57 	cap->min_pwr = 0;
58 	cap->max_pwr = 0;
59 	*buffer += sizeof(*cap);
60 
61 	constraint = (struct mwifiex_ie_types_local_pwr_constraint *)*buffer;
62 	constraint->header.type = cpu_to_le16(WLAN_EID_PWR_CONSTRAINT);
63 	constraint->header.len = cpu_to_le16(2);
64 	constraint->chan = bss_desc->channel;
65 	constraint->constraint = bss_desc->local_constraint;
66 	*buffer += sizeof(*constraint);
67 
68 	ie_header = (struct mwifiex_ie_types_header *)*buffer;
69 	ie_header->type = cpu_to_le16(TLV_TYPE_PASSTHROUGH);
70 	ie_header->len  = cpu_to_le16(2 * sband->n_channels + 2);
71 	*buffer += sizeof(*ie_header);
72 	*(*buffer)++ = WLAN_EID_SUPPORTED_CHANNELS;
73 	*(*buffer)++ = 2 * sband->n_channels;
74 	for (i = 0; i < sband->n_channels; i++) {
75 		*(*buffer)++ = ieee80211_frequency_to_channel(
76 					sband->channels[i].center_freq);
77 		*(*buffer)++ = 1; /* one channel in the subband */
78 	}
79 }
80 
81 /* Enable or disable the 11h extensions in the firmware */
mwifiex_11h_activate(struct mwifiex_private * priv,bool flag)82 int mwifiex_11h_activate(struct mwifiex_private *priv, bool flag)
83 {
84 	u32 enable = flag;
85 
86 	/* enable master mode radar detection on AP interface */
87 	if ((GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) && enable)
88 		enable |= MWIFIEX_MASTER_RADAR_DET_MASK;
89 
90 	return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
91 				HostCmd_ACT_GEN_SET, DOT11H_I, &enable, true);
92 }
93 
94 /* This functions processes TLV buffer for a pending BSS Join command.
95  *
96  * Activate 11h functionality in the firmware if the spectrum management
97  * capability bit is found in the network we are joining. Also, necessary
98  * TLVs are set based on requested network's 11h capability.
99  */
mwifiex_11h_process_join(struct mwifiex_private * priv,u8 ** buffer,struct mwifiex_bssdescriptor * bss_desc)100 void mwifiex_11h_process_join(struct mwifiex_private *priv, u8 **buffer,
101 			      struct mwifiex_bssdescriptor *bss_desc)
102 {
103 	if (bss_desc->sensed_11h) {
104 		/* Activate 11h functions in firmware, turns on capability
105 		 * bit
106 		 */
107 		mwifiex_11h_activate(priv, true);
108 		priv->state_11h.is_11h_active = true;
109 		bss_desc->cap_info_bitmap |= WLAN_CAPABILITY_SPECTRUM_MGMT;
110 		mwifiex_11h_process_infra_join(priv, buffer, bss_desc);
111 	} else {
112 		/* Deactivate 11h functions in the firmware */
113 		mwifiex_11h_activate(priv, false);
114 		priv->state_11h.is_11h_active = false;
115 		bss_desc->cap_info_bitmap &= ~WLAN_CAPABILITY_SPECTRUM_MGMT;
116 	}
117 }
118 
119 /* This is DFS CAC work queue function.
120  * This delayed work emits CAC finished event for cfg80211 if
121  * CAC was started earlier.
122  */
mwifiex_dfs_cac_work_queue(struct work_struct * work)123 void mwifiex_dfs_cac_work_queue(struct work_struct *work)
124 {
125 	struct cfg80211_chan_def chandef;
126 	struct delayed_work *delayed_work = to_delayed_work(work);
127 	struct mwifiex_private *priv =
128 			container_of(delayed_work, struct mwifiex_private,
129 				     dfs_cac_work);
130 
131 	chandef = priv->dfs_chandef;
132 	if (priv->wdev.cac_started) {
133 		mwifiex_dbg(priv->adapter, MSG,
134 			    "CAC timer finished; No radar detected\n");
135 		cfg80211_cac_event(priv->netdev, &chandef,
136 				   NL80211_RADAR_CAC_FINISHED,
137 				   GFP_KERNEL);
138 	}
139 }
140 
141 /* This function prepares channel report request command to FW for
142  * starting radar detection.
143  */
mwifiex_cmd_issue_chan_report_request(struct mwifiex_private * priv,struct host_cmd_ds_command * cmd,void * data_buf)144 int mwifiex_cmd_issue_chan_report_request(struct mwifiex_private *priv,
145 					  struct host_cmd_ds_command *cmd,
146 					  void *data_buf)
147 {
148 	struct host_cmd_ds_chan_rpt_req *cr_req = &cmd->params.chan_rpt_req;
149 	struct mwifiex_radar_params *radar_params = (void *)data_buf;
150 
151 	cmd->command = cpu_to_le16(HostCmd_CMD_CHAN_REPORT_REQUEST);
152 	cmd->size = cpu_to_le16(S_DS_GEN);
153 	le16_unaligned_add_cpu(&cmd->size,
154 			       sizeof(struct host_cmd_ds_chan_rpt_req));
155 
156 	cr_req->chan_desc.start_freq = cpu_to_le16(MWIFIEX_A_BAND_START_FREQ);
157 	cr_req->chan_desc.chan_num = radar_params->chandef->chan->hw_value;
158 	cr_req->chan_desc.chan_width = radar_params->chandef->width;
159 	cr_req->msec_dwell_time = cpu_to_le32(radar_params->cac_time_ms);
160 
161 	if (radar_params->cac_time_ms)
162 		mwifiex_dbg(priv->adapter, MSG,
163 			    "11h: issuing DFS Radar check for channel=%d\n",
164 			    radar_params->chandef->chan->hw_value);
165 	else
166 		mwifiex_dbg(priv->adapter, MSG, "cancelling CAC\n");
167 
168 	return 0;
169 }
170 
mwifiex_stop_radar_detection(struct mwifiex_private * priv,struct cfg80211_chan_def * chandef)171 int mwifiex_stop_radar_detection(struct mwifiex_private *priv,
172 				 struct cfg80211_chan_def *chandef)
173 {
174 	struct mwifiex_radar_params radar_params;
175 
176 	memset(&radar_params, 0, sizeof(struct mwifiex_radar_params));
177 	radar_params.chandef = chandef;
178 	radar_params.cac_time_ms = 0;
179 
180 	return mwifiex_send_cmd(priv, HostCmd_CMD_CHAN_REPORT_REQUEST,
181 				HostCmd_ACT_GEN_SET, 0, &radar_params, true);
182 }
183 
184 /* This function is to abort ongoing CAC upon stopping AP operations
185  * or during unload.
186  */
mwifiex_abort_cac(struct mwifiex_private * priv)187 void mwifiex_abort_cac(struct mwifiex_private *priv)
188 {
189 	if (priv->wdev.cac_started) {
190 		if (mwifiex_stop_radar_detection(priv, &priv->dfs_chandef))
191 			mwifiex_dbg(priv->adapter, ERROR,
192 				    "failed to stop CAC in FW\n");
193 		mwifiex_dbg(priv->adapter, MSG,
194 			    "Aborting delayed work for CAC.\n");
195 		cancel_delayed_work_sync(&priv->dfs_cac_work);
196 		cfg80211_cac_event(priv->netdev, &priv->dfs_chandef,
197 				   NL80211_RADAR_CAC_ABORTED, GFP_KERNEL);
198 	}
199 }
200 
201 /* This function handles channel report event from FW during CAC period.
202  * If radar is detected during CAC, driver indicates the same to cfg80211
203  * and also cancels ongoing delayed work.
204  */
mwifiex_11h_handle_chanrpt_ready(struct mwifiex_private * priv,struct sk_buff * skb)205 int mwifiex_11h_handle_chanrpt_ready(struct mwifiex_private *priv,
206 				     struct sk_buff *skb)
207 {
208 	struct host_cmd_ds_chan_rpt_event *rpt_event;
209 	struct mwifiex_ie_types_chan_rpt_data *rpt;
210 	u8 *evt_buf;
211 	u16 event_len, tlv_len;
212 
213 	rpt_event = (void *)(skb->data + sizeof(u32));
214 	event_len = skb->len - (sizeof(struct host_cmd_ds_chan_rpt_event)+
215 				sizeof(u32));
216 
217 	if (le32_to_cpu(rpt_event->result) != HostCmd_RESULT_OK) {
218 		mwifiex_dbg(priv->adapter, ERROR,
219 			    "Error in channel report event\n");
220 		return -1;
221 	}
222 
223 	evt_buf = (void *)&rpt_event->tlvbuf;
224 
225 	while (event_len >= sizeof(struct mwifiex_ie_types_header)) {
226 		rpt = (void *)&rpt_event->tlvbuf;
227 		tlv_len = le16_to_cpu(rpt->header.len);
228 
229 		switch (le16_to_cpu(rpt->header.type)) {
230 		case TLV_TYPE_CHANRPT_11H_BASIC:
231 			if (rpt->map.radar) {
232 				mwifiex_dbg(priv->adapter, MSG,
233 					    "RADAR Detected on channel %d!\n",
234 					    priv->dfs_chandef.chan->hw_value);
235 				cancel_delayed_work_sync(&priv->dfs_cac_work);
236 				cfg80211_cac_event(priv->netdev,
237 						   &priv->dfs_chandef,
238 						   NL80211_RADAR_DETECTED,
239 						   GFP_KERNEL);
240 			}
241 			break;
242 		default:
243 			break;
244 		}
245 
246 		evt_buf += (tlv_len + sizeof(rpt->header));
247 		event_len -= (tlv_len + sizeof(rpt->header));
248 	}
249 
250 	return 0;
251 }
252 
253 /* Handler for radar detected event from FW.*/
mwifiex_11h_handle_radar_detected(struct mwifiex_private * priv,struct sk_buff * skb)254 int mwifiex_11h_handle_radar_detected(struct mwifiex_private *priv,
255 				      struct sk_buff *skb)
256 {
257 	struct mwifiex_radar_det_event *rdr_event;
258 
259 	rdr_event = (void *)(skb->data + sizeof(u32));
260 
261 	mwifiex_dbg(priv->adapter, MSG,
262 		    "radar detected; indicating kernel\n");
263 	if (mwifiex_stop_radar_detection(priv, &priv->dfs_chandef))
264 		mwifiex_dbg(priv->adapter, ERROR,
265 			    "Failed to stop CAC in FW\n");
266 	cfg80211_radar_event(priv->adapter->wiphy, &priv->dfs_chandef,
267 			     GFP_KERNEL);
268 	mwifiex_dbg(priv->adapter, MSG, "regdomain: %d\n",
269 		    rdr_event->reg_domain);
270 	mwifiex_dbg(priv->adapter, MSG, "radar detection type: %d\n",
271 		    rdr_event->det_type);
272 
273 	return 0;
274 }
275 
276 /* This is work queue function for channel switch handling.
277  * This function takes care of updating new channel definitin to
278  * bss config structure, restart AP and indicate channel switch success
279  * to cfg80211.
280  */
mwifiex_dfs_chan_sw_work_queue(struct work_struct * work)281 void mwifiex_dfs_chan_sw_work_queue(struct work_struct *work)
282 {
283 	struct mwifiex_uap_bss_param *bss_cfg;
284 	struct delayed_work *delayed_work = to_delayed_work(work);
285 	struct mwifiex_private *priv =
286 			container_of(delayed_work, struct mwifiex_private,
287 				     dfs_chan_sw_work);
288 
289 	bss_cfg = &priv->bss_cfg;
290 	if (!bss_cfg->beacon_period) {
291 		mwifiex_dbg(priv->adapter, ERROR,
292 			    "channel switch: AP already stopped\n");
293 		return;
294 	}
295 
296 	mwifiex_uap_set_channel(priv, bss_cfg, priv->dfs_chandef);
297 
298 	if (mwifiex_config_start_uap(priv, bss_cfg)) {
299 		mwifiex_dbg(priv->adapter, ERROR,
300 			    "Failed to start AP after channel switch\n");
301 		return;
302 	}
303 
304 	mwifiex_dbg(priv->adapter, MSG,
305 		    "indicating channel switch completion to kernel\n");
306 	cfg80211_ch_switch_notify(priv->netdev, &priv->dfs_chandef);
307 }
308