mac-ctxt.c 42 KB

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