mac-ctxt.c 37 KB

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