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