11h.c 9.1 KB

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