mac-ctxt.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called COPYING.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/etherdevice.h>
  64. #include <net/mac80211.h>
  65. #include "iwl-io.h"
  66. #include "iwl-prph.h"
  67. #include "fw-api.h"
  68. #include "mvm.h"
  69. const u8 iwl_mvm_ac_to_tx_fifo[] = {
  70. IWL_MVM_TX_FIFO_BK,
  71. IWL_MVM_TX_FIFO_BE,
  72. IWL_MVM_TX_FIFO_VI,
  73. IWL_MVM_TX_FIFO_VO,
  74. };
  75. struct iwl_mvm_mac_iface_iterator_data {
  76. struct iwl_mvm *mvm;
  77. struct ieee80211_vif *vif;
  78. unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
  79. unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
  80. unsigned long used_hw_queues[BITS_TO_LONGS(IWL_MVM_FIRST_AGG_QUEUE)];
  81. enum iwl_tsf_id preferred_tsf;
  82. bool found_vif;
  83. };
  84. static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
  85. struct ieee80211_vif *vif)
  86. {
  87. struct iwl_mvm_mac_iface_iterator_data *data = _data;
  88. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  89. u32 ac;
  90. /* Iterator may already find the interface being added -- skip it */
  91. if (vif == data->vif) {
  92. data->found_vif = true;
  93. return;
  94. }
  95. /* Mark the queues used by the vif */
  96. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  97. if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
  98. __set_bit(vif->hw_queue[ac], data->used_hw_queues);
  99. if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
  100. __set_bit(vif->cab_queue, data->used_hw_queues);
  101. /*
  102. * Mark MAC IDs as used by clearing the available bit, and
  103. * (below) mark TSFs as used if their existing use is not
  104. * compatible with the new interface type.
  105. * No locking or atomic bit operations are needed since the
  106. * data is on the stack of the caller function.
  107. */
  108. __clear_bit(mvmvif->id, data->available_mac_ids);
  109. /*
  110. * The TSF is a hardware/firmware resource, there are 4 and
  111. * the driver should assign and free them as needed. However,
  112. * there are cases where 2 MACs should share the same TSF ID
  113. * for the purpose of clock sync, an optimization to avoid
  114. * clock drift causing overlapping TBTTs/DTIMs for a GO and
  115. * client in the system.
  116. *
  117. * The firmware will decide according to the MAC type which
  118. * will be the master and slave. Clients that need to sync
  119. * with a remote station will be the master, and an AP or GO
  120. * will be the slave.
  121. *
  122. * Depending on the new interface type it can be slaved to
  123. * or become the master of an existing interface.
  124. */
  125. switch (data->vif->type) {
  126. case NL80211_IFTYPE_STATION:
  127. /*
  128. * The new interface is client, so if the existing one
  129. * we're iterating is an AP, the TSF should be used to
  130. * avoid drift between the new client and existing AP,
  131. * the existing AP will get drift updates from the new
  132. * client context in this case
  133. */
  134. if (vif->type == NL80211_IFTYPE_AP) {
  135. if (data->preferred_tsf == NUM_TSF_IDS &&
  136. test_bit(mvmvif->tsf_id, data->available_tsf_ids))
  137. data->preferred_tsf = mvmvif->tsf_id;
  138. return;
  139. }
  140. break;
  141. case NL80211_IFTYPE_AP:
  142. /*
  143. * The new interface is AP/GO, so should get drift
  144. * updates from an existing client or use the same
  145. * TSF as an existing GO. There's no drift between
  146. * TSFs internally but if they used different TSFs
  147. * then a new client MAC could update one of them
  148. * and cause drift that way.
  149. */
  150. if (vif->type == NL80211_IFTYPE_STATION ||
  151. vif->type == NL80211_IFTYPE_AP) {
  152. if (data->preferred_tsf == NUM_TSF_IDS &&
  153. test_bit(mvmvif->tsf_id, data->available_tsf_ids))
  154. data->preferred_tsf = mvmvif->tsf_id;
  155. return;
  156. }
  157. break;
  158. default:
  159. /*
  160. * For all other interface types there's no need to
  161. * take drift into account. Either they're exclusive
  162. * like IBSS and monitor, or we don't care much about
  163. * their TSF (like P2P Device), but we won't be able
  164. * to share the TSF resource.
  165. */
  166. break;
  167. }
  168. /*
  169. * Unless we exited above, we can't share the TSF resource
  170. * that the virtual interface we're iterating over is using
  171. * with the new one, so clear the available bit and if this
  172. * was the preferred one, reset that as well.
  173. */
  174. __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
  175. if (data->preferred_tsf == mvmvif->tsf_id)
  176. data->preferred_tsf = NUM_TSF_IDS;
  177. }
  178. /*
  179. * Get the mask of the queus used by the vif
  180. */
  181. u32 iwl_mvm_mac_get_queues_mask(struct iwl_mvm *mvm,
  182. struct ieee80211_vif *vif)
  183. {
  184. u32 qmask = 0, ac;
  185. if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
  186. return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
  187. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  188. if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
  189. qmask |= BIT(vif->hw_queue[ac]);
  190. return qmask;
  191. }
  192. static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
  193. struct ieee80211_vif *vif)
  194. {
  195. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  196. struct iwl_mvm_mac_iface_iterator_data data = {
  197. .mvm = mvm,
  198. .vif = vif,
  199. .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
  200. .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
  201. /* no preference yet */
  202. .preferred_tsf = NUM_TSF_IDS,
  203. .used_hw_queues = {
  204. BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
  205. BIT(IWL_MVM_AUX_QUEUE) |
  206. BIT(IWL_MVM_CMD_QUEUE)
  207. },
  208. .found_vif = false,
  209. };
  210. u32 ac;
  211. int ret, i;
  212. /*
  213. * Allocate a MAC ID and a TSF for this MAC, along with the queues
  214. * and other resources.
  215. */
  216. /*
  217. * Before the iterator, we start with all MAC IDs and TSFs available.
  218. *
  219. * During iteration, all MAC IDs are cleared that are in use by other
  220. * virtual interfaces, and all TSF IDs are cleared that can't be used
  221. * by this new virtual interface because they're used by an interface
  222. * that can't share it with the new one.
  223. * At the same time, we check if there's a preferred TSF in the case
  224. * that we should share it with another interface.
  225. */
  226. /* Currently, MAC ID 0 should be used only for the managed vif */
  227. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  228. __clear_bit(0, data.available_mac_ids);
  229. ieee80211_iterate_active_interfaces_atomic(
  230. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  231. iwl_mvm_mac_iface_iterator, &data);
  232. /*
  233. * In the case we're getting here during resume, it's similar to
  234. * firmware restart, and with RESUME_ALL the iterator will find
  235. * the vif being added already.
  236. * We don't want to reassign any IDs in either case since doing
  237. * so would probably assign different IDs (as interfaces aren't
  238. * necessarily added in the same order), but the old IDs were
  239. * preserved anyway, so skip ID assignment for both resume and
  240. * recovery.
  241. */
  242. if (data.found_vif)
  243. return 0;
  244. /* Therefore, in recovery, we can't get here */
  245. WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
  246. mvmvif->id = find_first_bit(data.available_mac_ids,
  247. NUM_MAC_INDEX_DRIVER);
  248. if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
  249. IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
  250. ret = -EIO;
  251. goto exit_fail;
  252. }
  253. if (data.preferred_tsf != NUM_TSF_IDS)
  254. mvmvif->tsf_id = data.preferred_tsf;
  255. else
  256. mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
  257. NUM_TSF_IDS);
  258. if (mvmvif->tsf_id == NUM_TSF_IDS) {
  259. IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
  260. ret = -EIO;
  261. goto exit_fail;
  262. }
  263. mvmvif->color = 0;
  264. INIT_LIST_HEAD(&mvmvif->time_event_data.list);
  265. mvmvif->time_event_data.id = TE_MAX;
  266. /* No need to allocate data queues to P2P Device MAC.*/
  267. if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
  268. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  269. vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
  270. return 0;
  271. }
  272. /* Find available queues, and allocate them to the ACs */
  273. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  274. u8 queue = find_first_zero_bit(data.used_hw_queues,
  275. IWL_MVM_FIRST_AGG_QUEUE);
  276. if (queue >= IWL_MVM_FIRST_AGG_QUEUE) {
  277. IWL_ERR(mvm, "Failed to allocate queue\n");
  278. ret = -EIO;
  279. goto exit_fail;
  280. }
  281. __set_bit(queue, data.used_hw_queues);
  282. vif->hw_queue[ac] = queue;
  283. }
  284. /* Allocate the CAB queue for softAP and GO interfaces */
  285. if (vif->type == NL80211_IFTYPE_AP) {
  286. u8 queue = find_first_zero_bit(data.used_hw_queues,
  287. IWL_MVM_FIRST_AGG_QUEUE);
  288. if (queue >= IWL_MVM_FIRST_AGG_QUEUE) {
  289. IWL_ERR(mvm, "Failed to allocate cab queue\n");
  290. ret = -EIO;
  291. goto exit_fail;
  292. }
  293. vif->cab_queue = queue;
  294. } else {
  295. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  296. }
  297. mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
  298. mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
  299. for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
  300. mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
  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_MCAST);
  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. /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
  490. if (vif->type == NL80211_IFTYPE_AP)
  491. cmd->ac[AC_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
  492. if (vif->bss_conf.qos)
  493. cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
  494. /* Don't use cts to self as the fw doesn't support it currently. */
  495. if (vif->bss_conf.use_cts_prot)
  496. cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
  497. /*
  498. * I think that we should enable these 2 flags regardless the HT PROT
  499. * fields in the HT IE, but I am not sure. Someone knows whom to ask?...
  500. */
  501. if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
  502. cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
  503. cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_HT_PROT |
  504. MAC_PROT_FLG_FAT_PROT);
  505. }
  506. cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
  507. }
  508. static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
  509. struct iwl_mac_ctx_cmd *cmd)
  510. {
  511. int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
  512. sizeof(*cmd), cmd);
  513. if (ret)
  514. IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
  515. le32_to_cpu(cmd->action), ret);
  516. return ret;
  517. }
  518. /*
  519. * Fill the specific data for mac context of type station or p2p client
  520. */
  521. static void iwl_mvm_mac_ctxt_cmd_fill_sta(struct iwl_mvm *mvm,
  522. struct ieee80211_vif *vif,
  523. struct iwl_mac_data_sta *ctxt_sta,
  524. bool force_assoc_off)
  525. {
  526. /* We need the dtim_period to set the MAC as associated */
  527. if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
  528. !force_assoc_off) {
  529. u32 dtim_offs;
  530. /*
  531. * The DTIM count counts down, so when it is N that means N
  532. * more beacon intervals happen until the DTIM TBTT. Therefore
  533. * add this to the current time. If that ends up being in the
  534. * future, the firmware will handle it.
  535. *
  536. * Also note that the system_timestamp (which we get here as
  537. * "sync_device_ts") and TSF timestamp aren't at exactly the
  538. * same offset in the frame -- the TSF is at the first symbol
  539. * of the TSF, the system timestamp is at signal acquisition
  540. * time. This means there's an offset between them of at most
  541. * a few hundred microseconds (24 * 8 bits + PLCP time gives
  542. * 384us in the longest case), this is currently not relevant
  543. * as the firmware wakes up around 2ms before the TBTT.
  544. */
  545. dtim_offs = vif->bss_conf.sync_dtim_count *
  546. vif->bss_conf.beacon_int;
  547. /* convert TU to usecs */
  548. dtim_offs *= 1024;
  549. ctxt_sta->dtim_tsf =
  550. cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
  551. ctxt_sta->dtim_time =
  552. cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
  553. IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
  554. le64_to_cpu(ctxt_sta->dtim_tsf),
  555. le32_to_cpu(ctxt_sta->dtim_time),
  556. dtim_offs);
  557. ctxt_sta->is_assoc = cpu_to_le32(1);
  558. } else {
  559. ctxt_sta->is_assoc = cpu_to_le32(0);
  560. }
  561. ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
  562. ctxt_sta->bi_reciprocal =
  563. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  564. ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
  565. vif->bss_conf.dtim_period);
  566. ctxt_sta->dtim_reciprocal =
  567. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
  568. vif->bss_conf.dtim_period));
  569. ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
  570. ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
  571. }
  572. static int iwl_mvm_mac_ctxt_cmd_station(struct iwl_mvm *mvm,
  573. struct ieee80211_vif *vif,
  574. u32 action)
  575. {
  576. struct iwl_mac_ctx_cmd cmd = {};
  577. WARN_ON(vif->type != NL80211_IFTYPE_STATION || vif->p2p);
  578. /* Fill the common data for all mac context types */
  579. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  580. /* Allow beacons to pass through as long as we are not associated,or we
  581. * do not have dtim period information */
  582. if (!vif->bss_conf.assoc || !vif->bss_conf.dtim_period)
  583. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
  584. else
  585. cmd.filter_flags &= ~cpu_to_le32(MAC_FILTER_IN_BEACON);
  586. /* Fill the data specific for station mode */
  587. iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.sta,
  588. action == FW_CTXT_ACTION_ADD);
  589. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  590. }
  591. static int iwl_mvm_mac_ctxt_cmd_p2p_client(struct iwl_mvm *mvm,
  592. struct ieee80211_vif *vif,
  593. u32 action)
  594. {
  595. struct iwl_mac_ctx_cmd cmd = {};
  596. struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
  597. WARN_ON(vif->type != NL80211_IFTYPE_STATION || !vif->p2p);
  598. /* Fill the common data for all mac context types */
  599. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  600. /* Fill the data specific for station mode */
  601. iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.p2p_sta.sta,
  602. action == FW_CTXT_ACTION_ADD);
  603. cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
  604. IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
  605. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  606. }
  607. static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
  608. struct ieee80211_vif *vif,
  609. u32 action)
  610. {
  611. struct iwl_mac_ctx_cmd cmd = {};
  612. WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
  613. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  614. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
  615. MAC_FILTER_IN_CONTROL_AND_MGMT |
  616. MAC_FILTER_IN_BEACON |
  617. MAC_FILTER_IN_PROBE_REQUEST);
  618. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  619. }
  620. struct iwl_mvm_go_iterator_data {
  621. bool go_active;
  622. };
  623. static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
  624. {
  625. struct iwl_mvm_go_iterator_data *data = _data;
  626. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  627. if (vif->type == NL80211_IFTYPE_AP && vif->p2p && mvmvif->ap_active)
  628. data->go_active = true;
  629. }
  630. static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
  631. struct ieee80211_vif *vif,
  632. u32 action)
  633. {
  634. struct iwl_mac_ctx_cmd cmd = {};
  635. struct iwl_mvm_go_iterator_data data = {};
  636. WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
  637. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  638. cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
  639. /* Override the filter flags to accept only probe requests */
  640. cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  641. /*
  642. * This flag should be set to true when the P2P Device is
  643. * discoverable and there is at least another active P2P GO. Settings
  644. * this flag will allow the P2P Device to be discoverable on other
  645. * channels in addition to its listen channel.
  646. * Note that this flag should not be set in other cases as it opens the
  647. * Rx filters on all MAC and increases the number of interrupts.
  648. */
  649. ieee80211_iterate_active_interfaces_atomic(
  650. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  651. iwl_mvm_go_iterator, &data);
  652. cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
  653. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  654. }
  655. static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
  656. struct iwl_mac_beacon_cmd *beacon_cmd,
  657. u8 *beacon, u32 frame_size)
  658. {
  659. u32 tim_idx;
  660. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  661. /* The index is relative to frame start but we start looking at the
  662. * variable-length part of the beacon. */
  663. tim_idx = mgmt->u.beacon.variable - beacon;
  664. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  665. while ((tim_idx < (frame_size - 2)) &&
  666. (beacon[tim_idx] != WLAN_EID_TIM))
  667. tim_idx += beacon[tim_idx+1] + 2;
  668. /* If TIM field was found, set variables */
  669. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  670. beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
  671. beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
  672. } else {
  673. IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
  674. }
  675. }
  676. static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
  677. struct ieee80211_vif *vif,
  678. struct sk_buff *beacon)
  679. {
  680. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  681. struct iwl_host_cmd cmd = {
  682. .id = BEACON_TEMPLATE_CMD,
  683. .flags = CMD_ASYNC,
  684. };
  685. struct iwl_mac_beacon_cmd beacon_cmd = {};
  686. struct ieee80211_tx_info *info;
  687. u32 beacon_skb_len;
  688. u32 rate;
  689. if (WARN_ON(!beacon))
  690. return -EINVAL;
  691. beacon_skb_len = beacon->len;
  692. /* TODO: for now the beacon template id is set to be the mac context id.
  693. * Might be better to handle it as another resource ... */
  694. beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
  695. /* Set up TX command fields */
  696. beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
  697. beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
  698. beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
  699. beacon_cmd.tx.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
  700. TX_CMD_FLG_BT_DIS |
  701. TX_CMD_FLG_TSF);
  702. mvm->mgmt_last_antenna_idx =
  703. iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
  704. mvm->mgmt_last_antenna_idx);
  705. beacon_cmd.tx.rate_n_flags =
  706. cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
  707. RATE_MCS_ANT_POS);
  708. info = IEEE80211_SKB_CB(beacon);
  709. if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
  710. rate = IWL_FIRST_OFDM_RATE;
  711. } else {
  712. rate = IWL_FIRST_CCK_RATE;
  713. beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
  714. }
  715. beacon_cmd.tx.rate_n_flags |=
  716. cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
  717. /* Set up TX beacon command fields */
  718. iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
  719. beacon->data,
  720. beacon_skb_len);
  721. /* Submit command */
  722. cmd.len[0] = sizeof(beacon_cmd);
  723. cmd.data[0] = &beacon_cmd;
  724. cmd.dataflags[0] = 0;
  725. cmd.len[1] = beacon_skb_len;
  726. cmd.data[1] = beacon->data;
  727. cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
  728. return iwl_mvm_send_cmd(mvm, &cmd);
  729. }
  730. /* The beacon template for the AP/GO context has changed and needs update */
  731. int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
  732. struct ieee80211_vif *vif)
  733. {
  734. struct sk_buff *beacon;
  735. int ret;
  736. WARN_ON(vif->type != NL80211_IFTYPE_AP);
  737. beacon = ieee80211_beacon_get(mvm->hw, vif);
  738. if (!beacon)
  739. return -ENOMEM;
  740. ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
  741. dev_kfree_skb(beacon);
  742. return ret;
  743. }
  744. struct iwl_mvm_mac_ap_iterator_data {
  745. struct iwl_mvm *mvm;
  746. struct ieee80211_vif *vif;
  747. u32 beacon_device_ts;
  748. u16 beacon_int;
  749. };
  750. /* Find the beacon_device_ts and beacon_int for a managed interface */
  751. static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
  752. struct ieee80211_vif *vif)
  753. {
  754. struct iwl_mvm_mac_ap_iterator_data *data = _data;
  755. if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
  756. return;
  757. /* Station client has higher priority over P2P client*/
  758. if (vif->p2p && data->beacon_device_ts)
  759. return;
  760. data->beacon_device_ts = vif->bss_conf.sync_device_ts;
  761. data->beacon_int = vif->bss_conf.beacon_int;
  762. }
  763. /*
  764. * Fill the specific data for mac context of type AP of P2P GO
  765. */
  766. static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
  767. struct ieee80211_vif *vif,
  768. struct iwl_mac_data_ap *ctxt_ap,
  769. bool add)
  770. {
  771. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  772. struct iwl_mvm_mac_ap_iterator_data data = {
  773. .mvm = mvm,
  774. .vif = vif,
  775. .beacon_device_ts = 0
  776. };
  777. ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
  778. ctxt_ap->bi_reciprocal =
  779. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
  780. ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
  781. vif->bss_conf.dtim_period);
  782. ctxt_ap->dtim_reciprocal =
  783. cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
  784. vif->bss_conf.dtim_period));
  785. ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
  786. /*
  787. * Only set the beacon time when the MAC is being added, when we
  788. * just modify the MAC then we should keep the time -- the firmware
  789. * can otherwise have a "jumping" TBTT.
  790. */
  791. if (add) {
  792. /*
  793. * If there is a station/P2P client interface which is
  794. * associated, set the AP's TBTT far enough from the station's
  795. * TBTT. Otherwise, set it to the current system time
  796. */
  797. ieee80211_iterate_active_interfaces_atomic(
  798. mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  799. iwl_mvm_mac_ap_iterator, &data);
  800. if (data.beacon_device_ts) {
  801. u32 rand = (prandom_u32() % (80 - 20)) + 20;
  802. mvmvif->ap_beacon_time = data.beacon_device_ts +
  803. ieee80211_tu_to_usec(data.beacon_int * rand /
  804. 100);
  805. } else {
  806. mvmvif->ap_beacon_time =
  807. iwl_read_prph(mvm->trans,
  808. DEVICE_SYSTEM_TIME_REG);
  809. }
  810. }
  811. ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
  812. ctxt_ap->beacon_tsf = 0; /* unused */
  813. /* TODO: Assume that the beacon id == mac context id */
  814. ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
  815. }
  816. static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
  817. struct ieee80211_vif *vif,
  818. u32 action)
  819. {
  820. struct iwl_mac_ctx_cmd cmd = {};
  821. WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
  822. /* Fill the common data for all mac context types */
  823. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  824. /* Also enable probe requests to pass */
  825. cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
  826. /* Fill the data specific for ap mode */
  827. iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
  828. action == FW_CTXT_ACTION_ADD);
  829. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  830. }
  831. static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
  832. struct ieee80211_vif *vif,
  833. u32 action)
  834. {
  835. struct iwl_mac_ctx_cmd cmd = {};
  836. struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
  837. WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
  838. /* Fill the common data for all mac context types */
  839. iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
  840. /* Fill the data specific for GO mode */
  841. iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
  842. action == FW_CTXT_ACTION_ADD);
  843. cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
  844. IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
  845. cmd.go.opp_ps_enabled =
  846. cpu_to_le32(!!(noa->oppps_ctwindow &
  847. IEEE80211_P2P_OPPPS_ENABLE_BIT));
  848. return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
  849. }
  850. static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  851. u32 action)
  852. {
  853. switch (vif->type) {
  854. case NL80211_IFTYPE_STATION:
  855. if (!vif->p2p)
  856. return iwl_mvm_mac_ctxt_cmd_station(mvm, vif,
  857. action);
  858. else
  859. return iwl_mvm_mac_ctxt_cmd_p2p_client(mvm, vif,
  860. action);
  861. break;
  862. case NL80211_IFTYPE_AP:
  863. if (!vif->p2p)
  864. return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
  865. else
  866. return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
  867. break;
  868. case NL80211_IFTYPE_MONITOR:
  869. return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
  870. case NL80211_IFTYPE_P2P_DEVICE:
  871. return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
  872. default:
  873. break;
  874. }
  875. return -EOPNOTSUPP;
  876. }
  877. int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  878. {
  879. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  880. int ret;
  881. if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
  882. vif->addr, ieee80211_vif_type_p2p(vif)))
  883. return -EIO;
  884. ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD);
  885. if (ret)
  886. return ret;
  887. mvmvif->uploaded = true;
  888. return 0;
  889. }
  890. int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  891. {
  892. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  893. if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
  894. vif->addr, ieee80211_vif_type_p2p(vif)))
  895. return -EIO;
  896. return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY);
  897. }
  898. int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  899. {
  900. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  901. struct iwl_mac_ctx_cmd cmd;
  902. int ret;
  903. if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
  904. vif->addr, ieee80211_vif_type_p2p(vif)))
  905. return -EIO;
  906. memset(&cmd, 0, sizeof(cmd));
  907. cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  908. mvmvif->color));
  909. cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
  910. ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
  911. sizeof(cmd), &cmd);
  912. if (ret) {
  913. IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
  914. return ret;
  915. }
  916. mvmvif->uploaded = false;
  917. return 0;
  918. }
  919. int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
  920. struct iwl_rx_cmd_buffer *rxb,
  921. struct iwl_device_cmd *cmd)
  922. {
  923. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  924. struct iwl_beacon_notif *beacon = (void *)pkt->data;
  925. u16 status __maybe_unused =
  926. le16_to_cpu(beacon->beacon_notify_hdr.status.status);
  927. u32 rate __maybe_unused =
  928. le32_to_cpu(beacon->beacon_notify_hdr.initial_rate);
  929. IWL_DEBUG_RX(mvm, "beacon status %#x retries:%d tsf:0x%16llX rate:%d\n",
  930. status & TX_STATUS_MSK,
  931. beacon->beacon_notify_hdr.failure_frame,
  932. le64_to_cpu(beacon->tsf),
  933. rate);
  934. return 0;
  935. }
  936. static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
  937. struct ieee80211_vif *vif)
  938. {
  939. u16 *id = _data;
  940. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  941. if (mvmvif->id == *id)
  942. ieee80211_beacon_loss(vif);
  943. }
  944. int iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
  945. struct iwl_rx_cmd_buffer *rxb,
  946. struct iwl_device_cmd *cmd)
  947. {
  948. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  949. struct iwl_missed_beacons_notif *missed_beacons = (void *)pkt->data;
  950. u16 id = (u16)le32_to_cpu(missed_beacons->mac_id);
  951. ieee80211_iterate_active_interfaces_atomic(mvm->hw,
  952. IEEE80211_IFACE_ITER_NORMAL,
  953. iwl_mvm_beacon_loss_iterator,
  954. &id);
  955. return 0;
  956. }