mac-ctxt.c 31 KB

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