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