mac-ctxt.c 44 KB

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