scan.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/etherdevice.h>
  66. #include <net/mac80211.h>
  67. #include "mvm.h"
  68. #include "iwl-eeprom-parse.h"
  69. #include "fw-api-scan.h"
  70. #define IWL_PLCP_QUIET_THRESH 1
  71. #define IWL_ACTIVE_QUIET_TIME 10
  72. struct iwl_mvm_scan_params {
  73. u32 max_out_time;
  74. u32 suspend_time;
  75. bool passive_fragmented;
  76. struct _dwell {
  77. u16 passive;
  78. u16 active;
  79. } dwell[IEEE80211_NUM_BANDS];
  80. };
  81. static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
  82. {
  83. u16 rx_chain;
  84. u8 rx_ant;
  85. if (mvm->scan_rx_ant != ANT_NONE)
  86. rx_ant = mvm->scan_rx_ant;
  87. else
  88. rx_ant = mvm->fw->valid_rx_ant;
  89. rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
  90. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
  91. rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
  92. rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
  93. return cpu_to_le16(rx_chain);
  94. }
  95. static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
  96. {
  97. if (band == IEEE80211_BAND_2GHZ)
  98. return cpu_to_le32(PHY_BAND_24);
  99. else
  100. return cpu_to_le32(PHY_BAND_5);
  101. }
  102. static inline __le32
  103. iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
  104. bool no_cck)
  105. {
  106. u32 tx_ant;
  107. mvm->scan_last_antenna_idx =
  108. iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
  109. mvm->scan_last_antenna_idx);
  110. tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
  111. if (band == IEEE80211_BAND_2GHZ && !no_cck)
  112. return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
  113. tx_ant);
  114. else
  115. return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
  116. }
  117. /*
  118. * We insert the SSIDs in an inverted order, because the FW will
  119. * invert it back. The most prioritized SSID, which is first in the
  120. * request list, is not copied here, but inserted directly to the probe
  121. * request.
  122. */
  123. static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
  124. struct cfg80211_ssid *ssids,
  125. int n_ssids, int first)
  126. {
  127. int fw_idx, req_idx;
  128. for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
  129. req_idx--, fw_idx++) {
  130. cmd_ssid[fw_idx].id = WLAN_EID_SSID;
  131. cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
  132. memcpy(cmd_ssid[fw_idx].ssid,
  133. ssids[req_idx].ssid,
  134. ssids[req_idx].ssid_len);
  135. }
  136. }
  137. /*
  138. * If req->n_ssids > 0, it means we should do an active scan.
  139. * In case of active scan w/o directed scan, we receive a zero-length SSID
  140. * just to notify that this scan is active and not passive.
  141. * In order to notify the FW of the number of SSIDs we wish to scan (including
  142. * the zero-length one), we need to set the corresponding bits in chan->type,
  143. * one for each SSID, and set the active bit (first). If the first SSID is
  144. * already included in the probe template, so we need to set only
  145. * req->n_ssids - 1 bits in addition to the first bit.
  146. */
  147. static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
  148. {
  149. if (band == IEEE80211_BAND_2GHZ)
  150. return 20 + 3 * (n_ssids + 1);
  151. return 10 + 2 * (n_ssids + 1);
  152. }
  153. static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
  154. {
  155. return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
  156. }
  157. static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
  158. struct cfg80211_scan_request *req,
  159. bool basic_ssid,
  160. struct iwl_mvm_scan_params *params)
  161. {
  162. struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
  163. (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
  164. int i;
  165. int type = BIT(req->n_ssids) - 1;
  166. enum ieee80211_band band = req->channels[0]->band;
  167. if (!basic_ssid)
  168. type |= BIT(req->n_ssids);
  169. for (i = 0; i < cmd->channel_count; i++) {
  170. chan->channel = cpu_to_le16(req->channels[i]->hw_value);
  171. chan->type = cpu_to_le32(type);
  172. if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
  173. chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
  174. chan->active_dwell = cpu_to_le16(params->dwell[band].active);
  175. chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
  176. chan->iteration_count = cpu_to_le16(1);
  177. chan++;
  178. }
  179. }
  180. /*
  181. * Fill in probe request with the following parameters:
  182. * TA is our vif HW address, which mac80211 ensures we have.
  183. * Packet is broadcasted, so this is both SA and DA.
  184. * The probe request IE is made out of two: first comes the most prioritized
  185. * SSID if a directed scan is requested. Second comes whatever extra
  186. * information was given to us as the scan request IE.
  187. */
  188. static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
  189. int n_ssids, const u8 *ssid, int ssid_len,
  190. const u8 *band_ie, int band_ie_len,
  191. const u8 *common_ie, int common_ie_len,
  192. int left)
  193. {
  194. int len = 0;
  195. u8 *pos = NULL;
  196. /* Make sure there is enough space for the probe request,
  197. * two mandatory IEs and the data */
  198. left -= 24;
  199. if (left < 0)
  200. return 0;
  201. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  202. eth_broadcast_addr(frame->da);
  203. memcpy(frame->sa, ta, ETH_ALEN);
  204. eth_broadcast_addr(frame->bssid);
  205. frame->seq_ctrl = 0;
  206. len += 24;
  207. /* for passive scans, no need to fill anything */
  208. if (n_ssids == 0)
  209. return (u16)len;
  210. /* points to the payload of the request */
  211. pos = &frame->u.probe_req.variable[0];
  212. /* fill in our SSID IE */
  213. left -= ssid_len + 2;
  214. if (left < 0)
  215. return 0;
  216. *pos++ = WLAN_EID_SSID;
  217. *pos++ = ssid_len;
  218. if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
  219. memcpy(pos, ssid, ssid_len);
  220. pos += ssid_len;
  221. }
  222. len += ssid_len + 2;
  223. if (WARN_ON(left < band_ie_len + common_ie_len))
  224. return len;
  225. if (band_ie && band_ie_len) {
  226. memcpy(pos, band_ie, band_ie_len);
  227. pos += band_ie_len;
  228. len += band_ie_len;
  229. }
  230. if (common_ie && common_ie_len) {
  231. memcpy(pos, common_ie, common_ie_len);
  232. pos += common_ie_len;
  233. len += common_ie_len;
  234. }
  235. return (u16)len;
  236. }
  237. static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
  238. struct ieee80211_vif *vif)
  239. {
  240. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  241. bool *global_bound = data;
  242. if (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < MAX_PHYS)
  243. *global_bound = true;
  244. }
  245. static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
  246. struct ieee80211_vif *vif,
  247. int n_ssids, u32 flags,
  248. struct iwl_mvm_scan_params *params)
  249. {
  250. bool global_bound = false;
  251. enum ieee80211_band band;
  252. u8 frag_passive_dwell = 0;
  253. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  254. IEEE80211_IFACE_ITER_NORMAL,
  255. iwl_mvm_scan_condition_iterator,
  256. &global_bound);
  257. if (!global_bound)
  258. goto not_bound;
  259. params->suspend_time = 30;
  260. params->max_out_time = 170;
  261. if (iwl_mvm_low_latency(mvm)) {
  262. if (mvm->fw->ucode_capa.api[0] &
  263. IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
  264. params->suspend_time = 105;
  265. params->max_out_time = 70;
  266. frag_passive_dwell = 20;
  267. } else {
  268. params->suspend_time = 120;
  269. params->max_out_time = 120;
  270. }
  271. }
  272. if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
  273. IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
  274. /*
  275. * P2P device scan should not be fragmented to avoid negative
  276. * impact on P2P device discovery. Configure max_out_time to be
  277. * equal to dwell time on passive channel. Take a longest
  278. * possible value, one that corresponds to 2GHz band
  279. */
  280. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  281. u32 passive_dwell =
  282. iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
  283. params->max_out_time = passive_dwell;
  284. } else {
  285. params->passive_fragmented = true;
  286. }
  287. }
  288. if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
  289. params->max_out_time = 200;
  290. not_bound:
  291. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  292. if (params->passive_fragmented)
  293. params->dwell[band].passive = frag_passive_dwell;
  294. else
  295. params->dwell[band].passive =
  296. iwl_mvm_get_passive_dwell(band);
  297. params->dwell[band].active = iwl_mvm_get_active_dwell(band,
  298. n_ssids);
  299. }
  300. }
  301. int iwl_mvm_scan_request(struct iwl_mvm *mvm,
  302. struct ieee80211_vif *vif,
  303. struct cfg80211_scan_request *req)
  304. {
  305. struct iwl_host_cmd hcmd = {
  306. .id = SCAN_REQUEST_CMD,
  307. .len = { 0, },
  308. .data = { mvm->scan_cmd, },
  309. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  310. };
  311. struct iwl_scan_cmd *cmd = mvm->scan_cmd;
  312. int ret;
  313. u32 status;
  314. int ssid_len = 0;
  315. u8 *ssid = NULL;
  316. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  317. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  318. struct iwl_mvm_scan_params params = {};
  319. lockdep_assert_held(&mvm->mutex);
  320. /* we should have failed registration if scan_cmd was NULL */
  321. if (WARN_ON(mvm->scan_cmd == NULL))
  322. return -ENOMEM;
  323. IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
  324. mvm->scan_status = IWL_MVM_SCAN_OS;
  325. memset(cmd, 0, ksize(cmd));
  326. cmd->channel_count = (u8)req->n_channels;
  327. cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  328. cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  329. cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
  330. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
  331. cmd->max_out_time = cpu_to_le32(params.max_out_time);
  332. cmd->suspend_time = cpu_to_le32(params.suspend_time);
  333. if (params.passive_fragmented)
  334. cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  335. cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  336. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  337. MAC_FILTER_IN_BEACON);
  338. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  339. cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
  340. else
  341. cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
  342. cmd->repeats = cpu_to_le32(1);
  343. /*
  344. * If the user asked for passive scan, don't change to active scan if
  345. * you see any activity on the channel - remain passive.
  346. */
  347. if (req->n_ssids > 0) {
  348. cmd->passive2active = cpu_to_le16(1);
  349. cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
  350. if (basic_ssid) {
  351. ssid = req->ssids[0].ssid;
  352. ssid_len = req->ssids[0].ssid_len;
  353. }
  354. } else {
  355. cmd->passive2active = 0;
  356. cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
  357. }
  358. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
  359. basic_ssid ? 1 : 0);
  360. cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  361. TX_CMD_FLG_BT_DIS);
  362. cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
  363. cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  364. cmd->tx_cmd.rate_n_flags =
  365. iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
  366. req->no_cck);
  367. cmd->tx_cmd.len =
  368. cpu_to_le16(iwl_mvm_fill_probe_req(
  369. (struct ieee80211_mgmt *)cmd->data,
  370. vif->addr,
  371. req->n_ssids, ssid, ssid_len,
  372. req->ie, req->ie_len, NULL, 0,
  373. mvm->fw->ucode_capa.max_probe_length));
  374. iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
  375. cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
  376. le16_to_cpu(cmd->tx_cmd.len) +
  377. (cmd->channel_count * sizeof(struct iwl_scan_channel)));
  378. hcmd.len[0] = le16_to_cpu(cmd->len);
  379. status = SCAN_RESPONSE_OK;
  380. ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
  381. if (!ret && status == SCAN_RESPONSE_OK) {
  382. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  383. } else {
  384. /*
  385. * If the scan failed, it usually means that the FW was unable
  386. * to allocate the time events. Warn on it, but maybe we
  387. * should try to send the command again with different params.
  388. */
  389. IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
  390. status, ret);
  391. mvm->scan_status = IWL_MVM_SCAN_NONE;
  392. ret = -EIO;
  393. }
  394. return ret;
  395. }
  396. int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  397. struct iwl_device_cmd *cmd)
  398. {
  399. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  400. struct iwl_cmd_response *resp = (void *)pkt->data;
  401. IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
  402. le32_to_cpu(resp->status));
  403. return 0;
  404. }
  405. int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
  406. struct iwl_device_cmd *cmd)
  407. {
  408. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  409. struct iwl_scan_complete_notif *notif = (void *)pkt->data;
  410. lockdep_assert_held(&mvm->mutex);
  411. IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
  412. notif->status, notif->scanned_channels);
  413. if (mvm->scan_status == IWL_MVM_SCAN_OS)
  414. mvm->scan_status = IWL_MVM_SCAN_NONE;
  415. ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
  416. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  417. return 0;
  418. }
  419. int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
  420. struct iwl_rx_cmd_buffer *rxb,
  421. struct iwl_device_cmd *cmd)
  422. {
  423. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  424. u8 client_bitmap = 0;
  425. if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
  426. struct iwl_sched_scan_results *notif = (void *)pkt->data;
  427. client_bitmap = notif->client_bitmap;
  428. }
  429. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN ||
  430. client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
  431. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  432. IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
  433. ieee80211_sched_scan_results(mvm->hw);
  434. } else {
  435. IWL_DEBUG_SCAN(mvm, "Scan results\n");
  436. }
  437. }
  438. return 0;
  439. }
  440. static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
  441. struct iwl_rx_packet *pkt, void *data)
  442. {
  443. struct iwl_mvm *mvm =
  444. container_of(notif_wait, struct iwl_mvm, notif_wait);
  445. struct iwl_scan_complete_notif *notif;
  446. u32 *resp;
  447. switch (pkt->hdr.cmd) {
  448. case SCAN_ABORT_CMD:
  449. resp = (void *)pkt->data;
  450. if (*resp == CAN_ABORT_STATUS) {
  451. IWL_DEBUG_SCAN(mvm,
  452. "Scan can be aborted, wait until completion\n");
  453. return false;
  454. }
  455. /*
  456. * If scan cannot be aborted, it means that we had a
  457. * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
  458. * ieee80211_scan_completed already.
  459. */
  460. IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
  461. *resp);
  462. return true;
  463. case SCAN_COMPLETE_NOTIFICATION:
  464. notif = (void *)pkt->data;
  465. IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
  466. notif->status);
  467. return true;
  468. default:
  469. WARN_ON(1);
  470. return false;
  471. };
  472. }
  473. static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
  474. {
  475. struct iwl_notification_wait wait_scan_abort;
  476. static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
  477. SCAN_COMPLETE_NOTIFICATION };
  478. int ret;
  479. if (mvm->scan_status == IWL_MVM_SCAN_NONE)
  480. return 0;
  481. if (iwl_mvm_is_radio_killed(mvm)) {
  482. ieee80211_scan_completed(mvm->hw, true);
  483. iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
  484. mvm->scan_status = IWL_MVM_SCAN_NONE;
  485. return 0;
  486. }
  487. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
  488. scan_abort_notif,
  489. ARRAY_SIZE(scan_abort_notif),
  490. iwl_mvm_scan_abort_notif, NULL);
  491. ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
  492. if (ret) {
  493. IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
  494. /* mac80211's state will be cleaned in the nic_restart flow */
  495. goto out_remove_notif;
  496. }
  497. return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
  498. out_remove_notif:
  499. iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
  500. return ret;
  501. }
  502. int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
  503. struct iwl_rx_cmd_buffer *rxb,
  504. struct iwl_device_cmd *cmd)
  505. {
  506. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  507. u8 status, ebs_status;
  508. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
  509. struct iwl_periodic_scan_complete *scan_notif;
  510. scan_notif = (void *)pkt->data;
  511. status = scan_notif->status;
  512. ebs_status = scan_notif->ebs_status;
  513. } else {
  514. struct iwl_scan_offload_complete *scan_notif;
  515. scan_notif = (void *)pkt->data;
  516. status = scan_notif->status;
  517. ebs_status = scan_notif->ebs_status;
  518. }
  519. /* scan status must be locked for proper checking */
  520. lockdep_assert_held(&mvm->mutex);
  521. IWL_DEBUG_SCAN(mvm,
  522. "%s completed, status %s, EBS status %s\n",
  523. mvm->scan_status == IWL_MVM_SCAN_SCHED ?
  524. "Scheduled scan" : "Scan",
  525. status == IWL_SCAN_OFFLOAD_COMPLETED ?
  526. "completed" : "aborted",
  527. ebs_status == IWL_SCAN_EBS_SUCCESS ?
  528. "success" : "failed");
  529. /* only call mac80211 completion if the stop was initiated by FW */
  530. if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
  531. mvm->scan_status = IWL_MVM_SCAN_NONE;
  532. ieee80211_sched_scan_stopped(mvm->hw);
  533. } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
  534. mvm->scan_status = IWL_MVM_SCAN_NONE;
  535. ieee80211_scan_completed(mvm->hw,
  536. status == IWL_SCAN_OFFLOAD_ABORTED);
  537. }
  538. mvm->last_ebs_successful = !ebs_status;
  539. return 0;
  540. }
  541. static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
  542. struct ieee80211_vif *vif,
  543. struct ieee80211_scan_ies *ies,
  544. enum ieee80211_band band,
  545. struct iwl_tx_cmd *cmd,
  546. u8 *data)
  547. {
  548. u16 cmd_len;
  549. cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
  550. cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  551. cmd->sta_id = mvm->aux_sta.sta_id;
  552. cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
  553. cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
  554. vif->addr,
  555. 1, NULL, 0,
  556. ies->ies[band], ies->len[band],
  557. ies->common_ies, ies->common_ie_len,
  558. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  559. cmd->len = cpu_to_le16(cmd_len);
  560. }
  561. static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
  562. struct ieee80211_vif *vif,
  563. struct cfg80211_sched_scan_request *req,
  564. struct iwl_scan_offload_cmd *scan,
  565. struct iwl_mvm_scan_params *params)
  566. {
  567. scan->channel_count = req->n_channels;
  568. scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
  569. scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
  570. scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
  571. scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
  572. scan->max_out_time = cpu_to_le32(params->max_out_time);
  573. scan->suspend_time = cpu_to_le32(params->suspend_time);
  574. scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  575. MAC_FILTER_IN_BEACON);
  576. scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
  577. scan->rep_count = cpu_to_le32(1);
  578. if (params->passive_fragmented)
  579. scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
  580. }
  581. static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
  582. {
  583. int i;
  584. for (i = 0; i < PROBE_OPTION_MAX; i++) {
  585. if (!ssid_list[i].len)
  586. break;
  587. if (ssid_list[i].len == ssid_len &&
  588. !memcmp(ssid_list->ssid, ssid, ssid_len))
  589. return i;
  590. }
  591. return -1;
  592. }
  593. static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
  594. struct iwl_ssid_ie *direct_scan,
  595. u32 *ssid_bitmap, bool basic_ssid)
  596. {
  597. int i, j;
  598. int index;
  599. /*
  600. * copy SSIDs from match list.
  601. * iwl_config_sched_scan_profiles() uses the order of these ssids to
  602. * config match list.
  603. */
  604. for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
  605. /* skip empty SSID matchsets */
  606. if (!req->match_sets[i].ssid.ssid_len)
  607. continue;
  608. direct_scan[i].id = WLAN_EID_SSID;
  609. direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
  610. memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
  611. direct_scan[i].len);
  612. }
  613. /* add SSIDs from scan SSID list */
  614. *ssid_bitmap = 0;
  615. for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
  616. index = iwl_ssid_exist(req->ssids[j].ssid,
  617. req->ssids[j].ssid_len,
  618. direct_scan);
  619. if (index < 0) {
  620. if (!req->ssids[j].ssid_len && basic_ssid)
  621. continue;
  622. direct_scan[i].id = WLAN_EID_SSID;
  623. direct_scan[i].len = req->ssids[j].ssid_len;
  624. memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
  625. direct_scan[i].len);
  626. *ssid_bitmap |= BIT(i + 1);
  627. i++;
  628. } else {
  629. *ssid_bitmap |= BIT(index + 1);
  630. }
  631. }
  632. }
  633. static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
  634. struct cfg80211_sched_scan_request *req,
  635. u8 *channels_buffer,
  636. enum ieee80211_band band,
  637. int *head,
  638. u32 ssid_bitmap,
  639. struct iwl_mvm_scan_params *params)
  640. {
  641. u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
  642. __le32 *type = (__le32 *)channels_buffer;
  643. __le16 *channel_number = (__le16 *)(type + n_channels);
  644. __le16 *iter_count = channel_number + n_channels;
  645. __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
  646. u8 *active_dwell = (u8 *)(iter_interval + n_channels);
  647. u8 *passive_dwell = active_dwell + n_channels;
  648. int i, index = 0;
  649. for (i = 0; i < req->n_channels; i++) {
  650. struct ieee80211_channel *chan = req->channels[i];
  651. if (chan->band != band)
  652. continue;
  653. index = *head;
  654. (*head)++;
  655. channel_number[index] = cpu_to_le16(chan->hw_value);
  656. active_dwell[index] = params->dwell[band].active;
  657. passive_dwell[index] = params->dwell[band].passive;
  658. iter_count[index] = cpu_to_le16(1);
  659. iter_interval[index] = 0;
  660. if (!(chan->flags & IEEE80211_CHAN_NO_IR))
  661. type[index] |=
  662. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
  663. type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
  664. IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
  665. if (chan->flags & IEEE80211_CHAN_NO_HT40)
  666. type[index] |=
  667. cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
  668. /* scan for all SSIDs from req->ssids */
  669. type[index] |= cpu_to_le32(ssid_bitmap);
  670. }
  671. }
  672. int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
  673. struct ieee80211_vif *vif,
  674. struct cfg80211_sched_scan_request *req,
  675. struct ieee80211_scan_ies *ies)
  676. {
  677. int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
  678. int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
  679. int head = 0;
  680. u32 ssid_bitmap;
  681. int cmd_len;
  682. int ret;
  683. u8 *probes;
  684. bool basic_ssid = !(mvm->fw->ucode_capa.flags &
  685. IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
  686. struct iwl_scan_offload_cfg *scan_cfg;
  687. struct iwl_host_cmd cmd = {
  688. .id = SCAN_OFFLOAD_CONFIG_CMD,
  689. };
  690. struct iwl_mvm_scan_params params = {};
  691. lockdep_assert_held(&mvm->mutex);
  692. cmd_len = sizeof(struct iwl_scan_offload_cfg) +
  693. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
  694. 2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
  695. scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
  696. if (!scan_cfg)
  697. return -ENOMEM;
  698. probes = scan_cfg->data +
  699. mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
  700. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  701. iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
  702. scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
  703. iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
  704. &ssid_bitmap, basic_ssid);
  705. /* build tx frames for supported bands */
  706. if (band_2ghz) {
  707. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  708. IEEE80211_BAND_2GHZ,
  709. &scan_cfg->scan_cmd.tx_cmd[0],
  710. probes);
  711. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  712. IEEE80211_BAND_2GHZ, &head,
  713. ssid_bitmap, &params);
  714. }
  715. if (band_5ghz) {
  716. iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
  717. IEEE80211_BAND_5GHZ,
  718. &scan_cfg->scan_cmd.tx_cmd[1],
  719. probes +
  720. SCAN_OFFLOAD_PROBE_REQ_SIZE);
  721. iwl_build_channel_cfg(mvm, req, scan_cfg->data,
  722. IEEE80211_BAND_5GHZ, &head,
  723. ssid_bitmap, &params);
  724. }
  725. cmd.data[0] = scan_cfg;
  726. cmd.len[0] = cmd_len;
  727. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  728. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
  729. ret = iwl_mvm_send_cmd(mvm, &cmd);
  730. kfree(scan_cfg);
  731. return ret;
  732. }
  733. int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
  734. struct cfg80211_sched_scan_request *req)
  735. {
  736. struct iwl_scan_offload_profile *profile;
  737. struct iwl_scan_offload_profile_cfg *profile_cfg;
  738. struct iwl_scan_offload_blacklist *blacklist;
  739. struct iwl_host_cmd cmd = {
  740. .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
  741. .len[1] = sizeof(*profile_cfg),
  742. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  743. .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
  744. };
  745. int blacklist_len;
  746. int i;
  747. int ret;
  748. if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
  749. return -EIO;
  750. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
  751. blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
  752. else
  753. blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
  754. blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
  755. if (!blacklist)
  756. return -ENOMEM;
  757. profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
  758. if (!profile_cfg) {
  759. ret = -ENOMEM;
  760. goto free_blacklist;
  761. }
  762. cmd.data[0] = blacklist;
  763. cmd.len[0] = sizeof(*blacklist) * blacklist_len;
  764. cmd.data[1] = profile_cfg;
  765. /* No blacklist configuration */
  766. profile_cfg->num_profiles = req->n_match_sets;
  767. profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
  768. profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
  769. profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
  770. if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
  771. profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
  772. for (i = 0; i < req->n_match_sets; i++) {
  773. profile = &profile_cfg->profiles[i];
  774. profile->ssid_index = i;
  775. /* Support any cipher and auth algorithm */
  776. profile->unicast_cipher = 0xff;
  777. profile->auth_alg = 0xff;
  778. profile->network_type = IWL_NETWORK_TYPE_ANY;
  779. profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
  780. profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
  781. }
  782. IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
  783. ret = iwl_mvm_send_cmd(mvm, &cmd);
  784. kfree(profile_cfg);
  785. free_blacklist:
  786. kfree(blacklist);
  787. return ret;
  788. }
  789. int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
  790. struct cfg80211_sched_scan_request *req)
  791. {
  792. struct iwl_scan_offload_req scan_req = {
  793. .watchdog = IWL_SCHED_SCAN_WATCHDOG,
  794. .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
  795. .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
  796. .schedule_line[0].full_scan_mul = 1,
  797. .schedule_line[1].iterations = 0xff,
  798. .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
  799. .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
  800. };
  801. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  802. IWL_DEBUG_SCAN(mvm,
  803. "Sending scheduled scan with filtering, filter len %d\n",
  804. req->n_match_sets);
  805. } else {
  806. IWL_DEBUG_SCAN(mvm,
  807. "Sending Scheduled scan without filtering\n");
  808. scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
  809. }
  810. if (mvm->last_ebs_successful &&
  811. mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
  812. scan_req.flags |=
  813. cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
  814. return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
  815. sizeof(scan_req), &scan_req);
  816. }
  817. static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
  818. {
  819. int ret;
  820. struct iwl_host_cmd cmd = {
  821. .id = SCAN_OFFLOAD_ABORT_CMD,
  822. };
  823. u32 status;
  824. /* Exit instantly with error when device is not ready
  825. * to receive scan abort command or it does not perform
  826. * scheduled scan currently */
  827. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  828. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  829. mvm->scan_status != IWL_MVM_SCAN_OS))
  830. return -EIO;
  831. ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
  832. if (ret)
  833. return ret;
  834. if (status != CAN_ABORT_STATUS) {
  835. /*
  836. * The scan abort will return 1 for success or
  837. * 2 for "failure". A failure condition can be
  838. * due to simply not being in an active scan which
  839. * can occur if we send the scan abort before the
  840. * microcode has notified us that a scan is completed.
  841. */
  842. IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
  843. ret = -ENOENT;
  844. }
  845. return ret;
  846. }
  847. int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
  848. {
  849. int ret;
  850. struct iwl_notification_wait wait_scan_done;
  851. static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
  852. bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
  853. lockdep_assert_held(&mvm->mutex);
  854. if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
  855. (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
  856. mvm->scan_status != IWL_MVM_SCAN_OS)) {
  857. IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
  858. return 0;
  859. }
  860. iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
  861. scan_done_notif,
  862. ARRAY_SIZE(scan_done_notif),
  863. NULL, NULL);
  864. ret = iwl_mvm_send_scan_offload_abort(mvm);
  865. if (ret) {
  866. IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
  867. sched ? "offloaded " : "", ret);
  868. iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
  869. return ret;
  870. }
  871. IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
  872. sched ? "offloaded " : "");
  873. ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
  874. if (ret)
  875. return ret;
  876. /*
  877. * Clear the scan status so the next scan requests will succeed. This
  878. * also ensures the Rx handler doesn't do anything, as the scan was
  879. * stopped from above.
  880. */
  881. mvm->scan_status = IWL_MVM_SCAN_NONE;
  882. if (notify) {
  883. if (sched)
  884. ieee80211_sched_scan_stopped(mvm->hw);
  885. else
  886. ieee80211_scan_completed(mvm->hw, true);
  887. }
  888. return 0;
  889. }
  890. static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
  891. struct iwl_scan_req_tx_cmd *tx_cmd,
  892. bool no_cck)
  893. {
  894. tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  895. TX_CMD_FLG_BT_DIS);
  896. tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  897. IEEE80211_BAND_2GHZ,
  898. no_cck);
  899. tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
  900. tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  901. TX_CMD_FLG_BT_DIS);
  902. tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
  903. IEEE80211_BAND_5GHZ,
  904. no_cck);
  905. tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
  906. }
  907. static void
  908. iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
  909. struct ieee80211_channel **channels,
  910. int n_channels, u32 ssid_bitmap,
  911. struct iwl_scan_req_unified_lmac *cmd)
  912. {
  913. struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
  914. int i;
  915. for (i = 0; i < n_channels; i++) {
  916. channel_cfg[i].channel_num =
  917. cpu_to_le16(channels[i]->hw_value);
  918. channel_cfg[i].iter_count = cpu_to_le16(1);
  919. channel_cfg[i].iter_interval = 0;
  920. channel_cfg[i].flags =
  921. cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
  922. ssid_bitmap);
  923. }
  924. }
  925. static void
  926. iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  927. struct ieee80211_scan_ies *ies,
  928. struct iwl_scan_req_unified_lmac *cmd)
  929. {
  930. struct iwl_scan_probe_req *preq = (void *)(cmd->data +
  931. sizeof(struct iwl_scan_channel_cfg_lmac) *
  932. mvm->fw->ucode_capa.n_scan_channels);
  933. struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
  934. u8 *pos;
  935. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  936. eth_broadcast_addr(frame->da);
  937. memcpy(frame->sa, vif->addr, ETH_ALEN);
  938. eth_broadcast_addr(frame->bssid);
  939. frame->seq_ctrl = 0;
  940. pos = frame->u.probe_req.variable;
  941. *pos++ = WLAN_EID_SSID;
  942. *pos++ = 0;
  943. preq->mac_header.offset = 0;
  944. preq->mac_header.len = cpu_to_le16(24 + 2);
  945. memcpy(pos, ies->ies[IEEE80211_BAND_2GHZ],
  946. ies->len[IEEE80211_BAND_2GHZ]);
  947. preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
  948. preq->band_data[0].len = cpu_to_le16(ies->len[IEEE80211_BAND_2GHZ]);
  949. pos += ies->len[IEEE80211_BAND_2GHZ];
  950. memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
  951. ies->len[IEEE80211_BAND_5GHZ]);
  952. preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
  953. preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
  954. pos += ies->len[IEEE80211_BAND_5GHZ];
  955. memcpy(pos, ies->common_ies, ies->common_ie_len);
  956. preq->common_data.offset = cpu_to_le16(pos - preq->buf);
  957. preq->common_data.len = cpu_to_le16(ies->common_ie_len);
  958. }
  959. static void
  960. iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
  961. struct iwl_scan_req_unified_lmac *cmd,
  962. struct iwl_mvm_scan_params *params)
  963. {
  964. memset(cmd, 0, ksize(cmd));
  965. cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
  966. cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
  967. if (params->passive_fragmented)
  968. cmd->fragmented_dwell =
  969. params->dwell[IEEE80211_BAND_2GHZ].passive;
  970. cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
  971. cmd->max_out_time = cpu_to_le32(params->max_out_time);
  972. cmd->suspend_time = cpu_to_le32(params->suspend_time);
  973. cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
  974. cmd->iter_num = cpu_to_le32(1);
  975. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
  976. mvm->last_ebs_successful) {
  977. cmd->channel_opt[0].flags =
  978. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  979. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  980. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  981. cmd->channel_opt[1].flags =
  982. cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
  983. IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
  984. IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
  985. }
  986. }
  987. int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
  988. struct ieee80211_vif *vif,
  989. struct ieee80211_scan_request *req)
  990. {
  991. struct iwl_host_cmd hcmd = {
  992. .id = SCAN_OFFLOAD_REQUEST_CMD,
  993. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  994. sizeof(struct iwl_scan_channel_cfg_lmac) *
  995. mvm->fw->ucode_capa.n_scan_channels +
  996. sizeof(struct iwl_scan_probe_req), },
  997. .data = { mvm->scan_cmd, },
  998. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  999. };
  1000. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1001. struct iwl_mvm_scan_params params = {};
  1002. u32 flags;
  1003. int ssid_bitmap = 0;
  1004. int ret, i;
  1005. lockdep_assert_held(&mvm->mutex);
  1006. /* we should have failed registration if scan_cmd was NULL */
  1007. if (WARN_ON(mvm->scan_cmd == NULL))
  1008. return -ENOMEM;
  1009. if (WARN_ON_ONCE(req->req.n_ssids > PROBE_OPTION_MAX ||
  1010. req->ies.common_ie_len + req->ies.len[0] +
  1011. req->ies.len[1] + 24 + 2 >
  1012. SCAN_OFFLOAD_PROBE_REQ_SIZE ||
  1013. req->req.n_channels >
  1014. mvm->fw->ucode_capa.n_scan_channels))
  1015. return -1;
  1016. mvm->scan_status = IWL_MVM_SCAN_OS;
  1017. iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
  1018. &params);
  1019. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1020. cmd->n_channels = (u8)req->req.n_channels;
  1021. flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1022. if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
  1023. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1024. if (params.passive_fragmented)
  1025. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1026. if (req->req.n_ssids == 0)
  1027. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1028. cmd->scan_flags = cpu_to_le32(flags);
  1029. cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
  1030. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1031. MAC_FILTER_IN_BEACON);
  1032. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
  1033. iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
  1034. req->req.n_ssids, 0);
  1035. cmd->schedule[0].delay = 0;
  1036. cmd->schedule[0].iterations = 1;
  1037. cmd->schedule[0].full_scan_mul = 0;
  1038. cmd->schedule[1].delay = 0;
  1039. cmd->schedule[1].iterations = 0;
  1040. cmd->schedule[1].full_scan_mul = 0;
  1041. for (i = 1; i <= req->req.n_ssids; i++)
  1042. ssid_bitmap |= BIT(i);
  1043. iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
  1044. req->req.n_channels, ssid_bitmap,
  1045. cmd);
  1046. iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, cmd);
  1047. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1048. if (!ret) {
  1049. IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
  1050. } else {
  1051. /*
  1052. * If the scan failed, it usually means that the FW was unable
  1053. * to allocate the time events. Warn on it, but maybe we
  1054. * should try to send the command again with different params.
  1055. */
  1056. IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
  1057. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1058. ret = -EIO;
  1059. }
  1060. return ret;
  1061. }
  1062. int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
  1063. struct ieee80211_vif *vif,
  1064. struct cfg80211_sched_scan_request *req,
  1065. struct ieee80211_scan_ies *ies)
  1066. {
  1067. struct iwl_host_cmd hcmd = {
  1068. .id = SCAN_OFFLOAD_REQUEST_CMD,
  1069. .len = { sizeof(struct iwl_scan_req_unified_lmac) +
  1070. sizeof(struct iwl_scan_channel_cfg_lmac) *
  1071. mvm->fw->ucode_capa.n_scan_channels +
  1072. sizeof(struct iwl_scan_probe_req), },
  1073. .data = { mvm->scan_cmd, },
  1074. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  1075. };
  1076. struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
  1077. struct iwl_mvm_scan_params params = {};
  1078. int ret;
  1079. u32 flags = 0, ssid_bitmap = 0;
  1080. lockdep_assert_held(&mvm->mutex);
  1081. /* we should have failed registration if scan_cmd was NULL */
  1082. if (WARN_ON(mvm->scan_cmd == NULL))
  1083. return -ENOMEM;
  1084. if (WARN_ON_ONCE(req->n_ssids > PROBE_OPTION_MAX ||
  1085. ies->common_ie_len + ies->len[0] + ies->len[1] + 24 + 2
  1086. > SCAN_OFFLOAD_PROBE_REQ_SIZE ||
  1087. req->n_channels > mvm->fw->ucode_capa.n_scan_channels))
  1088. return -ENOBUFS;
  1089. iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
  1090. iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
  1091. cmd->n_channels = (u8)req->n_channels;
  1092. if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
  1093. IWL_DEBUG_SCAN(mvm,
  1094. "Sending scheduled scan with filtering, n_match_sets %d\n",
  1095. req->n_match_sets);
  1096. } else {
  1097. IWL_DEBUG_SCAN(mvm,
  1098. "Sending Scheduled scan without filtering\n");
  1099. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
  1100. }
  1101. if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
  1102. flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
  1103. if (params.passive_fragmented)
  1104. flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
  1105. if (req->n_ssids == 0)
  1106. flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
  1107. cmd->scan_flags = cpu_to_le32(flags);
  1108. cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
  1109. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
  1110. MAC_FILTER_IN_BEACON);
  1111. iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
  1112. iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
  1113. cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1114. cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
  1115. cmd->schedule[0].full_scan_mul = 1;
  1116. cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
  1117. cmd->schedule[1].iterations = 0xff;
  1118. cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
  1119. iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
  1120. ssid_bitmap, cmd);
  1121. iwl_mvm_build_unified_scan_probe(mvm, vif, ies, cmd);
  1122. ret = iwl_mvm_send_cmd(mvm, &hcmd);
  1123. if (!ret) {
  1124. IWL_DEBUG_SCAN(mvm,
  1125. "Sched scan request was sent successfully\n");
  1126. } else {
  1127. /*
  1128. * If the scan failed, it usually means that the FW was unable
  1129. * to allocate the time events. Warn on it, but maybe we
  1130. * should try to send the command again with different params.
  1131. */
  1132. IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
  1133. mvm->scan_status = IWL_MVM_SCAN_NONE;
  1134. ret = -EIO;
  1135. }
  1136. return ret;
  1137. }
  1138. int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
  1139. {
  1140. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
  1141. return iwl_mvm_scan_offload_stop(mvm, true);
  1142. return iwl_mvm_cancel_regular_scan(mvm);
  1143. }