mac-ctxt.c 39 KB

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