mac-ctxt.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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 "iwl-io.h"
  68. #include "iwl-prph.h"
  69. #include "fw-api.h"
  70. #include "mvm.h"
  71. #include "time-event.h"
  72. const u8 iwl_mvm_ac_to_tx_fifo[] = {
  73. IWL_MVM_TX_FIFO_VO,
  74. IWL_MVM_TX_FIFO_VI,
  75. IWL_MVM_TX_FIFO_BE,
  76. IWL_MVM_TX_FIFO_BK,
  77. };
  78. struct iwl_mvm_mac_iface_iterator_data {
  79. struct iwl_mvm *mvm;
  80. struct ieee80211_vif *vif;
  81. unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
  82. unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
  83. u32 used_hw_queues;
  84. enum iwl_tsf_id preferred_tsf;
  85. bool found_vif;
  86. };
  87. static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
  88. struct ieee80211_vif *vif)
  89. {
  90. struct iwl_mvm_mac_iface_iterator_data *data = _data;
  91. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  92. u16 min_bi;
  93. /* Skip the interface for which we are trying to assign a tsf_id */
  94. if (vif == data->vif)
  95. return;
  96. /*
  97. * The TSF is a hardware/firmware resource, there are 4 and
  98. * the driver should assign and free them as needed. However,
  99. * there are cases where 2 MACs should share the same TSF ID
  100. * for the purpose of clock sync, an optimization to avoid
  101. * clock drift causing overlapping TBTTs/DTIMs for a GO and
  102. * client in the system.
  103. *
  104. * The firmware will decide according to the MAC type which
  105. * will be the master and slave. Clients that need to sync
  106. * with a remote station will be the master, and an AP or GO
  107. * will be the slave.
  108. *
  109. * Depending on the new interface type it can be slaved to
  110. * or become the master of an existing interface.
  111. */
  112. switch (data->vif->type) {
  113. case NL80211_IFTYPE_STATION:
  114. /*
  115. * The new interface is a client, so if the one we're iterating
  116. * is an AP, and the beacon interval of the AP is a multiple or
  117. * divisor of the beacon interval of the client, the same TSF
  118. * should be used to avoid drift between the new client and
  119. * existing AP. The existing AP will get drift updates from the
  120. * new client context in this case.
  121. */
  122. if (vif->type != NL80211_IFTYPE_AP ||
  123. data->preferred_tsf != NUM_TSF_IDS ||
  124. !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
  125. break;
  126. min_bi = min(data->vif->bss_conf.beacon_int,
  127. vif->bss_conf.beacon_int);
  128. if (!min_bi)
  129. break;
  130. if ((data->vif->bss_conf.beacon_int -
  131. vif->bss_conf.beacon_int) % min_bi == 0) {
  132. data->preferred_tsf = mvmvif->tsf_id;
  133. return;
  134. }
  135. break;
  136. case NL80211_IFTYPE_AP:
  137. /*
  138. * The new interface is AP/GO, so if its beacon interval is a
  139. * multiple or a divisor of the beacon interval of an existing
  140. * interface, it should get drift updates from an existing
  141. * client or use the same TSF as an existing GO. There's no
  142. * drift between TSFs internally but if they used different
  143. * TSFs then a new client MAC could update one of them and
  144. * cause drift that way.
  145. */
  146. if ((vif->type != NL80211_IFTYPE_AP &&
  147. vif->type != NL80211_IFTYPE_STATION) ||
  148. data->preferred_tsf != NUM_TSF_IDS ||
  149. !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
  150. break;
  151. min_bi = min(data->vif->bss_conf.beacon_int,
  152. vif->bss_conf.beacon_int);
  153. if (!min_bi)
  154. break;
  155. if ((data->vif->bss_conf.beacon_int -
  156. vif->bss_conf.beacon_int) % min_bi == 0) {
  157. data->preferred_tsf = mvmvif->tsf_id;
  158. return;
  159. }
  160. break;
  161. default:
  162. /*
  163. * For all other interface types there's no need to
  164. * take drift into account. Either they're exclusive
  165. * like IBSS and monitor, or we don't care much about
  166. * their TSF (like P2P Device), but we won't be able
  167. * to share the TSF resource.
  168. */
  169. break;
  170. }
  171. /*
  172. * Unless we exited above, we can't share the TSF resource
  173. * that the virtual interface we're iterating over is using
  174. * with the new one, so clear the available bit and if this
  175. * was the preferred one, reset that as well.
  176. */
  177. __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
  178. if (data->preferred_tsf == mvmvif->tsf_id)
  179. data->preferred_tsf = NUM_TSF_IDS;
  180. }
  181. /*
  182. * Get the mask of the queues used by the vif
  183. */
  184. u32 iwl_mvm_mac_get_queues_mask(struct iwl_mvm *mvm,
  185. struct ieee80211_vif *vif)
  186. {
  187. u32 qmask = 0, ac;
  188. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  189. return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
  190. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  191. qmask |= BIT(vif->hw_queue[ac]);
  192. if (vif->type == NL80211_IFTYPE_AP)
  193. qmask |= BIT(vif->cab_queue);
  194. return qmask;
  195. }
  196. static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
  197. struct ieee80211_vif *vif)
  198. {
  199. struct iwl_mvm_mac_iface_iterator_data *data = _data;
  200. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  201. /* Iterator may already find the interface being added -- skip it */
  202. if (vif == data->vif) {
  203. data->found_vif = true;
  204. return;
  205. }
  206. /* Mark the queues used by the vif */
  207. data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(data->mvm, vif);
  208. /* Mark MAC IDs as used by clearing the available bit, and
  209. * (below) mark TSFs as used if their existing use is not
  210. * compatible with the new interface type.
  211. * No locking or atomic bit operations are needed since the
  212. * data is on the stack of the caller function.
  213. */
  214. __clear_bit(mvmvif->id, data->available_mac_ids);
  215. /* find a suitable tsf_id */
  216. iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
  217. }
  218. void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
  219. struct ieee80211_vif *vif)
  220. {
  221. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  222. struct iwl_mvm_mac_iface_iterator_data data = {
  223. .mvm = mvm,
  224. .vif = vif,
  225. .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
  226. /* no preference yet */
  227. .preferred_tsf = NUM_TSF_IDS,
  228. };
  229. ieee80211_iterate_active_interfaces_atomic(
  230. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  231. iwl_mvm_mac_tsf_id_iter, &data);
  232. if (data.preferred_tsf != NUM_TSF_IDS)
  233. mvmvif->tsf_id = data.preferred_tsf;
  234. else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
  235. mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
  236. NUM_TSF_IDS);
  237. }
  238. static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
  239. struct ieee80211_vif *vif)
  240. {
  241. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  242. struct iwl_mvm_mac_iface_iterator_data data = {
  243. .mvm = mvm,
  244. .vif = vif,
  245. .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
  246. .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
  247. /* no preference yet */
  248. .preferred_tsf = NUM_TSF_IDS,
  249. .used_hw_queues =
  250. BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
  251. BIT(mvm->aux_queue) |
  252. BIT(IWL_MVM_CMD_QUEUE),
  253. .found_vif = false,
  254. };
  255. u32 ac;
  256. int ret, i;
  257. unsigned long used_hw_queues;
  258. /*
  259. * Allocate a MAC ID and a TSF for this MAC, along with the queues
  260. * and other resources.
  261. */
  262. /*
  263. * Before the iterator, we start with all MAC IDs and TSFs available.
  264. *
  265. * During iteration, all MAC IDs are cleared that are in use by other
  266. * virtual interfaces, and all TSF IDs are cleared that can't be used
  267. * by this new virtual interface because they're used by an interface
  268. * that can't share it with the new one.
  269. * At the same time, we check if there's a preferred TSF in the case
  270. * that we should share it with another interface.
  271. */
  272. /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
  273. switch (vif->type) {
  274. case NL80211_IFTYPE_ADHOC:
  275. break;
  276. case NL80211_IFTYPE_STATION:
  277. if (!vif->p2p)
  278. break;
  279. /* fall through */
  280. default:
  281. __clear_bit(0, data.available_mac_ids);
  282. }
  283. ieee80211_iterate_active_interfaces_atomic(
  284. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  285. iwl_mvm_mac_iface_iterator, &data);
  286. /*
  287. * In the case we're getting here during resume, it's similar to
  288. * firmware restart, and with RESUME_ALL the iterator will find
  289. * the vif being added already.
  290. * We don't want to reassign any IDs in either case since doing
  291. * so would probably assign different IDs (as interfaces aren't
  292. * necessarily added in the same order), but the old IDs were
  293. * preserved anyway, so skip ID assignment for both resume and
  294. * recovery.
  295. */
  296. if (data.found_vif)
  297. return 0;
  298. /* Therefore, in recovery, we can't get here */
  299. if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
  300. return -EBUSY;
  301. mvmvif->id = find_first_bit(data.available_mac_ids,
  302. NUM_MAC_INDEX_DRIVER);
  303. if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
  304. IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
  305. ret = -EIO;
  306. goto exit_fail;
  307. }
  308. if (data.preferred_tsf != NUM_TSF_IDS)
  309. mvmvif->tsf_id = data.preferred_tsf;
  310. else
  311. mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
  312. NUM_TSF_IDS);
  313. if (mvmvif->tsf_id == NUM_TSF_IDS) {
  314. IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
  315. ret = -EIO;
  316. goto exit_fail;
  317. }
  318. mvmvif->color = 0;
  319. INIT_LIST_HEAD(&mvmvif->time_event_data.list);
  320. mvmvif->time_event_data.id = TE_MAX;
  321. /* No need to allocate data queues to P2P Device MAC.*/
  322. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  323. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  324. vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
  325. return 0;
  326. }
  327. used_hw_queues = data.used_hw_queues;
  328. /* Find available queues, and allocate them to the ACs */
  329. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  330. u8 queue = find_first_zero_bit(&used_hw_queues,
  331. mvm->first_agg_queue);
  332. if (queue >= mvm->first_agg_queue) {
  333. IWL_ERR(mvm, "Failed to allocate queue\n");
  334. ret = -EIO;
  335. goto exit_fail;
  336. }
  337. __set_bit(queue, &used_hw_queues);
  338. vif->hw_queue[ac] = queue;
  339. }
  340. /* Allocate the CAB queue for softAP and GO interfaces */
  341. if (vif->type == NL80211_IFTYPE_AP) {
  342. u8 queue = find_first_zero_bit(&used_hw_queues,
  343. mvm->first_agg_queue);
  344. if (queue >= mvm->first_agg_queue) {
  345. IWL_ERR(mvm, "Failed to allocate cab queue\n");
  346. ret = -EIO;
  347. goto exit_fail;
  348. }
  349. vif->cab_queue = queue;
  350. } else {
  351. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  352. }
  353. mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
  354. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  355. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
  356. mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
  357. return 0;
  358. exit_fail:
  359. memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
  360. memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
  361. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  362. return ret;
  363. }
  364. int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  365. {
  366. u32 ac;
  367. int ret;
  368. lockdep_assert_held(&mvm->mutex);
  369. ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
  370. if (ret)
  371. return ret;
  372. switch (vif->type) {
  373. case NL80211_IFTYPE_P2P_DEVICE:
  374. iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
  375. IWL_MVM_TX_FIFO_VO);
  376. break;
  377. case NL80211_IFTYPE_AP:
  378. iwl_mvm_enable_ac_txq(mvm, vif->cab_queue,
  379. IWL_MVM_TX_FIFO_MCAST);
  380. /* fall through */
  381. default:
  382. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  383. iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
  384. iwl_mvm_ac_to_tx_fifo[ac]);
  385. break;
  386. }
  387. return 0;
  388. }
  389. void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  390. {
  391. int ac;
  392. lockdep_assert_held(&mvm->mutex);
  393. switch (vif->type) {
  394. case NL80211_IFTYPE_P2P_DEVICE:
  395. iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE);
  396. break;
  397. case NL80211_IFTYPE_AP:
  398. iwl_mvm_disable_txq(mvm, vif->cab_queue);
  399. /* fall through */
  400. default:
  401. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  402. iwl_mvm_disable_txq(mvm, vif->hw_queue[ac]);
  403. }
  404. }
  405. static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
  406. struct ieee80211_vif *vif,
  407. enum ieee80211_band band,
  408. u8 *cck_rates, u8 *ofdm_rates)
  409. {
  410. struct ieee80211_supported_band *sband;
  411. unsigned long basic = vif->bss_conf.basic_rates;
  412. int lowest_present_ofdm = 100;
  413. int lowest_present_cck = 100;
  414. u8 cck = 0;
  415. u8 ofdm = 0;
  416. int i;
  417. sband = mvm->hw->wiphy->bands[band];
  418. for_each_set_bit(i, &basic, BITS_PER_LONG) {
  419. int hw = sband->bitrates[i].hw_value;
  420. if (hw >= IWL_FIRST_OFDM_RATE) {
  421. ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
  422. if (lowest_present_ofdm > hw)
  423. lowest_present_ofdm = hw;
  424. } else {
  425. BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
  426. cck |= BIT(hw);
  427. if (lowest_present_cck > hw)
  428. lowest_present_cck = hw;
  429. }
  430. }
  431. /*
  432. * Now we've got the basic rates as bitmaps in the ofdm and cck
  433. * variables. This isn't sufficient though, as there might not
  434. * be all the right rates in the bitmap. E.g. if the only basic
  435. * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
  436. * and 6 Mbps because the 802.11-2007 standard says in 9.6:
  437. *
  438. * [...] a STA responding to a received frame shall transmit
  439. * its Control Response frame [...] at the highest rate in the
  440. * BSSBasicRateSet parameter that is less than or equal to the
  441. * rate of the immediately previous frame in the frame exchange
  442. * sequence ([...]) and that is of the same modulation class
  443. * ([...]) as the received frame. If no rate contained in the
  444. * BSSBasicRateSet parameter meets these conditions, then the
  445. * control frame sent in response to a received frame shall be
  446. * transmitted at the highest mandatory rate of the PHY that is
  447. * less than or equal to the rate of the received frame, and
  448. * that is of the same modulation class as the received frame.
  449. *
  450. * As a consequence, we need to add all mandatory rates that are
  451. * lower than all of the basic rates to these bitmaps.
  452. */
  453. if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
  454. ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
  455. if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
  456. ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
  457. /* 6M already there or needed so always add */
  458. ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
  459. /*
  460. * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
  461. * Note, however:
  462. * - if no CCK rates are basic, it must be ERP since there must
  463. * be some basic rates at all, so they're OFDM => ERP PHY
  464. * (or we're in 5 GHz, and the cck bitmap will never be used)
  465. * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
  466. * - if 5.5M is basic, 1M and 2M are mandatory
  467. * - if 2M is basic, 1M is mandatory
  468. * - if 1M is basic, that's the only valid ACK rate.
  469. * As a consequence, it's not as complicated as it sounds, just add
  470. * any lower rates to the ACK rate bitmap.
  471. */
  472. if (IWL_RATE_11M_INDEX < lowest_present_cck)
  473. cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
  474. if (IWL_RATE_5M_INDEX < lowest_present_cck)
  475. cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
  476. if (IWL_RATE_2M_INDEX < lowest_present_cck)
  477. cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
  478. /* 1M already there or needed so always add */
  479. cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
  480. *cck_rates = cck;
  481. *ofdm_rates = ofdm;
  482. }
  483. static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
  484. struct ieee80211_vif *vif,
  485. struct iwl_mac_ctx_cmd *cmd)
  486. {
  487. /* for both sta and ap, ht_operation_mode hold the protection_mode */
  488. u8 protection_mode = vif->bss_conf.ht_operation_mode &
  489. IEEE80211_HT_OP_MODE_PROTECTION;
  490. /* The fw does not distinguish between ht and fat */
  491. u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
  492. IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
  493. /*
  494. * See section 9.23.3.1 of IEEE 80211-2012.
  495. * Nongreenfield HT STAs Present is not supported.
  496. */
  497. switch (protection_mode) {
  498. case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
  499. break;
  500. case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
  501. case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
  502. cmd->protection_flags |= cpu_to_le32(ht_flag);
  503. break;
  504. case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
  505. /* Protect when channel wider than 20MHz */
  506. if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
  507. cmd->protection_flags |= cpu_to_le32(ht_flag);
  508. break;
  509. default:
  510. IWL_ERR(mvm, "Illegal protection mode %d\n",
  511. protection_mode);
  512. break;
  513. }
  514. }
  515. static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
  516. struct ieee80211_vif *vif,
  517. struct iwl_mac_ctx_cmd *cmd,
  518. const u8 *bssid_override,
  519. u32 action)
  520. {
  521. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  522. struct ieee80211_chanctx_conf *chanctx;
  523. bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
  524. IEEE80211_HT_OP_MODE_PROTECTION);
  525. u8 cck_ack_rates, ofdm_ack_rates;
  526. const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
  527. int i;
  528. cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  529. mvmvif->color));
  530. cmd->action = cpu_to_le32(action);
  531. switch (vif->type) {
  532. case NL80211_IFTYPE_STATION:
  533. if (vif->p2p)
  534. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
  535. else
  536. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
  537. break;
  538. case NL80211_IFTYPE_AP:
  539. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
  540. break;
  541. case NL80211_IFTYPE_MONITOR:
  542. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
  543. break;
  544. case NL80211_IFTYPE_P2P_DEVICE:
  545. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
  546. break;
  547. case NL80211_IFTYPE_ADHOC:
  548. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
  549. break;
  550. default:
  551. WARN_ON_ONCE(1);
  552. }
  553. cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
  554. memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
  555. if (bssid)
  556. memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
  557. else
  558. eth_broadcast_addr(cmd->bssid_addr);
  559. rcu_read_lock();
  560. chanctx = rcu_dereference(vif->chanctx_conf);
  561. iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
  562. : IEEE80211_BAND_2GHZ,
  563. &cck_ack_rates, &ofdm_ack_rates);
  564. rcu_read_unlock();
  565. cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
  566. cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
  567. cmd->cck_short_preamble =
  568. cpu_to_le32(vif->bss_conf.use_short_preamble ?
  569. MAC_FLG_SHORT_PREAMBLE : 0);
  570. cmd->short_slot =
  571. cpu_to_le32(vif->bss_conf.use_short_slot ?
  572. MAC_FLG_SHORT_SLOT : 0);
  573. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  574. u8 txf = iwl_mvm_ac_to_tx_fifo[i];
  575. cmd->ac[txf].cw_min =
  576. cpu_to_le16(mvmvif->queue_params[i].cw_min);
  577. cmd->ac[txf].cw_max =
  578. cpu_to_le16(mvmvif->queue_params[i].cw_max);
  579. cmd->ac[txf].edca_txop =
  580. cpu_to_le16(mvmvif->queue_params[i].txop * 32);
  581. cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
  582. cmd->ac[txf].fifos_mask = BIT(txf);
  583. }
  584. /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
  585. if (vif->type == NL80211_IFTYPE_AP)
  586. cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
  587. BIT(IWL_MVM_TX_FIFO_MCAST);
  588. if (vif->bss_conf.qos)
  589. cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
  590. if (vif->bss_conf.use_cts_prot)
  591. cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
  592. IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
  593. vif->bss_conf.use_cts_prot,
  594. vif->bss_conf.ht_operation_mode);
  595. if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
  596. cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
  597. if (ht_enabled)
  598. iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
  599. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
  600. }
  601. static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
  602. struct iwl_mac_ctx_cmd *cmd)
  603. {
  604. int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
  605. sizeof(*cmd), cmd);
  606. if (ret)
  607. IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
  608. le32_to_cpu(cmd->action), ret);
  609. return ret;
  610. }
  611. static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
  612. struct ieee80211_vif *vif,
  613. u32 action, bool force_assoc_off,
  614. const u8 *bssid_override)
  615. {
  616. struct iwl_mac_ctx_cmd cmd = {};
  617. struct iwl_mac_data_sta *ctxt_sta;
  618. WARN_ON(vif->type != NL80211_IFTYPE_STATION);
  619. /* Fill the common data for all mac context types */
  620. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
  621. if (vif->p2p) {
  622. struct ieee80211_p2p_noa_attr *noa =
  623. &vif->bss_conf.p2p_noa_attr;
  624. cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
  625. IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
  626. ctxt_sta = &cmd.p2p_sta.sta;
  627. } else {
  628. ctxt_sta = &cmd.sta;
  629. }
  630. /* We need the dtim_period to set the MAC as associated */
  631. if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
  632. !force_assoc_off) {
  633. u32 dtim_offs;
  634. /*
  635. * The DTIM count counts down, so when it is N that means N
  636. * more beacon intervals happen until the DTIM TBTT. Therefore
  637. * add this to the current time. If that ends up being in the
  638. * future, the firmware will handle it.
  639. *
  640. * Also note that the system_timestamp (which we get here as
  641. * "sync_device_ts") and TSF timestamp aren't at exactly the
  642. * same offset in the frame -- the TSF is at the first symbol
  643. * of the TSF, the system timestamp is at signal acquisition
  644. * time. This means there's an offset between them of at most
  645. * a few hundred microseconds (24 * 8 bits + PLCP time gives
  646. * 384us in the longest case), this is currently not relevant
  647. * as the firmware wakes up around 2ms before the TBTT.
  648. */
  649. dtim_offs = vif->bss_conf.sync_dtim_count *
  650. vif->bss_conf.beacon_int;
  651. /* convert TU to usecs */
  652. dtim_offs *= 1024;
  653. ctxt_sta->dtim_tsf =
  654. cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
  655. ctxt_sta->dtim_time =
  656. cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
  657. IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
  658. le64_to_cpu(ctxt_sta->dtim_tsf),
  659. le32_to_cpu(ctxt_sta->dtim_time),
  660. dtim_offs);
  661. ctxt_sta->is_assoc = cpu_to_le32(1);
  662. } else {
  663. ctxt_sta->is_assoc = cpu_to_le32(0);
  664. /* Allow beacons to pass through as long as we are not
  665. * associated, or we do not have dtim period information.
  666. */
  667. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
  668. }
  669. ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
  670. ctxt_sta->bi_reciprocal =
  671. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  672. ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
  673. vif->bss_conf.dtim_period);
  674. ctxt_sta->dtim_reciprocal =
  675. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
  676. vif->bss_conf.dtim_period));
  677. ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
  678. ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
  679. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  680. }
  681. static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
  682. struct ieee80211_vif *vif,
  683. u32 action)
  684. {
  685. struct iwl_mac_ctx_cmd cmd = {};
  686. WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
  687. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  688. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
  689. MAC_FILTER_IN_CONTROL_AND_MGMT |
  690. MAC_FILTER_IN_BEACON |
  691. MAC_FILTER_IN_PROBE_REQUEST |
  692. MAC_FILTER_IN_CRC32);
  693. mvm->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS;
  694. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  695. }
  696. static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
  697. struct ieee80211_vif *vif,
  698. u32 action)
  699. {
  700. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  701. struct iwl_mac_ctx_cmd cmd = {};
  702. WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
  703. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  704. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
  705. MAC_FILTER_IN_PROBE_REQUEST);
  706. /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
  707. cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
  708. cmd.ibss.bi_reciprocal =
  709. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  710. /* TODO: Assumes that the beacon id == mac context id */
  711. cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
  712. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  713. }
  714. struct iwl_mvm_go_iterator_data {
  715. bool go_active;
  716. };
  717. static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
  718. {
  719. struct iwl_mvm_go_iterator_data *data = _data;
  720. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  721. if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
  722. mvmvif->ap_ibss_active)
  723. data->go_active = true;
  724. }
  725. static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
  726. struct ieee80211_vif *vif,
  727. u32 action)
  728. {
  729. struct iwl_mac_ctx_cmd cmd = {};
  730. struct iwl_mvm_go_iterator_data data = {};
  731. WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
  732. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  733. cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
  734. /* Override the filter flags to accept only probe requests */
  735. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  736. /*
  737. * This flag should be set to true when the P2P Device is
  738. * discoverable and there is at least another active P2P GO. Settings
  739. * this flag will allow the P2P Device to be discoverable on other
  740. * channels in addition to its listen channel.
  741. * Note that this flag should not be set in other cases as it opens the
  742. * Rx filters on all MAC and increases the number of interrupts.
  743. */
  744. ieee80211_iterate_active_interfaces_atomic(
  745. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  746. iwl_mvm_go_iterator, &data);
  747. cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
  748. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  749. }
  750. static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
  751. struct iwl_mac_beacon_cmd *beacon_cmd,
  752. u8 *beacon, u32 frame_size)
  753. {
  754. u32 tim_idx;
  755. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  756. /* The index is relative to frame start but we start looking at the
  757. * variable-length part of the beacon. */
  758. tim_idx = mgmt->u.beacon.variable - beacon;
  759. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  760. while ((tim_idx < (frame_size - 2)) &&
  761. (beacon[tim_idx] != WLAN_EID_TIM))
  762. tim_idx += beacon[tim_idx+1] + 2;
  763. /* If TIM field was found, set variables */
  764. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  765. beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
  766. beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
  767. } else {
  768. IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
  769. }
  770. }
  771. static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
  772. struct ieee80211_vif *vif,
  773. struct sk_buff *beacon)
  774. {
  775. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  776. struct iwl_host_cmd cmd = {
  777. .id = BEACON_TEMPLATE_CMD,
  778. .flags = CMD_ASYNC,
  779. };
  780. struct iwl_mac_beacon_cmd beacon_cmd = {};
  781. struct ieee80211_tx_info *info;
  782. u32 beacon_skb_len;
  783. u32 rate, tx_flags;
  784. if (WARN_ON(!beacon))
  785. return -EINVAL;
  786. beacon_skb_len = beacon->len;
  787. /* TODO: for now the beacon template id is set to be the mac context id.
  788. * Might be better to handle it as another resource ... */
  789. beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
  790. info = IEEE80211_SKB_CB(beacon);
  791. /* Set up TX command fields */
  792. beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
  793. beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
  794. beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  795. tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
  796. tx_flags |=
  797. iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
  798. TX_CMD_FLG_BT_PRIO_POS;
  799. beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
  800. mvm->mgmt_last_antenna_idx =
  801. iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
  802. mvm->mgmt_last_antenna_idx);
  803. beacon_cmd.tx.rate_n_flags =
  804. cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
  805. RATE_MCS_ANT_POS);
  806. if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
  807. rate = IWL_FIRST_OFDM_RATE;
  808. } else {
  809. rate = IWL_FIRST_CCK_RATE;
  810. beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
  811. }
  812. beacon_cmd.tx.rate_n_flags |=
  813. cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
  814. /* Set up TX beacon command fields */
  815. if (vif->type == NL80211_IFTYPE_AP)
  816. iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
  817. beacon->data,
  818. beacon_skb_len);
  819. /* Submit command */
  820. cmd.len[0] = sizeof(beacon_cmd);
  821. cmd.data[0] = &beacon_cmd;
  822. cmd.dataflags[0] = 0;
  823. cmd.len[1] = beacon_skb_len;
  824. cmd.data[1] = beacon->data;
  825. cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
  826. return iwl_mvm_send_cmd(mvm, &cmd);
  827. }
  828. /* The beacon template for the AP/GO/IBSS has changed and needs update */
  829. int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
  830. struct ieee80211_vif *vif)
  831. {
  832. struct sk_buff *beacon;
  833. int ret;
  834. WARN_ON(vif->type != NL80211_IFTYPE_AP &&
  835. vif->type != NL80211_IFTYPE_ADHOC);
  836. beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
  837. if (!beacon)
  838. return -ENOMEM;
  839. ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
  840. dev_kfree_skb(beacon);
  841. return ret;
  842. }
  843. struct iwl_mvm_mac_ap_iterator_data {
  844. struct iwl_mvm *mvm;
  845. struct ieee80211_vif *vif;
  846. u32 beacon_device_ts;
  847. u16 beacon_int;
  848. };
  849. /* Find the beacon_device_ts and beacon_int for a managed interface */
  850. static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
  851. struct ieee80211_vif *vif)
  852. {
  853. struct iwl_mvm_mac_ap_iterator_data *data = _data;
  854. if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
  855. return;
  856. /* Station client has higher priority over P2P client*/
  857. if (vif->p2p && data->beacon_device_ts)
  858. return;
  859. data->beacon_device_ts = vif->bss_conf.sync_device_ts;
  860. data->beacon_int = vif->bss_conf.beacon_int;
  861. }
  862. /*
  863. * Fill the specific data for mac context of type AP of P2P GO
  864. */
  865. static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
  866. struct ieee80211_vif *vif,
  867. struct iwl_mac_data_ap *ctxt_ap,
  868. bool add)
  869. {
  870. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  871. struct iwl_mvm_mac_ap_iterator_data data = {
  872. .mvm = mvm,
  873. .vif = vif,
  874. .beacon_device_ts = 0
  875. };
  876. ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
  877. ctxt_ap->bi_reciprocal =
  878. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  879. ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
  880. vif->bss_conf.dtim_period);
  881. ctxt_ap->dtim_reciprocal =
  882. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
  883. vif->bss_conf.dtim_period));
  884. ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
  885. /*
  886. * Only set the beacon time when the MAC is being added, when we
  887. * just modify the MAC then we should keep the time -- the firmware
  888. * can otherwise have a "jumping" TBTT.
  889. */
  890. if (add) {
  891. /*
  892. * If there is a station/P2P client interface which is
  893. * associated, set the AP's TBTT far enough from the station's
  894. * TBTT. Otherwise, set it to the current system time
  895. */
  896. ieee80211_iterate_active_interfaces_atomic(
  897. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  898. iwl_mvm_mac_ap_iterator, &data);
  899. if (data.beacon_device_ts) {
  900. u32 rand = (prandom_u32() % (64 - 36)) + 36;
  901. mvmvif->ap_beacon_time = data.beacon_device_ts +
  902. ieee80211_tu_to_usec(data.beacon_int * rand /
  903. 100);
  904. } else {
  905. mvmvif->ap_beacon_time =
  906. iwl_read_prph(mvm->trans,
  907. DEVICE_SYSTEM_TIME_REG);
  908. }
  909. }
  910. ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
  911. ctxt_ap->beacon_tsf = 0; /* unused */
  912. /* TODO: Assume that the beacon id == mac context id */
  913. ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
  914. }
  915. static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
  916. struct ieee80211_vif *vif,
  917. u32 action)
  918. {
  919. struct iwl_mac_ctx_cmd cmd = {};
  920. WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
  921. /* Fill the common data for all mac context types */
  922. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  923. /*
  924. * pass probe requests and beacons from other APs (needed
  925. * for ht protection)
  926. */
  927. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
  928. MAC_FILTER_IN_BEACON);
  929. /* Fill the data specific for ap mode */
  930. iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
  931. action == FW_CTXT_ACTION_ADD);
  932. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  933. }
  934. static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
  935. struct ieee80211_vif *vif,
  936. u32 action)
  937. {
  938. struct iwl_mac_ctx_cmd cmd = {};
  939. struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
  940. WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
  941. /* Fill the common data for all mac context types */
  942. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
  943. /*
  944. * pass probe requests and beacons from other APs (needed
  945. * for ht protection)
  946. */
  947. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
  948. MAC_FILTER_IN_BEACON);
  949. /* Fill the data specific for GO mode */
  950. iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
  951. action == FW_CTXT_ACTION_ADD);
  952. cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
  953. IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
  954. cmd.go.opp_ps_enabled =
  955. cpu_to_le32(!!(noa->oppps_ctwindow &
  956. IEEE80211_P2P_OPPPS_ENABLE_BIT));
  957. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  958. }
  959. static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  960. u32 action, bool force_assoc_off,
  961. const u8 *bssid_override)
  962. {
  963. switch (vif->type) {
  964. case NL80211_IFTYPE_STATION:
  965. return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
  966. force_assoc_off,
  967. bssid_override);
  968. break;
  969. case NL80211_IFTYPE_AP:
  970. if (!vif->p2p)
  971. return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
  972. else
  973. return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
  974. break;
  975. case NL80211_IFTYPE_MONITOR:
  976. return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
  977. case NL80211_IFTYPE_P2P_DEVICE:
  978. return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
  979. case NL80211_IFTYPE_ADHOC:
  980. return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
  981. default:
  982. break;
  983. }
  984. return -EOPNOTSUPP;
  985. }
  986. int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  987. {
  988. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  989. int ret;
  990. if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
  991. vif->addr, ieee80211_vif_type_p2p(vif)))
  992. return -EIO;
  993. ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
  994. true, NULL);
  995. if (ret)
  996. return ret;
  997. /* will only do anything at resume from D3 time */
  998. iwl_mvm_set_last_nonqos_seq(mvm, vif);
  999. mvmvif->uploaded = true;
  1000. return 0;
  1001. }
  1002. int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1003. bool force_assoc_off, const u8 *bssid_override)
  1004. {
  1005. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1006. if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
  1007. vif->addr, ieee80211_vif_type_p2p(vif)))
  1008. return -EIO;
  1009. return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
  1010. force_assoc_off, bssid_override);
  1011. }
  1012. int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  1013. {
  1014. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1015. struct iwl_mac_ctx_cmd cmd;
  1016. int ret;
  1017. if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
  1018. vif->addr, ieee80211_vif_type_p2p(vif)))
  1019. return -EIO;
  1020. memset(&cmd, 0, sizeof(cmd));
  1021. cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  1022. mvmvif->color));
  1023. cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
  1024. ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
  1025. sizeof(cmd), &cmd);
  1026. if (ret) {
  1027. IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
  1028. return ret;
  1029. }
  1030. mvmvif->uploaded = false;
  1031. if (vif->type == NL80211_IFTYPE_MONITOR)
  1032. mvm->hw->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
  1033. return 0;
  1034. }
  1035. static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
  1036. struct ieee80211_vif *csa_vif, u32 gp2)
  1037. {
  1038. struct iwl_mvm_vif *mvmvif =
  1039. iwl_mvm_vif_from_mac80211(csa_vif);
  1040. if (!ieee80211_csa_is_complete(csa_vif)) {
  1041. int c = ieee80211_csa_update_counter(csa_vif);
  1042. iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
  1043. if (csa_vif->p2p &&
  1044. !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2) {
  1045. u32 rel_time = (c + 1) *
  1046. csa_vif->bss_conf.beacon_int -
  1047. IWL_MVM_CHANNEL_SWITCH_TIME_GO;
  1048. u32 apply_time = gp2 + rel_time * 1024;
  1049. iwl_mvm_schedule_csa_period(mvm, csa_vif,
  1050. IWL_MVM_CHANNEL_SWITCH_TIME_GO -
  1051. IWL_MVM_CHANNEL_SWITCH_MARGIN,
  1052. apply_time);
  1053. }
  1054. } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
  1055. /* we don't have CSA NoA scheduled yet, switch now */
  1056. ieee80211_csa_finish(csa_vif);
  1057. RCU_INIT_POINTER(mvm->csa_vif, NULL);
  1058. }
  1059. }
  1060. int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
  1061. struct iwl_rx_cmd_buffer *rxb,
  1062. struct iwl_device_cmd *cmd)
  1063. {
  1064. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1065. struct iwl_mvm_tx_resp *beacon_notify_hdr;
  1066. struct ieee80211_vif *csa_vif;
  1067. struct ieee80211_vif *tx_blocked_vif;
  1068. u64 tsf;
  1069. lockdep_assert_held(&mvm->mutex);
  1070. if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_CAPA_EXTENDED_BEACON) {
  1071. struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
  1072. beacon_notify_hdr = &beacon->beacon_notify_hdr;
  1073. tsf = le64_to_cpu(beacon->tsf);
  1074. mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
  1075. } else {
  1076. struct iwl_beacon_notif *beacon = (void *)pkt->data;
  1077. beacon_notify_hdr = &beacon->beacon_notify_hdr;
  1078. tsf = le64_to_cpu(beacon->tsf);
  1079. }
  1080. IWL_DEBUG_RX(mvm,
  1081. "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
  1082. le16_to_cpu(beacon_notify_hdr->status.status) &
  1083. TX_STATUS_MSK,
  1084. beacon_notify_hdr->failure_frame, tsf,
  1085. mvm->ap_last_beacon_gp2,
  1086. le32_to_cpu(beacon_notify_hdr->initial_rate));
  1087. csa_vif = rcu_dereference_protected(mvm->csa_vif,
  1088. lockdep_is_held(&mvm->mutex));
  1089. if (unlikely(csa_vif && csa_vif->csa_active))
  1090. iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2);
  1091. tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
  1092. lockdep_is_held(&mvm->mutex));
  1093. if (unlikely(tx_blocked_vif)) {
  1094. struct iwl_mvm_vif *mvmvif =
  1095. iwl_mvm_vif_from_mac80211(tx_blocked_vif);
  1096. /*
  1097. * The channel switch is started and we have blocked the
  1098. * stations. If this is the first beacon (the timeout wasn't
  1099. * set), set the unblock timeout, otherwise countdown
  1100. */
  1101. if (!mvm->csa_tx_block_bcn_timeout)
  1102. mvm->csa_tx_block_bcn_timeout =
  1103. IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
  1104. else
  1105. mvm->csa_tx_block_bcn_timeout--;
  1106. /* Check if the timeout is expired, and unblock tx */
  1107. if (mvm->csa_tx_block_bcn_timeout == 0) {
  1108. iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
  1109. RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
  1110. }
  1111. }
  1112. return 0;
  1113. }
  1114. static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
  1115. struct ieee80211_vif *vif)
  1116. {
  1117. struct iwl_missed_beacons_notif *missed_beacons = _data;
  1118. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1119. if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
  1120. return;
  1121. /*
  1122. * TODO: the threshold should be adjusted based on latency conditions,
  1123. * and/or in case of a CS flow on one of the other AP vifs.
  1124. */
  1125. if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
  1126. IWL_MVM_MISSED_BEACONS_THRESHOLD)
  1127. ieee80211_beacon_loss(vif);
  1128. }
  1129. int iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
  1130. struct iwl_rx_cmd_buffer *rxb,
  1131. struct iwl_device_cmd *cmd)
  1132. {
  1133. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1134. struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
  1135. IWL_DEBUG_INFO(mvm,
  1136. "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
  1137. le32_to_cpu(mb->mac_id),
  1138. le32_to_cpu(mb->consec_missed_beacons),
  1139. le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
  1140. le32_to_cpu(mb->num_recvd_beacons),
  1141. le32_to_cpu(mb->num_expected_beacons));
  1142. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  1143. IEEE80211_IFACE_ITER_NORMAL,
  1144. iwl_mvm_beacon_loss_iterator,
  1145. mb);
  1146. return 0;
  1147. }