mac-ctxt.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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_FIRST_AGG_QUEUE)];
  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, ac;
  185. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  186. return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
  187. qmask = (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE) ?
  188. BIT(vif->cab_queue) : 0;
  189. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  190. if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
  191. qmask |= BIT(vif->hw_queue[ac]);
  192. return qmask;
  193. }
  194. static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
  195. struct ieee80211_vif *vif)
  196. {
  197. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  198. struct iwl_mvm_mac_iface_iterator_data data = {
  199. .mvm = mvm,
  200. .vif = vif,
  201. .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
  202. .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
  203. /* no preference yet */
  204. .preferred_tsf = NUM_TSF_IDS,
  205. .used_hw_queues = {
  206. BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
  207. BIT(IWL_MVM_AUX_QUEUE) |
  208. BIT(IWL_MVM_CMD_QUEUE)
  209. },
  210. .found_vif = false,
  211. };
  212. u32 ac;
  213. int ret, i;
  214. /*
  215. * Allocate a MAC ID and a TSF for this MAC, along with the queues
  216. * and other resources.
  217. */
  218. /*
  219. * Before the iterator, we start with all MAC IDs and TSFs available.
  220. *
  221. * During iteration, all MAC IDs are cleared that are in use by other
  222. * virtual interfaces, and all TSF IDs are cleared that can't be used
  223. * by this new virtual interface because they're used by an interface
  224. * that can't share it with the new one.
  225. * At the same time, we check if there's a preferred TSF in the case
  226. * that we should share it with another interface.
  227. */
  228. /* Currently, MAC ID 0 should be used only for the managed vif */
  229. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  230. __clear_bit(0, data.available_mac_ids);
  231. ieee80211_iterate_active_interfaces_atomic(
  232. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  233. iwl_mvm_mac_iface_iterator, &data);
  234. /*
  235. * In the case we're getting here during resume, it's similar to
  236. * firmware restart, and with RESUME_ALL the iterator will find
  237. * the vif being added already.
  238. * We don't want to reassign any IDs in either case since doing
  239. * so would probably assign different IDs (as interfaces aren't
  240. * necessarily added in the same order), but the old IDs were
  241. * preserved anyway, so skip ID assignment for both resume and
  242. * recovery.
  243. */
  244. if (data.found_vif)
  245. return 0;
  246. /* Therefore, in recovery, we can't get here */
  247. WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
  248. mvmvif->id = find_first_bit(data.available_mac_ids,
  249. NUM_MAC_INDEX_DRIVER);
  250. if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
  251. IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
  252. ret = -EIO;
  253. goto exit_fail;
  254. }
  255. if (data.preferred_tsf != NUM_TSF_IDS)
  256. mvmvif->tsf_id = data.preferred_tsf;
  257. else
  258. mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
  259. NUM_TSF_IDS);
  260. if (mvmvif->tsf_id == NUM_TSF_IDS) {
  261. IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
  262. ret = -EIO;
  263. goto exit_fail;
  264. }
  265. mvmvif->color = 0;
  266. INIT_LIST_HEAD(&mvmvif->time_event_data.list);
  267. mvmvif->time_event_data.id = TE_MAX;
  268. /* No need to allocate data queues to P2P Device MAC.*/
  269. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  270. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  271. vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
  272. return 0;
  273. }
  274. /* Find available queues, and allocate them to the ACs */
  275. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  276. u8 queue = find_first_zero_bit(data.used_hw_queues,
  277. IWL_MVM_FIRST_AGG_QUEUE);
  278. if (queue >= IWL_MVM_FIRST_AGG_QUEUE) {
  279. IWL_ERR(mvm, "Failed to allocate queue\n");
  280. ret = -EIO;
  281. goto exit_fail;
  282. }
  283. __set_bit(queue, data.used_hw_queues);
  284. vif->hw_queue[ac] = queue;
  285. }
  286. /* Allocate the CAB queue for softAP and GO interfaces */
  287. if (vif->type == NL80211_IFTYPE_AP) {
  288. u8 queue = find_first_zero_bit(data.used_hw_queues,
  289. IWL_MVM_FIRST_AGG_QUEUE);
  290. if (queue >= IWL_MVM_FIRST_AGG_QUEUE) {
  291. IWL_ERR(mvm, "Failed to allocate cab queue\n");
  292. ret = -EIO;
  293. goto exit_fail;
  294. }
  295. vif->cab_queue = queue;
  296. } else {
  297. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  298. }
  299. mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
  300. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  301. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
  302. mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
  303. return 0;
  304. exit_fail:
  305. memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
  306. memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
  307. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  308. return ret;
  309. }
  310. int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  311. {
  312. u32 ac;
  313. int ret;
  314. lockdep_assert_held(&mvm->mutex);
  315. ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
  316. if (ret)
  317. return ret;
  318. switch (vif->type) {
  319. case NL80211_IFTYPE_P2P_DEVICE:
  320. iwl_trans_ac_txq_enable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE,
  321. IWL_MVM_TX_FIFO_VO);
  322. break;
  323. case NL80211_IFTYPE_AP:
  324. iwl_trans_ac_txq_enable(mvm->trans, vif->cab_queue,
  325. IWL_MVM_TX_FIFO_MCAST);
  326. /* fall through */
  327. default:
  328. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  329. iwl_trans_ac_txq_enable(mvm->trans, vif->hw_queue[ac],
  330. iwl_mvm_ac_to_tx_fifo[ac]);
  331. break;
  332. }
  333. return 0;
  334. }
  335. void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  336. {
  337. int ac;
  338. lockdep_assert_held(&mvm->mutex);
  339. switch (vif->type) {
  340. case NL80211_IFTYPE_P2P_DEVICE:
  341. iwl_trans_txq_disable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE);
  342. break;
  343. case NL80211_IFTYPE_AP:
  344. iwl_trans_txq_disable(mvm->trans, vif->cab_queue);
  345. /* fall through */
  346. default:
  347. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  348. iwl_trans_txq_disable(mvm->trans, vif->hw_queue[ac]);
  349. }
  350. }
  351. static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
  352. struct ieee80211_vif *vif,
  353. enum ieee80211_band band,
  354. u8 *cck_rates, u8 *ofdm_rates)
  355. {
  356. struct ieee80211_supported_band *sband;
  357. unsigned long basic = vif->bss_conf.basic_rates;
  358. int lowest_present_ofdm = 100;
  359. int lowest_present_cck = 100;
  360. u8 cck = 0;
  361. u8 ofdm = 0;
  362. int i;
  363. sband = mvm->hw->wiphy->bands[band];
  364. for_each_set_bit(i, &basic, BITS_PER_LONG) {
  365. int hw = sband->bitrates[i].hw_value;
  366. if (hw >= IWL_FIRST_OFDM_RATE) {
  367. ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
  368. if (lowest_present_ofdm > hw)
  369. lowest_present_ofdm = hw;
  370. } else {
  371. BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
  372. cck |= BIT(hw);
  373. if (lowest_present_cck > hw)
  374. lowest_present_cck = hw;
  375. }
  376. }
  377. /*
  378. * Now we've got the basic rates as bitmaps in the ofdm and cck
  379. * variables. This isn't sufficient though, as there might not
  380. * be all the right rates in the bitmap. E.g. if the only basic
  381. * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
  382. * and 6 Mbps because the 802.11-2007 standard says in 9.6:
  383. *
  384. * [...] a STA responding to a received frame shall transmit
  385. * its Control Response frame [...] at the highest rate in the
  386. * BSSBasicRateSet parameter that is less than or equal to the
  387. * rate of the immediately previous frame in the frame exchange
  388. * sequence ([...]) and that is of the same modulation class
  389. * ([...]) as the received frame. If no rate contained in the
  390. * BSSBasicRateSet parameter meets these conditions, then the
  391. * control frame sent in response to a received frame shall be
  392. * transmitted at the highest mandatory rate of the PHY that is
  393. * less than or equal to the rate of the received frame, and
  394. * that is of the same modulation class as the received frame.
  395. *
  396. * As a consequence, we need to add all mandatory rates that are
  397. * lower than all of the basic rates to these bitmaps.
  398. */
  399. if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
  400. ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
  401. if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
  402. ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
  403. /* 6M already there or needed so always add */
  404. ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
  405. /*
  406. * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
  407. * Note, however:
  408. * - if no CCK rates are basic, it must be ERP since there must
  409. * be some basic rates at all, so they're OFDM => ERP PHY
  410. * (or we're in 5 GHz, and the cck bitmap will never be used)
  411. * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
  412. * - if 5.5M is basic, 1M and 2M are mandatory
  413. * - if 2M is basic, 1M is mandatory
  414. * - if 1M is basic, that's the only valid ACK rate.
  415. * As a consequence, it's not as complicated as it sounds, just add
  416. * any lower rates to the ACK rate bitmap.
  417. */
  418. if (IWL_RATE_11M_INDEX < lowest_present_cck)
  419. cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
  420. if (IWL_RATE_5M_INDEX < lowest_present_cck)
  421. cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
  422. if (IWL_RATE_2M_INDEX < lowest_present_cck)
  423. cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
  424. /* 1M already there or needed so always add */
  425. cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
  426. *cck_rates = cck;
  427. *ofdm_rates = ofdm;
  428. }
  429. static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
  430. struct ieee80211_vif *vif,
  431. struct iwl_mac_ctx_cmd *cmd,
  432. u32 action)
  433. {
  434. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  435. struct ieee80211_chanctx_conf *chanctx;
  436. u8 cck_ack_rates, ofdm_ack_rates;
  437. int i;
  438. cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  439. mvmvif->color));
  440. cmd->action = cpu_to_le32(action);
  441. switch (vif->type) {
  442. case NL80211_IFTYPE_STATION:
  443. if (vif->p2p)
  444. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
  445. else
  446. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
  447. break;
  448. case NL80211_IFTYPE_AP:
  449. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
  450. break;
  451. case NL80211_IFTYPE_MONITOR:
  452. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
  453. break;
  454. case NL80211_IFTYPE_P2P_DEVICE:
  455. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
  456. break;
  457. case NL80211_IFTYPE_ADHOC:
  458. cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
  459. break;
  460. default:
  461. WARN_ON_ONCE(1);
  462. }
  463. cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
  464. memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
  465. if (vif->bss_conf.bssid)
  466. memcpy(cmd->bssid_addr, vif->bss_conf.bssid, ETH_ALEN);
  467. else
  468. eth_broadcast_addr(cmd->bssid_addr);
  469. rcu_read_lock();
  470. chanctx = rcu_dereference(vif->chanctx_conf);
  471. iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
  472. : IEEE80211_BAND_2GHZ,
  473. &cck_ack_rates, &ofdm_ack_rates);
  474. rcu_read_unlock();
  475. cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
  476. cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
  477. cmd->cck_short_preamble =
  478. cpu_to_le32(vif->bss_conf.use_short_preamble ?
  479. MAC_FLG_SHORT_PREAMBLE : 0);
  480. cmd->short_slot =
  481. cpu_to_le32(vif->bss_conf.use_short_slot ?
  482. MAC_FLG_SHORT_SLOT : 0);
  483. for (i = 0; i < AC_NUM; i++) {
  484. cmd->ac[i].cw_min = cpu_to_le16(mvmvif->queue_params[i].cw_min);
  485. cmd->ac[i].cw_max = cpu_to_le16(mvmvif->queue_params[i].cw_max);
  486. cmd->ac[i].aifsn = mvmvif->queue_params[i].aifs;
  487. cmd->ac[i].edca_txop =
  488. cpu_to_le16(mvmvif->queue_params[i].txop * 32);
  489. cmd->ac[i].fifos_mask = BIT(iwl_mvm_ac_to_tx_fifo[i]);
  490. }
  491. /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
  492. if (vif->type == NL80211_IFTYPE_AP)
  493. cmd->ac[AC_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
  494. if (vif->bss_conf.qos)
  495. cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
  496. /* Don't use cts to self as the fw doesn't support it currently. */
  497. if (vif->bss_conf.use_cts_prot)
  498. cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
  499. /*
  500. * I think that we should enable these 2 flags regardless the HT PROT
  501. * fields in the HT IE, but I am not sure. Someone knows whom to ask?...
  502. */
  503. if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
  504. cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
  505. cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_HT_PROT |
  506. MAC_PROT_FLG_FAT_PROT);
  507. }
  508. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
  509. }
  510. static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
  511. struct iwl_mac_ctx_cmd *cmd)
  512. {
  513. int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
  514. sizeof(*cmd), cmd);
  515. if (ret)
  516. IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
  517. le32_to_cpu(cmd->action), ret);
  518. return ret;
  519. }
  520. /*
  521. * Fill the specific data for mac context of type station or p2p client
  522. */
  523. static void iwl_mvm_mac_ctxt_cmd_fill_sta(struct iwl_mvm *mvm,
  524. struct ieee80211_vif *vif,
  525. struct iwl_mac_data_sta *ctxt_sta,
  526. bool force_assoc_off)
  527. {
  528. /* We need the dtim_period to set the MAC as associated */
  529. if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
  530. !force_assoc_off) {
  531. u32 dtim_offs;
  532. /*
  533. * The DTIM count counts down, so when it is N that means N
  534. * more beacon intervals happen until the DTIM TBTT. Therefore
  535. * add this to the current time. If that ends up being in the
  536. * future, the firmware will handle it.
  537. *
  538. * Also note that the system_timestamp (which we get here as
  539. * "sync_device_ts") and TSF timestamp aren't at exactly the
  540. * same offset in the frame -- the TSF is at the first symbol
  541. * of the TSF, the system timestamp is at signal acquisition
  542. * time. This means there's an offset between them of at most
  543. * a few hundred microseconds (24 * 8 bits + PLCP time gives
  544. * 384us in the longest case), this is currently not relevant
  545. * as the firmware wakes up around 2ms before the TBTT.
  546. */
  547. dtim_offs = vif->bss_conf.sync_dtim_count *
  548. vif->bss_conf.beacon_int;
  549. /* convert TU to usecs */
  550. dtim_offs *= 1024;
  551. ctxt_sta->dtim_tsf =
  552. cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
  553. ctxt_sta->dtim_time =
  554. cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
  555. IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
  556. le64_to_cpu(ctxt_sta->dtim_tsf),
  557. le32_to_cpu(ctxt_sta->dtim_time),
  558. dtim_offs);
  559. ctxt_sta->is_assoc = cpu_to_le32(1);
  560. } else {
  561. ctxt_sta->is_assoc = cpu_to_le32(0);
  562. }
  563. ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
  564. ctxt_sta->bi_reciprocal =
  565. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  566. ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
  567. vif->bss_conf.dtim_period);
  568. ctxt_sta->dtim_reciprocal =
  569. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
  570. vif->bss_conf.dtim_period));
  571. ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
  572. ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
  573. }
  574. static int iwl_mvm_mac_ctxt_cmd_station(struct iwl_mvm *mvm,
  575. struct ieee80211_vif *vif,
  576. u32 action)
  577. {
  578. struct iwl_mac_ctx_cmd cmd = {};
  579. WARN_ON(vif->type != NL80211_IFTYPE_STATION || vif->p2p);
  580. /* Fill the common data for all mac context types */
  581. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  582. /* Allow beacons to pass through as long as we are not associated,or we
  583. * do not have dtim period information */
  584. if (!vif->bss_conf.assoc || !vif->bss_conf.dtim_period)
  585. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
  586. else
  587. cmd.filter_flags &= ~cpu_to_le32(MAC_FILTER_IN_BEACON);
  588. /* Fill the data specific for station mode */
  589. iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.sta,
  590. action == FW_CTXT_ACTION_ADD);
  591. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  592. }
  593. static int iwl_mvm_mac_ctxt_cmd_p2p_client(struct iwl_mvm *mvm,
  594. struct ieee80211_vif *vif,
  595. u32 action)
  596. {
  597. struct iwl_mac_ctx_cmd cmd = {};
  598. struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
  599. WARN_ON(vif->type != NL80211_IFTYPE_STATION || !vif->p2p);
  600. /* Fill the common data for all mac context types */
  601. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  602. /* Fill the data specific for station mode */
  603. iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.p2p_sta.sta,
  604. action == FW_CTXT_ACTION_ADD);
  605. cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
  606. IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
  607. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  608. }
  609. static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
  610. struct ieee80211_vif *vif,
  611. u32 action)
  612. {
  613. struct iwl_mac_ctx_cmd cmd = {};
  614. WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
  615. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  616. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
  617. MAC_FILTER_IN_CONTROL_AND_MGMT |
  618. MAC_FILTER_IN_BEACON |
  619. MAC_FILTER_IN_PROBE_REQUEST);
  620. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  621. }
  622. struct iwl_mvm_go_iterator_data {
  623. bool go_active;
  624. };
  625. static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
  626. {
  627. struct iwl_mvm_go_iterator_data *data = _data;
  628. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  629. if (vif->type == NL80211_IFTYPE_AP && vif->p2p && mvmvif->ap_active)
  630. data->go_active = true;
  631. }
  632. static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
  633. struct ieee80211_vif *vif,
  634. u32 action)
  635. {
  636. struct iwl_mac_ctx_cmd cmd = {};
  637. struct iwl_mvm_go_iterator_data data = {};
  638. WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
  639. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  640. cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
  641. /* Override the filter flags to accept only probe requests */
  642. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  643. /*
  644. * This flag should be set to true when the P2P Device is
  645. * discoverable and there is at least another active P2P GO. Settings
  646. * this flag will allow the P2P Device to be discoverable on other
  647. * channels in addition to its listen channel.
  648. * Note that this flag should not be set in other cases as it opens the
  649. * Rx filters on all MAC and increases the number of interrupts.
  650. */
  651. ieee80211_iterate_active_interfaces_atomic(
  652. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  653. iwl_mvm_go_iterator, &data);
  654. cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
  655. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  656. }
  657. static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
  658. struct iwl_mac_beacon_cmd *beacon_cmd,
  659. u8 *beacon, u32 frame_size)
  660. {
  661. u32 tim_idx;
  662. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  663. /* The index is relative to frame start but we start looking at the
  664. * variable-length part of the beacon. */
  665. tim_idx = mgmt->u.beacon.variable - beacon;
  666. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  667. while ((tim_idx < (frame_size - 2)) &&
  668. (beacon[tim_idx] != WLAN_EID_TIM))
  669. tim_idx += beacon[tim_idx+1] + 2;
  670. /* If TIM field was found, set variables */
  671. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  672. beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
  673. beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
  674. } else {
  675. IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
  676. }
  677. }
  678. static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
  679. struct ieee80211_vif *vif,
  680. struct sk_buff *beacon)
  681. {
  682. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  683. struct iwl_host_cmd cmd = {
  684. .id = BEACON_TEMPLATE_CMD,
  685. .flags = CMD_ASYNC,
  686. };
  687. struct iwl_mac_beacon_cmd beacon_cmd = {};
  688. struct ieee80211_tx_info *info;
  689. u32 beacon_skb_len;
  690. u32 rate;
  691. if (WARN_ON(!beacon))
  692. return -EINVAL;
  693. beacon_skb_len = beacon->len;
  694. /* TODO: for now the beacon template id is set to be the mac context id.
  695. * Might be better to handle it as another resource ... */
  696. beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
  697. /* Set up TX command fields */
  698. beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
  699. beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
  700. beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  701. beacon_cmd.tx.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  702. TX_CMD_FLG_BT_DIS |
  703. TX_CMD_FLG_TSF);
  704. mvm->mgmt_last_antenna_idx =
  705. iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
  706. mvm->mgmt_last_antenna_idx);
  707. beacon_cmd.tx.rate_n_flags =
  708. cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
  709. RATE_MCS_ANT_POS);
  710. info = IEEE80211_SKB_CB(beacon);
  711. if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
  712. rate = IWL_FIRST_OFDM_RATE;
  713. } else {
  714. rate = IWL_FIRST_CCK_RATE;
  715. beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
  716. }
  717. beacon_cmd.tx.rate_n_flags |=
  718. cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
  719. /* Set up TX beacon command fields */
  720. iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
  721. beacon->data,
  722. beacon_skb_len);
  723. /* Submit command */
  724. cmd.len[0] = sizeof(beacon_cmd);
  725. cmd.data[0] = &beacon_cmd;
  726. cmd.dataflags[0] = 0;
  727. cmd.len[1] = beacon_skb_len;
  728. cmd.data[1] = beacon->data;
  729. cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
  730. return iwl_mvm_send_cmd(mvm, &cmd);
  731. }
  732. /* The beacon template for the AP/GO context has changed and needs update */
  733. int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
  734. struct ieee80211_vif *vif)
  735. {
  736. struct sk_buff *beacon;
  737. int ret;
  738. WARN_ON(vif->type != NL80211_IFTYPE_AP);
  739. beacon = ieee80211_beacon_get(mvm->hw, vif);
  740. if (!beacon)
  741. return -ENOMEM;
  742. ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
  743. dev_kfree_skb(beacon);
  744. return ret;
  745. }
  746. /*
  747. * Fill the specific data for mac context of type AP of P2P GO
  748. */
  749. static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
  750. struct ieee80211_vif *vif,
  751. struct iwl_mac_data_ap *ctxt_ap,
  752. bool add)
  753. {
  754. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  755. ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
  756. ctxt_ap->bi_reciprocal =
  757. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  758. ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
  759. vif->bss_conf.dtim_period);
  760. ctxt_ap->dtim_reciprocal =
  761. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
  762. vif->bss_conf.dtim_period));
  763. ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
  764. /*
  765. * Only read the system time when the MAC is being added, when we
  766. * just modify the MAC then we should keep the time -- the firmware
  767. * can otherwise have a "jumping" TBTT.
  768. */
  769. if (add)
  770. mvmvif->ap_beacon_time =
  771. iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG);
  772. ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
  773. ctxt_ap->beacon_tsf = 0; /* unused */
  774. /* TODO: Assume that the beacon id == mac context id */
  775. ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
  776. }
  777. static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
  778. struct ieee80211_vif *vif,
  779. u32 action)
  780. {
  781. struct iwl_mac_ctx_cmd cmd = {};
  782. WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
  783. /* Fill the common data for all mac context types */
  784. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  785. /* Also enable probe requests to pass */
  786. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  787. /* Fill the data specific for ap mode */
  788. iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
  789. action == FW_CTXT_ACTION_ADD);
  790. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  791. }
  792. static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
  793. struct ieee80211_vif *vif,
  794. u32 action)
  795. {
  796. struct iwl_mac_ctx_cmd cmd = {};
  797. struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
  798. WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
  799. /* Fill the common data for all mac context types */
  800. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  801. /* Fill the data specific for GO mode */
  802. iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
  803. action == FW_CTXT_ACTION_ADD);
  804. cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
  805. IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
  806. cmd.go.opp_ps_enabled =
  807. cpu_to_le32(!!(noa->oppps_ctwindow &
  808. IEEE80211_P2P_OPPPS_ENABLE_BIT));
  809. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  810. }
  811. static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  812. u32 action)
  813. {
  814. switch (vif->type) {
  815. case NL80211_IFTYPE_STATION:
  816. if (!vif->p2p)
  817. return iwl_mvm_mac_ctxt_cmd_station(mvm, vif,
  818. action);
  819. else
  820. return iwl_mvm_mac_ctxt_cmd_p2p_client(mvm, vif,
  821. action);
  822. break;
  823. case NL80211_IFTYPE_AP:
  824. if (!vif->p2p)
  825. return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
  826. else
  827. return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
  828. break;
  829. case NL80211_IFTYPE_MONITOR:
  830. return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
  831. case NL80211_IFTYPE_P2P_DEVICE:
  832. return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
  833. default:
  834. break;
  835. }
  836. return -EOPNOTSUPP;
  837. }
  838. int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  839. {
  840. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  841. int ret;
  842. if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
  843. vif->addr, ieee80211_vif_type_p2p(vif)))
  844. return -EIO;
  845. ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD);
  846. if (ret)
  847. return ret;
  848. mvmvif->uploaded = true;
  849. return 0;
  850. }
  851. int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  852. {
  853. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  854. if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
  855. vif->addr, ieee80211_vif_type_p2p(vif)))
  856. return -EIO;
  857. return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY);
  858. }
  859. int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  860. {
  861. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  862. struct iwl_mac_ctx_cmd cmd;
  863. int ret;
  864. if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
  865. vif->addr, ieee80211_vif_type_p2p(vif)))
  866. return -EIO;
  867. memset(&cmd, 0, sizeof(cmd));
  868. cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  869. mvmvif->color));
  870. cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
  871. ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
  872. sizeof(cmd), &cmd);
  873. if (ret) {
  874. IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
  875. return ret;
  876. }
  877. mvmvif->uploaded = false;
  878. return 0;
  879. }
  880. int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
  881. struct iwl_rx_cmd_buffer *rxb,
  882. struct iwl_device_cmd *cmd)
  883. {
  884. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  885. struct iwl_beacon_notif *beacon = (void *)pkt->data;
  886. u16 status __maybe_unused =
  887. le16_to_cpu(beacon->beacon_notify_hdr.status.status);
  888. u32 rate __maybe_unused =
  889. le32_to_cpu(beacon->beacon_notify_hdr.initial_rate);
  890. IWL_DEBUG_RX(mvm, "beacon status %#x retries:%d tsf:0x%16llX rate:%d\n",
  891. status & TX_STATUS_MSK,
  892. beacon->beacon_notify_hdr.failure_frame,
  893. le64_to_cpu(beacon->tsf),
  894. rate);
  895. return 0;
  896. }
  897. static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
  898. struct ieee80211_vif *vif)
  899. {
  900. u16 *id = _data;
  901. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  902. if (mvmvif->id == *id)
  903. ieee80211_beacon_loss(vif);
  904. }
  905. int iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
  906. struct iwl_rx_cmd_buffer *rxb,
  907. struct iwl_device_cmd *cmd)
  908. {
  909. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  910. struct iwl_missed_beacons_notif *missed_beacons = (void *)pkt->data;
  911. u16 id = (u16)le32_to_cpu(missed_beacons->mac_id);
  912. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  913. IEEE80211_IFACE_ITER_NORMAL,
  914. iwl_mvm_beacon_loss_iterator,
  915. &id);
  916. return 0;
  917. }