scan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Scan implementation for ST-Ericsson CW1200 mac80211 drivers
  4. *
  5. * Copyright (c) 2010, ST-Ericsson
  6. * Author: Dmitry Tarnyagin <[email protected]>
  7. */
  8. #include <linux/sched.h>
  9. #include "cw1200.h"
  10. #include "scan.h"
  11. #include "sta.h"
  12. #include "pm.h"
  13. static void cw1200_scan_restart_delayed(struct cw1200_common *priv);
  14. static int cw1200_scan_start(struct cw1200_common *priv, struct wsm_scan *scan)
  15. {
  16. int ret, i;
  17. int tmo = 2000;
  18. switch (priv->join_status) {
  19. case CW1200_JOIN_STATUS_PRE_STA:
  20. case CW1200_JOIN_STATUS_JOINING:
  21. return -EBUSY;
  22. default:
  23. break;
  24. }
  25. wiphy_dbg(priv->hw->wiphy, "[SCAN] hw req, type %d, %d channels, flags: 0x%x.\n",
  26. scan->type, scan->num_channels, scan->flags);
  27. for (i = 0; i < scan->num_channels; ++i)
  28. tmo += scan->ch[i].max_chan_time + 10;
  29. cancel_delayed_work_sync(&priv->clear_recent_scan_work);
  30. atomic_set(&priv->scan.in_progress, 1);
  31. atomic_set(&priv->recent_scan, 1);
  32. cw1200_pm_stay_awake(&priv->pm_state, msecs_to_jiffies(tmo));
  33. queue_delayed_work(priv->workqueue, &priv->scan.timeout,
  34. msecs_to_jiffies(tmo));
  35. ret = wsm_scan(priv, scan);
  36. if (ret) {
  37. atomic_set(&priv->scan.in_progress, 0);
  38. cancel_delayed_work_sync(&priv->scan.timeout);
  39. cw1200_scan_restart_delayed(priv);
  40. }
  41. return ret;
  42. }
  43. int cw1200_hw_scan(struct ieee80211_hw *hw,
  44. struct ieee80211_vif *vif,
  45. struct ieee80211_scan_request *hw_req)
  46. {
  47. struct cw1200_common *priv = hw->priv;
  48. struct cfg80211_scan_request *req = &hw_req->req;
  49. struct wsm_template_frame frame = {
  50. .frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
  51. };
  52. int i, ret;
  53. if (!priv->vif)
  54. return -EINVAL;
  55. /* Scan when P2P_GO corrupt firmware MiniAP mode */
  56. if (priv->join_status == CW1200_JOIN_STATUS_AP)
  57. return -EOPNOTSUPP;
  58. if (req->n_ssids == 1 && !req->ssids[0].ssid_len)
  59. req->n_ssids = 0;
  60. wiphy_dbg(hw->wiphy, "[SCAN] Scan request for %d SSIDs.\n",
  61. req->n_ssids);
  62. if (req->n_ssids > WSM_SCAN_MAX_NUM_OF_SSIDS)
  63. return -EINVAL;
  64. frame.skb = ieee80211_probereq_get(hw, priv->vif->addr, NULL, 0,
  65. req->ie_len);
  66. if (!frame.skb)
  67. return -ENOMEM;
  68. if (req->ie_len)
  69. skb_put_data(frame.skb, req->ie, req->ie_len);
  70. /* will be unlocked in cw1200_scan_work() */
  71. down(&priv->scan.lock);
  72. mutex_lock(&priv->conf_mutex);
  73. ret = wsm_set_template_frame(priv, &frame);
  74. if (!ret) {
  75. /* Host want to be the probe responder. */
  76. ret = wsm_set_probe_responder(priv, true);
  77. }
  78. if (ret) {
  79. mutex_unlock(&priv->conf_mutex);
  80. up(&priv->scan.lock);
  81. dev_kfree_skb(frame.skb);
  82. return ret;
  83. }
  84. wsm_lock_tx(priv);
  85. BUG_ON(priv->scan.req);
  86. priv->scan.req = req;
  87. priv->scan.n_ssids = 0;
  88. priv->scan.status = 0;
  89. priv->scan.begin = &req->channels[0];
  90. priv->scan.curr = priv->scan.begin;
  91. priv->scan.end = &req->channels[req->n_channels];
  92. priv->scan.output_power = priv->output_power;
  93. for (i = 0; i < req->n_ssids; ++i) {
  94. struct wsm_ssid *dst = &priv->scan.ssids[priv->scan.n_ssids];
  95. memcpy(&dst->ssid[0], req->ssids[i].ssid, sizeof(dst->ssid));
  96. dst->length = req->ssids[i].ssid_len;
  97. ++priv->scan.n_ssids;
  98. }
  99. mutex_unlock(&priv->conf_mutex);
  100. dev_kfree_skb(frame.skb);
  101. queue_work(priv->workqueue, &priv->scan.work);
  102. return 0;
  103. }
  104. void cw1200_scan_work(struct work_struct *work)
  105. {
  106. struct cw1200_common *priv = container_of(work, struct cw1200_common,
  107. scan.work);
  108. struct ieee80211_channel **it;
  109. struct wsm_scan scan = {
  110. .type = WSM_SCAN_TYPE_FOREGROUND,
  111. .flags = WSM_SCAN_FLAG_SPLIT_METHOD,
  112. };
  113. bool first_run = (priv->scan.begin == priv->scan.curr &&
  114. priv->scan.begin != priv->scan.end);
  115. int i;
  116. if (first_run) {
  117. /* Firmware gets crazy if scan request is sent
  118. * when STA is joined but not yet associated.
  119. * Force unjoin in this case.
  120. */
  121. if (cancel_delayed_work_sync(&priv->join_timeout) > 0)
  122. cw1200_join_timeout(&priv->join_timeout.work);
  123. }
  124. mutex_lock(&priv->conf_mutex);
  125. if (first_run) {
  126. if (priv->join_status == CW1200_JOIN_STATUS_STA &&
  127. !(priv->powersave_mode.mode & WSM_PSM_PS)) {
  128. struct wsm_set_pm pm = priv->powersave_mode;
  129. pm.mode = WSM_PSM_PS;
  130. cw1200_set_pm(priv, &pm);
  131. } else if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
  132. /* FW bug: driver has to restart p2p-dev mode
  133. * after scan
  134. */
  135. cw1200_disable_listening(priv);
  136. }
  137. }
  138. if (!priv->scan.req || (priv->scan.curr == priv->scan.end)) {
  139. struct cfg80211_scan_info info = {
  140. .aborted = priv->scan.status ? 1 : 0,
  141. };
  142. if (priv->scan.output_power != priv->output_power)
  143. wsm_set_output_power(priv, priv->output_power * 10);
  144. if (priv->join_status == CW1200_JOIN_STATUS_STA &&
  145. !(priv->powersave_mode.mode & WSM_PSM_PS))
  146. cw1200_set_pm(priv, &priv->powersave_mode);
  147. if (priv->scan.status < 0)
  148. wiphy_warn(priv->hw->wiphy,
  149. "[SCAN] Scan failed (%d).\n",
  150. priv->scan.status);
  151. else if (priv->scan.req)
  152. wiphy_dbg(priv->hw->wiphy,
  153. "[SCAN] Scan completed.\n");
  154. else
  155. wiphy_dbg(priv->hw->wiphy,
  156. "[SCAN] Scan canceled.\n");
  157. priv->scan.req = NULL;
  158. cw1200_scan_restart_delayed(priv);
  159. wsm_unlock_tx(priv);
  160. mutex_unlock(&priv->conf_mutex);
  161. ieee80211_scan_completed(priv->hw, &info);
  162. up(&priv->scan.lock);
  163. return;
  164. } else {
  165. struct ieee80211_channel *first = *priv->scan.curr;
  166. for (it = priv->scan.curr + 1, i = 1;
  167. it != priv->scan.end && i < WSM_SCAN_MAX_NUM_OF_CHANNELS;
  168. ++it, ++i) {
  169. if ((*it)->band != first->band)
  170. break;
  171. if (((*it)->flags ^ first->flags) &
  172. IEEE80211_CHAN_NO_IR)
  173. break;
  174. if (!(first->flags & IEEE80211_CHAN_NO_IR) &&
  175. (*it)->max_power != first->max_power)
  176. break;
  177. }
  178. scan.band = first->band;
  179. if (priv->scan.req->no_cck)
  180. scan.max_tx_rate = WSM_TRANSMIT_RATE_6;
  181. else
  182. scan.max_tx_rate = WSM_TRANSMIT_RATE_1;
  183. scan.num_probes =
  184. (first->flags & IEEE80211_CHAN_NO_IR) ? 0 : 2;
  185. scan.num_ssids = priv->scan.n_ssids;
  186. scan.ssids = &priv->scan.ssids[0];
  187. scan.num_channels = it - priv->scan.curr;
  188. /* TODO: Is it optimal? */
  189. scan.probe_delay = 100;
  190. /* It is not stated in WSM specification, however
  191. * FW team says that driver may not use FG scan
  192. * when joined.
  193. */
  194. if (priv->join_status == CW1200_JOIN_STATUS_STA) {
  195. scan.type = WSM_SCAN_TYPE_BACKGROUND;
  196. scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
  197. }
  198. scan.ch = kcalloc(it - priv->scan.curr,
  199. sizeof(struct wsm_scan_ch),
  200. GFP_KERNEL);
  201. if (!scan.ch) {
  202. priv->scan.status = -ENOMEM;
  203. goto fail;
  204. }
  205. for (i = 0; i < scan.num_channels; ++i) {
  206. scan.ch[i].number = priv->scan.curr[i]->hw_value;
  207. if (priv->scan.curr[i]->flags & IEEE80211_CHAN_NO_IR) {
  208. scan.ch[i].min_chan_time = 50;
  209. scan.ch[i].max_chan_time = 100;
  210. } else {
  211. scan.ch[i].min_chan_time = 10;
  212. scan.ch[i].max_chan_time = 25;
  213. }
  214. }
  215. if (!(first->flags & IEEE80211_CHAN_NO_IR) &&
  216. priv->scan.output_power != first->max_power) {
  217. priv->scan.output_power = first->max_power;
  218. wsm_set_output_power(priv,
  219. priv->scan.output_power * 10);
  220. }
  221. priv->scan.status = cw1200_scan_start(priv, &scan);
  222. kfree(scan.ch);
  223. if (priv->scan.status)
  224. goto fail;
  225. priv->scan.curr = it;
  226. }
  227. mutex_unlock(&priv->conf_mutex);
  228. return;
  229. fail:
  230. priv->scan.curr = priv->scan.end;
  231. mutex_unlock(&priv->conf_mutex);
  232. queue_work(priv->workqueue, &priv->scan.work);
  233. return;
  234. }
  235. static void cw1200_scan_restart_delayed(struct cw1200_common *priv)
  236. {
  237. /* FW bug: driver has to restart p2p-dev mode after scan. */
  238. if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
  239. cw1200_enable_listening(priv);
  240. cw1200_update_filtering(priv);
  241. }
  242. if (priv->delayed_unjoin) {
  243. priv->delayed_unjoin = false;
  244. if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
  245. wsm_unlock_tx(priv);
  246. } else if (priv->delayed_link_loss) {
  247. wiphy_dbg(priv->hw->wiphy, "[CQM] Requeue BSS loss.\n");
  248. priv->delayed_link_loss = 0;
  249. cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
  250. }
  251. }
  252. static void cw1200_scan_complete(struct cw1200_common *priv)
  253. {
  254. queue_delayed_work(priv->workqueue, &priv->clear_recent_scan_work, HZ);
  255. if (priv->scan.direct_probe) {
  256. wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe complete.\n");
  257. cw1200_scan_restart_delayed(priv);
  258. priv->scan.direct_probe = 0;
  259. up(&priv->scan.lock);
  260. wsm_unlock_tx(priv);
  261. } else {
  262. cw1200_scan_work(&priv->scan.work);
  263. }
  264. }
  265. void cw1200_scan_failed_cb(struct cw1200_common *priv)
  266. {
  267. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  268. /* STA is stopped. */
  269. return;
  270. if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
  271. priv->scan.status = -EIO;
  272. queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
  273. }
  274. }
  275. void cw1200_scan_complete_cb(struct cw1200_common *priv,
  276. struct wsm_scan_complete *arg)
  277. {
  278. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
  279. /* STA is stopped. */
  280. return;
  281. if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
  282. priv->scan.status = 1;
  283. queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
  284. }
  285. }
  286. void cw1200_clear_recent_scan_work(struct work_struct *work)
  287. {
  288. struct cw1200_common *priv =
  289. container_of(work, struct cw1200_common,
  290. clear_recent_scan_work.work);
  291. atomic_xchg(&priv->recent_scan, 0);
  292. }
  293. void cw1200_scan_timeout(struct work_struct *work)
  294. {
  295. struct cw1200_common *priv =
  296. container_of(work, struct cw1200_common, scan.timeout.work);
  297. if (atomic_xchg(&priv->scan.in_progress, 0)) {
  298. if (priv->scan.status > 0) {
  299. priv->scan.status = 0;
  300. } else if (!priv->scan.status) {
  301. wiphy_warn(priv->hw->wiphy,
  302. "Timeout waiting for scan complete notification.\n");
  303. priv->scan.status = -ETIMEDOUT;
  304. priv->scan.curr = priv->scan.end;
  305. wsm_stop_scan(priv);
  306. }
  307. cw1200_scan_complete(priv);
  308. }
  309. }
  310. void cw1200_probe_work(struct work_struct *work)
  311. {
  312. struct cw1200_common *priv =
  313. container_of(work, struct cw1200_common, scan.probe_work.work);
  314. u8 queue_id = cw1200_queue_get_queue_id(priv->pending_frame_id);
  315. struct cw1200_queue *queue = &priv->tx_queue[queue_id];
  316. const struct cw1200_txpriv *txpriv;
  317. struct wsm_tx *wsm;
  318. struct wsm_template_frame frame = {
  319. .frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
  320. };
  321. struct wsm_ssid ssids[1] = {{
  322. .length = 0,
  323. } };
  324. struct wsm_scan_ch ch[1] = {{
  325. .min_chan_time = 0,
  326. .max_chan_time = 10,
  327. } };
  328. struct wsm_scan scan = {
  329. .type = WSM_SCAN_TYPE_FOREGROUND,
  330. .num_probes = 1,
  331. .probe_delay = 0,
  332. .num_channels = 1,
  333. .ssids = ssids,
  334. .ch = ch,
  335. };
  336. u8 *ies;
  337. size_t ies_len;
  338. int ret;
  339. wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe work.\n");
  340. mutex_lock(&priv->conf_mutex);
  341. if (down_trylock(&priv->scan.lock)) {
  342. /* Scan is already in progress. Requeue self. */
  343. schedule();
  344. queue_delayed_work(priv->workqueue, &priv->scan.probe_work,
  345. msecs_to_jiffies(100));
  346. mutex_unlock(&priv->conf_mutex);
  347. return;
  348. }
  349. /* Make sure we still have a pending probe req */
  350. if (cw1200_queue_get_skb(queue, priv->pending_frame_id,
  351. &frame.skb, &txpriv)) {
  352. up(&priv->scan.lock);
  353. mutex_unlock(&priv->conf_mutex);
  354. wsm_unlock_tx(priv);
  355. return;
  356. }
  357. wsm = (struct wsm_tx *)frame.skb->data;
  358. scan.max_tx_rate = wsm->max_tx_rate;
  359. scan.band = (priv->channel->band == NL80211_BAND_5GHZ) ?
  360. WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
  361. if (priv->join_status == CW1200_JOIN_STATUS_STA ||
  362. priv->join_status == CW1200_JOIN_STATUS_IBSS) {
  363. scan.type = WSM_SCAN_TYPE_BACKGROUND;
  364. scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
  365. }
  366. ch[0].number = priv->channel->hw_value;
  367. skb_pull(frame.skb, txpriv->offset);
  368. ies = &frame.skb->data[sizeof(struct ieee80211_hdr_3addr)];
  369. ies_len = frame.skb->len - sizeof(struct ieee80211_hdr_3addr);
  370. if (ies_len) {
  371. u8 *ssidie =
  372. (u8 *)cfg80211_find_ie(WLAN_EID_SSID, ies, ies_len);
  373. if (ssidie && ssidie[1] && ssidie[1] <= sizeof(ssids[0].ssid)) {
  374. u8 *nextie = &ssidie[2 + ssidie[1]];
  375. /* Remove SSID from the IE list. It has to be provided
  376. * as a separate argument in cw1200_scan_start call
  377. */
  378. /* Store SSID localy */
  379. ssids[0].length = ssidie[1];
  380. memcpy(ssids[0].ssid, &ssidie[2], ssids[0].length);
  381. scan.num_ssids = 1;
  382. /* Remove SSID from IE list */
  383. ssidie[1] = 0;
  384. memmove(&ssidie[2], nextie, &ies[ies_len] - nextie);
  385. skb_trim(frame.skb, frame.skb->len - ssids[0].length);
  386. }
  387. }
  388. /* FW bug: driver has to restart p2p-dev mode after scan */
  389. if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
  390. cw1200_disable_listening(priv);
  391. ret = wsm_set_template_frame(priv, &frame);
  392. priv->scan.direct_probe = 1;
  393. if (!ret) {
  394. wsm_flush_tx(priv);
  395. ret = cw1200_scan_start(priv, &scan);
  396. }
  397. mutex_unlock(&priv->conf_mutex);
  398. skb_push(frame.skb, txpriv->offset);
  399. if (!ret)
  400. IEEE80211_SKB_CB(frame.skb)->flags |= IEEE80211_TX_STAT_ACK;
  401. BUG_ON(cw1200_queue_remove(queue, priv->pending_frame_id));
  402. if (ret) {
  403. priv->scan.direct_probe = 0;
  404. up(&priv->scan.lock);
  405. wsm_unlock_tx(priv);
  406. }
  407. return;
  408. }