mac-ctxt.c 45 KB

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