mac-ctxt.c 39 KB

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