scan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of wl12xx
  4. *
  5. * Copyright (C) 2012 Texas Instruments. All rights reserved.
  6. */
  7. #include <linux/ieee80211.h>
  8. #include "scan.h"
  9. #include "../wlcore/debug.h"
  10. #include "../wlcore/tx.h"
  11. static int wl1271_get_scan_channels(struct wl1271 *wl,
  12. struct cfg80211_scan_request *req,
  13. struct basic_scan_channel_params *channels,
  14. enum nl80211_band band, bool passive)
  15. {
  16. struct conf_scan_settings *c = &wl->conf.scan;
  17. int i, j;
  18. u32 flags;
  19. for (i = 0, j = 0;
  20. i < req->n_channels && j < WL1271_SCAN_MAX_CHANNELS;
  21. i++) {
  22. flags = req->channels[i]->flags;
  23. if (!test_bit(i, wl->scan.scanned_ch) &&
  24. !(flags & IEEE80211_CHAN_DISABLED) &&
  25. (req->channels[i]->band == band) &&
  26. /*
  27. * In passive scans, we scan all remaining
  28. * channels, even if not marked as such.
  29. * In active scans, we only scan channels not
  30. * marked as passive.
  31. */
  32. (passive || !(flags & IEEE80211_CHAN_NO_IR))) {
  33. wl1271_debug(DEBUG_SCAN, "band %d, center_freq %d ",
  34. req->channels[i]->band,
  35. req->channels[i]->center_freq);
  36. wl1271_debug(DEBUG_SCAN, "hw_value %d, flags %X",
  37. req->channels[i]->hw_value,
  38. req->channels[i]->flags);
  39. wl1271_debug(DEBUG_SCAN,
  40. "max_antenna_gain %d, max_power %d",
  41. req->channels[i]->max_antenna_gain,
  42. req->channels[i]->max_power);
  43. wl1271_debug(DEBUG_SCAN, "beacon_found %d",
  44. req->channels[i]->beacon_found);
  45. if (!passive) {
  46. channels[j].min_duration =
  47. cpu_to_le32(c->min_dwell_time_active);
  48. channels[j].max_duration =
  49. cpu_to_le32(c->max_dwell_time_active);
  50. } else {
  51. channels[j].min_duration =
  52. cpu_to_le32(c->dwell_time_passive);
  53. channels[j].max_duration =
  54. cpu_to_le32(c->dwell_time_passive);
  55. }
  56. channels[j].early_termination = 0;
  57. channels[j].tx_power_att = req->channels[i]->max_power;
  58. channels[j].channel = req->channels[i]->hw_value;
  59. memset(&channels[j].bssid_lsb, 0xff, 4);
  60. memset(&channels[j].bssid_msb, 0xff, 2);
  61. /* Mark the channels we already used */
  62. set_bit(i, wl->scan.scanned_ch);
  63. j++;
  64. }
  65. }
  66. return j;
  67. }
  68. #define WL1271_NOTHING_TO_SCAN 1
  69. static int wl1271_scan_send(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  70. enum nl80211_band band,
  71. bool passive, u32 basic_rate)
  72. {
  73. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  74. struct wl1271_cmd_scan *cmd;
  75. struct wl1271_cmd_trigger_scan_to *trigger;
  76. int ret;
  77. u16 scan_options = 0;
  78. /* skip active scans if we don't have SSIDs */
  79. if (!passive && wl->scan.req->n_ssids == 0)
  80. return WL1271_NOTHING_TO_SCAN;
  81. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  82. trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
  83. if (!cmd || !trigger) {
  84. ret = -ENOMEM;
  85. goto out;
  86. }
  87. if (wl->conf.scan.split_scan_timeout)
  88. scan_options |= WL1271_SCAN_OPT_SPLIT_SCAN;
  89. if (passive)
  90. scan_options |= WL1271_SCAN_OPT_PASSIVE;
  91. /* scan on the dev role if the regular one is not started */
  92. if (wlcore_is_p2p_mgmt(wlvif))
  93. cmd->params.role_id = wlvif->dev_role_id;
  94. else
  95. cmd->params.role_id = wlvif->role_id;
  96. if (WARN_ON(cmd->params.role_id == WL12XX_INVALID_ROLE_ID)) {
  97. ret = -EINVAL;
  98. goto out;
  99. }
  100. cmd->params.scan_options = cpu_to_le16(scan_options);
  101. cmd->params.n_ch = wl1271_get_scan_channels(wl, wl->scan.req,
  102. cmd->channels,
  103. band, passive);
  104. if (cmd->params.n_ch == 0) {
  105. ret = WL1271_NOTHING_TO_SCAN;
  106. goto out;
  107. }
  108. cmd->params.tx_rate = cpu_to_le32(basic_rate);
  109. cmd->params.n_probe_reqs = wl->conf.scan.num_probe_reqs;
  110. cmd->params.tid_trigger = CONF_TX_AC_ANY_TID;
  111. cmd->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
  112. if (band == NL80211_BAND_2GHZ)
  113. cmd->params.band = WL1271_SCAN_BAND_2_4_GHZ;
  114. else
  115. cmd->params.band = WL1271_SCAN_BAND_5_GHZ;
  116. if (wl->scan.ssid_len) {
  117. cmd->params.ssid_len = wl->scan.ssid_len;
  118. memcpy(cmd->params.ssid, wl->scan.ssid, wl->scan.ssid_len);
  119. }
  120. memcpy(cmd->addr, vif->addr, ETH_ALEN);
  121. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  122. cmd->params.role_id, band,
  123. wl->scan.ssid, wl->scan.ssid_len,
  124. wl->scan.req->ie,
  125. wl->scan.req->ie_len, NULL, 0, false);
  126. if (ret < 0) {
  127. wl1271_error("PROBE request template failed");
  128. goto out;
  129. }
  130. trigger->timeout = cpu_to_le32(wl->conf.scan.split_scan_timeout);
  131. ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
  132. sizeof(*trigger), 0);
  133. if (ret < 0) {
  134. wl1271_error("trigger scan to failed for hw scan");
  135. goto out;
  136. }
  137. wl1271_dump(DEBUG_SCAN, "SCAN: ", cmd, sizeof(*cmd));
  138. ret = wl1271_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd), 0);
  139. if (ret < 0) {
  140. wl1271_error("SCAN failed");
  141. goto out;
  142. }
  143. out:
  144. kfree(cmd);
  145. kfree(trigger);
  146. return ret;
  147. }
  148. int wl12xx_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  149. {
  150. struct wl1271_cmd_header *cmd = NULL;
  151. int ret = 0;
  152. if (WARN_ON(wl->scan.state == WL1271_SCAN_STATE_IDLE))
  153. return -EINVAL;
  154. wl1271_debug(DEBUG_CMD, "cmd scan stop");
  155. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  156. if (!cmd) {
  157. ret = -ENOMEM;
  158. goto out;
  159. }
  160. ret = wl1271_cmd_send(wl, CMD_STOP_SCAN, cmd,
  161. sizeof(*cmd), 0);
  162. if (ret < 0) {
  163. wl1271_error("cmd stop_scan failed");
  164. goto out;
  165. }
  166. out:
  167. kfree(cmd);
  168. return ret;
  169. }
  170. void wl1271_scan_stm(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  171. {
  172. int ret = 0;
  173. enum nl80211_band band;
  174. u32 rate, mask;
  175. switch (wl->scan.state) {
  176. case WL1271_SCAN_STATE_IDLE:
  177. break;
  178. case WL1271_SCAN_STATE_2GHZ_ACTIVE:
  179. band = NL80211_BAND_2GHZ;
  180. mask = wlvif->bitrate_masks[band];
  181. if (wl->scan.req->no_cck) {
  182. mask &= ~CONF_TX_CCK_RATES;
  183. if (!mask)
  184. mask = CONF_TX_RATE_MASK_BASIC_P2P;
  185. }
  186. rate = wl1271_tx_min_rate_get(wl, mask);
  187. ret = wl1271_scan_send(wl, wlvif, band, false, rate);
  188. if (ret == WL1271_NOTHING_TO_SCAN) {
  189. wl->scan.state = WL1271_SCAN_STATE_2GHZ_PASSIVE;
  190. wl1271_scan_stm(wl, wlvif);
  191. }
  192. break;
  193. case WL1271_SCAN_STATE_2GHZ_PASSIVE:
  194. band = NL80211_BAND_2GHZ;
  195. mask = wlvif->bitrate_masks[band];
  196. if (wl->scan.req->no_cck) {
  197. mask &= ~CONF_TX_CCK_RATES;
  198. if (!mask)
  199. mask = CONF_TX_RATE_MASK_BASIC_P2P;
  200. }
  201. rate = wl1271_tx_min_rate_get(wl, mask);
  202. ret = wl1271_scan_send(wl, wlvif, band, true, rate);
  203. if (ret == WL1271_NOTHING_TO_SCAN) {
  204. if (wl->enable_11a)
  205. wl->scan.state = WL1271_SCAN_STATE_5GHZ_ACTIVE;
  206. else
  207. wl->scan.state = WL1271_SCAN_STATE_DONE;
  208. wl1271_scan_stm(wl, wlvif);
  209. }
  210. break;
  211. case WL1271_SCAN_STATE_5GHZ_ACTIVE:
  212. band = NL80211_BAND_5GHZ;
  213. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  214. ret = wl1271_scan_send(wl, wlvif, band, false, rate);
  215. if (ret == WL1271_NOTHING_TO_SCAN) {
  216. wl->scan.state = WL1271_SCAN_STATE_5GHZ_PASSIVE;
  217. wl1271_scan_stm(wl, wlvif);
  218. }
  219. break;
  220. case WL1271_SCAN_STATE_5GHZ_PASSIVE:
  221. band = NL80211_BAND_5GHZ;
  222. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  223. ret = wl1271_scan_send(wl, wlvif, band, true, rate);
  224. if (ret == WL1271_NOTHING_TO_SCAN) {
  225. wl->scan.state = WL1271_SCAN_STATE_DONE;
  226. wl1271_scan_stm(wl, wlvif);
  227. }
  228. break;
  229. case WL1271_SCAN_STATE_DONE:
  230. wl->scan.failed = false;
  231. cancel_delayed_work(&wl->scan_complete_work);
  232. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  233. msecs_to_jiffies(0));
  234. break;
  235. default:
  236. wl1271_error("invalid scan state");
  237. break;
  238. }
  239. if (ret < 0) {
  240. cancel_delayed_work(&wl->scan_complete_work);
  241. ieee80211_queue_delayed_work(wl->hw, &wl->scan_complete_work,
  242. msecs_to_jiffies(0));
  243. }
  244. }
  245. static void wl12xx_adjust_channels(struct wl1271_cmd_sched_scan_config *cmd,
  246. struct wlcore_scan_channels *cmd_channels)
  247. {
  248. memcpy(cmd->passive, cmd_channels->passive, sizeof(cmd->passive));
  249. memcpy(cmd->active, cmd_channels->active, sizeof(cmd->active));
  250. cmd->dfs = cmd_channels->dfs;
  251. cmd->n_pactive_ch = cmd_channels->passive_active;
  252. memcpy(cmd->channels_2, cmd_channels->channels_2,
  253. sizeof(cmd->channels_2));
  254. memcpy(cmd->channels_5, cmd_channels->channels_5,
  255. sizeof(cmd->channels_5));
  256. /* channels_4 are not supported, so no need to copy them */
  257. }
  258. int wl1271_scan_sched_scan_config(struct wl1271 *wl,
  259. struct wl12xx_vif *wlvif,
  260. struct cfg80211_sched_scan_request *req,
  261. struct ieee80211_scan_ies *ies)
  262. {
  263. struct wl1271_cmd_sched_scan_config *cfg = NULL;
  264. struct wlcore_scan_channels *cfg_channels = NULL;
  265. struct conf_sched_scan_settings *c = &wl->conf.sched_scan;
  266. int i, ret;
  267. bool force_passive = !req->n_ssids;
  268. wl1271_debug(DEBUG_CMD, "cmd sched_scan scan config");
  269. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  270. if (!cfg)
  271. return -ENOMEM;
  272. cfg->role_id = wlvif->role_id;
  273. cfg->rssi_threshold = c->rssi_threshold;
  274. cfg->snr_threshold = c->snr_threshold;
  275. cfg->n_probe_reqs = c->num_probe_reqs;
  276. /* cycles set to 0 it means infinite (until manually stopped) */
  277. cfg->cycles = 0;
  278. /* report APs when at least 1 is found */
  279. cfg->report_after = 1;
  280. /* don't stop scanning automatically when something is found */
  281. cfg->terminate = 0;
  282. cfg->tag = WL1271_SCAN_DEFAULT_TAG;
  283. /* don't filter on BSS type */
  284. cfg->bss_type = SCAN_BSS_TYPE_ANY;
  285. /* currently NL80211 supports only a single interval */
  286. for (i = 0; i < SCAN_MAX_CYCLE_INTERVALS; i++)
  287. cfg->intervals[i] = cpu_to_le32(req->scan_plans[0].interval *
  288. MSEC_PER_SEC);
  289. cfg->ssid_len = 0;
  290. ret = wlcore_scan_sched_scan_ssid_list(wl, wlvif, req);
  291. if (ret < 0)
  292. goto out;
  293. cfg->filter_type = ret;
  294. wl1271_debug(DEBUG_SCAN, "filter_type = %d", cfg->filter_type);
  295. cfg_channels = kzalloc(sizeof(*cfg_channels), GFP_KERNEL);
  296. if (!cfg_channels) {
  297. ret = -ENOMEM;
  298. goto out;
  299. }
  300. if (!wlcore_set_scan_chan_params(wl, cfg_channels, req->channels,
  301. req->n_channels, req->n_ssids,
  302. SCAN_TYPE_PERIODIC)) {
  303. wl1271_error("scan channel list is empty");
  304. ret = -EINVAL;
  305. goto out;
  306. }
  307. wl12xx_adjust_channels(cfg, cfg_channels);
  308. if (!force_passive && cfg->active[0]) {
  309. u8 band = NL80211_BAND_2GHZ;
  310. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  311. wlvif->role_id, band,
  312. req->ssids[0].ssid,
  313. req->ssids[0].ssid_len,
  314. ies->ies[band],
  315. ies->len[band],
  316. ies->common_ies,
  317. ies->common_ie_len,
  318. true);
  319. if (ret < 0) {
  320. wl1271_error("2.4GHz PROBE request template failed");
  321. goto out;
  322. }
  323. }
  324. if (!force_passive && cfg->active[1]) {
  325. u8 band = NL80211_BAND_5GHZ;
  326. ret = wl12xx_cmd_build_probe_req(wl, wlvif,
  327. wlvif->role_id, band,
  328. req->ssids[0].ssid,
  329. req->ssids[0].ssid_len,
  330. ies->ies[band],
  331. ies->len[band],
  332. ies->common_ies,
  333. ies->common_ie_len,
  334. true);
  335. if (ret < 0) {
  336. wl1271_error("5GHz PROBE request template failed");
  337. goto out;
  338. }
  339. }
  340. wl1271_dump(DEBUG_SCAN, "SCAN_CFG: ", cfg, sizeof(*cfg));
  341. ret = wl1271_cmd_send(wl, CMD_CONNECTION_SCAN_CFG, cfg,
  342. sizeof(*cfg), 0);
  343. if (ret < 0) {
  344. wl1271_error("SCAN configuration failed");
  345. goto out;
  346. }
  347. out:
  348. kfree(cfg_channels);
  349. kfree(cfg);
  350. return ret;
  351. }
  352. int wl1271_scan_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  353. {
  354. struct wl1271_cmd_sched_scan_start *start;
  355. int ret = 0;
  356. wl1271_debug(DEBUG_CMD, "cmd periodic scan start");
  357. if (wlvif->bss_type != BSS_TYPE_STA_BSS)
  358. return -EOPNOTSUPP;
  359. if ((wl->quirks & WLCORE_QUIRK_NO_SCHED_SCAN_WHILE_CONN) &&
  360. test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
  361. return -EBUSY;
  362. start = kzalloc(sizeof(*start), GFP_KERNEL);
  363. if (!start)
  364. return -ENOMEM;
  365. start->role_id = wlvif->role_id;
  366. start->tag = WL1271_SCAN_DEFAULT_TAG;
  367. ret = wl1271_cmd_send(wl, CMD_START_PERIODIC_SCAN, start,
  368. sizeof(*start), 0);
  369. if (ret < 0) {
  370. wl1271_error("failed to send scan start command");
  371. goto out_free;
  372. }
  373. out_free:
  374. kfree(start);
  375. return ret;
  376. }
  377. int wl12xx_sched_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  378. struct cfg80211_sched_scan_request *req,
  379. struct ieee80211_scan_ies *ies)
  380. {
  381. int ret;
  382. ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
  383. if (ret < 0)
  384. return ret;
  385. return wl1271_scan_sched_scan_start(wl, wlvif);
  386. }
  387. void wl12xx_scan_sched_scan_stop(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  388. {
  389. struct wl1271_cmd_sched_scan_stop *stop;
  390. int ret = 0;
  391. wl1271_debug(DEBUG_CMD, "cmd periodic scan stop");
  392. /* FIXME: what to do if alloc'ing to stop fails? */
  393. stop = kzalloc(sizeof(*stop), GFP_KERNEL);
  394. if (!stop) {
  395. wl1271_error("failed to alloc memory to send sched scan stop");
  396. return;
  397. }
  398. stop->role_id = wlvif->role_id;
  399. stop->tag = WL1271_SCAN_DEFAULT_TAG;
  400. ret = wl1271_cmd_send(wl, CMD_STOP_PERIODIC_SCAN, stop,
  401. sizeof(*stop), 0);
  402. if (ret < 0) {
  403. wl1271_error("failed to send sched scan stop command");
  404. goto out_free;
  405. }
  406. out_free:
  407. kfree(stop);
  408. }
  409. int wl12xx_scan_start(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  410. struct cfg80211_scan_request *req)
  411. {
  412. wl1271_scan_stm(wl, wlvif);
  413. return 0;
  414. }
  415. void wl12xx_scan_completed(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  416. {
  417. wl1271_scan_stm(wl, wlvif);
  418. }