iwlwifi: remove CMD_SYNC
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / iwlwifi / mvm / time-event.c
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63
64 #include <linux/jiffies.h>
65 #include <net/mac80211.h>
66
67 #include "iwl-notif-wait.h"
68 #include "iwl-trans.h"
69 #include "fw-api.h"
70 #include "time-event.h"
71 #include "mvm.h"
72 #include "iwl-io.h"
73 #include "iwl-prph.h"
74
75 /* A TimeUnit is 1024 microsecond */
76 #define MSEC_TO_TU(_msec)       (_msec*1000/1024)
77
78 /*
79  * For the high priority TE use a time event type that has similar priority to
80  * the FW's action scan priority.
81  */
82 #define IWL_MVM_ROC_TE_TYPE_NORMAL TE_P2P_DEVICE_DISCOVERABLE
83 #define IWL_MVM_ROC_TE_TYPE_MGMT_TX TE_P2P_CLIENT_ASSOC
84
85 void iwl_mvm_te_clear_data(struct iwl_mvm *mvm,
86                            struct iwl_mvm_time_event_data *te_data)
87 {
88         lockdep_assert_held(&mvm->time_event_lock);
89
90         if (te_data->id == TE_MAX)
91                 return;
92
93         list_del(&te_data->list);
94         te_data->running = false;
95         te_data->uid = 0;
96         te_data->id = TE_MAX;
97         te_data->vif = NULL;
98 }
99
100 void iwl_mvm_roc_done_wk(struct work_struct *wk)
101 {
102         struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, roc_done_wk);
103
104         synchronize_net();
105
106         /*
107          * Flush the offchannel queue -- this is called when the time
108          * event finishes or is cancelled, so that frames queued for it
109          * won't get stuck on the queue and be transmitted in the next
110          * time event.
111          * We have to send the command asynchronously since this cannot
112          * be under the mutex for locking reasons, but that's not an
113          * issue as it will have to complete before the next command is
114          * executed, and a new time event means a new command.
115          */
116         iwl_mvm_flush_tx_path(mvm, BIT(IWL_MVM_OFFCHANNEL_QUEUE), false);
117 }
118
119 static void iwl_mvm_roc_finished(struct iwl_mvm *mvm)
120 {
121         /*
122          * First, clear the ROC_RUNNING status bit. This will cause the TX
123          * path to drop offchannel transmissions. That would also be done
124          * by mac80211, but it is racy, in particular in the case that the
125          * time event actually completed in the firmware (which is handled
126          * in iwl_mvm_te_handle_notif).
127          */
128         clear_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
129         iwl_mvm_unref(mvm, IWL_MVM_REF_ROC);
130
131         /*
132          * Of course, our status bit is just as racy as mac80211, so in
133          * addition, fire off the work struct which will drop all frames
134          * from the hardware queues that made it through the race. First
135          * it will of course synchronize the TX path to make sure that
136          * any *new* TX will be rejected.
137          */
138         schedule_work(&mvm->roc_done_wk);
139 }
140
141 static bool iwl_mvm_te_check_disconnect(struct iwl_mvm *mvm,
142                                         struct ieee80211_vif *vif,
143                                         const char *errmsg)
144 {
145         if (vif->type != NL80211_IFTYPE_STATION)
146                 return false;
147         if (vif->bss_conf.assoc && vif->bss_conf.dtim_period)
148                 return false;
149         if (errmsg)
150                 IWL_ERR(mvm, "%s\n", errmsg);
151         ieee80211_connection_loss(vif);
152         return true;
153 }
154
155 /*
156  * Handles a FW notification for an event that is known to the driver.
157  *
158  * @mvm: the mvm component
159  * @te_data: the time event data
160  * @notif: the notification data corresponding the time event data.
161  */
162 static void iwl_mvm_te_handle_notif(struct iwl_mvm *mvm,
163                                     struct iwl_mvm_time_event_data *te_data,
164                                     struct iwl_time_event_notif *notif)
165 {
166         lockdep_assert_held(&mvm->time_event_lock);
167
168         IWL_DEBUG_TE(mvm, "Handle time event notif - UID = 0x%x action %d\n",
169                      le32_to_cpu(notif->unique_id),
170                      le32_to_cpu(notif->action));
171
172         /*
173          * The FW sends the start/end time event notifications even for events
174          * that it fails to schedule. This is indicated in the status field of
175          * the notification. This happens in cases that the scheduler cannot
176          * find a schedule that can handle the event (for example requesting a
177          * P2P Device discoveribility, while there are other higher priority
178          * events in the system).
179          */
180         if (!le32_to_cpu(notif->status)) {
181                 bool start = le32_to_cpu(notif->action) &
182                                 TE_V2_NOTIF_HOST_EVENT_START;
183                 IWL_WARN(mvm, "Time Event %s notification failure\n",
184                          start ? "start" : "end");
185                 if (iwl_mvm_te_check_disconnect(mvm, te_data->vif, NULL)) {
186                         iwl_mvm_te_clear_data(mvm, te_data);
187                         return;
188                 }
189         }
190
191         if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_END) {
192                 IWL_DEBUG_TE(mvm,
193                              "TE ended - current time %lu, estimated end %lu\n",
194                              jiffies, te_data->end_jiffies);
195
196                 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
197                         ieee80211_remain_on_channel_expired(mvm->hw);
198                         iwl_mvm_roc_finished(mvm);
199                 }
200
201                 /*
202                  * By now, we should have finished association
203                  * and know the dtim period.
204                  */
205                 iwl_mvm_te_check_disconnect(mvm, te_data->vif,
206                         "No association and the time event is over already...");
207                 iwl_mvm_te_clear_data(mvm, te_data);
208         } else if (le32_to_cpu(notif->action) & TE_V2_NOTIF_HOST_EVENT_START) {
209                 te_data->running = true;
210                 te_data->end_jiffies = TU_TO_EXP_TIME(te_data->duration);
211
212                 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
213                         set_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status);
214                         iwl_mvm_ref(mvm, IWL_MVM_REF_ROC);
215                         ieee80211_ready_on_channel(mvm->hw);
216                 }
217         } else {
218                 IWL_WARN(mvm, "Got TE with unknown action\n");
219         }
220 }
221
222 /*
223  * The Rx handler for time event notifications
224  */
225 int iwl_mvm_rx_time_event_notif(struct iwl_mvm *mvm,
226                                 struct iwl_rx_cmd_buffer *rxb,
227                                 struct iwl_device_cmd *cmd)
228 {
229         struct iwl_rx_packet *pkt = rxb_addr(rxb);
230         struct iwl_time_event_notif *notif = (void *)pkt->data;
231         struct iwl_mvm_time_event_data *te_data, *tmp;
232
233         IWL_DEBUG_TE(mvm, "Time event notification - UID = 0x%x action %d\n",
234                      le32_to_cpu(notif->unique_id),
235                      le32_to_cpu(notif->action));
236
237         spin_lock_bh(&mvm->time_event_lock);
238         list_for_each_entry_safe(te_data, tmp, &mvm->time_event_list, list) {
239                 if (le32_to_cpu(notif->unique_id) == te_data->uid)
240                         iwl_mvm_te_handle_notif(mvm, te_data, notif);
241         }
242         spin_unlock_bh(&mvm->time_event_lock);
243
244         return 0;
245 }
246
247 static bool iwl_mvm_time_event_response(struct iwl_notif_wait_data *notif_wait,
248                                         struct iwl_rx_packet *pkt, void *data)
249 {
250         struct iwl_mvm *mvm =
251                 container_of(notif_wait, struct iwl_mvm, notif_wait);
252         struct iwl_mvm_time_event_data *te_data = data;
253         struct iwl_time_event_resp *resp;
254         int resp_len = iwl_rx_packet_payload_len(pkt);
255
256         if (WARN_ON(pkt->hdr.cmd != TIME_EVENT_CMD))
257                 return true;
258
259         if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
260                 IWL_ERR(mvm, "Invalid TIME_EVENT_CMD response\n");
261                 return true;
262         }
263
264         resp = (void *)pkt->data;
265
266         /* we should never get a response to another TIME_EVENT_CMD here */
267         if (WARN_ON_ONCE(le32_to_cpu(resp->id) != te_data->id))
268                 return false;
269
270         te_data->uid = le32_to_cpu(resp->unique_id);
271         IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
272                      te_data->uid);
273         return true;
274 }
275
276 static int iwl_mvm_time_event_send_add(struct iwl_mvm *mvm,
277                                        struct ieee80211_vif *vif,
278                                        struct iwl_mvm_time_event_data *te_data,
279                                        struct iwl_time_event_cmd *te_cmd)
280 {
281         static const u8 time_event_response[] = { TIME_EVENT_CMD };
282         struct iwl_notification_wait wait_time_event;
283         int ret;
284
285         lockdep_assert_held(&mvm->mutex);
286
287         IWL_DEBUG_TE(mvm, "Add new TE, duration %d TU\n",
288                      le32_to_cpu(te_cmd->duration));
289
290         spin_lock_bh(&mvm->time_event_lock);
291         if (WARN_ON(te_data->id != TE_MAX)) {
292                 spin_unlock_bh(&mvm->time_event_lock);
293                 return -EIO;
294         }
295         te_data->vif = vif;
296         te_data->duration = le32_to_cpu(te_cmd->duration);
297         te_data->id = le32_to_cpu(te_cmd->id);
298         list_add_tail(&te_data->list, &mvm->time_event_list);
299         spin_unlock_bh(&mvm->time_event_lock);
300
301         /*
302          * Use a notification wait, which really just processes the
303          * command response and doesn't wait for anything, in order
304          * to be able to process the response and get the UID inside
305          * the RX path. Using CMD_WANT_SKB doesn't work because it
306          * stores the buffer and then wakes up this thread, by which
307          * time another notification (that the time event started)
308          * might already be processed unsuccessfully.
309          */
310         iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
311                                    time_event_response,
312                                    ARRAY_SIZE(time_event_response),
313                                    iwl_mvm_time_event_response, te_data);
314
315         ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
316                                             sizeof(*te_cmd), te_cmd);
317         if (ret) {
318                 IWL_ERR(mvm, "Couldn't send TIME_EVENT_CMD: %d\n", ret);
319                 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
320                 goto out_clear_te;
321         }
322
323         /* No need to wait for anything, so just pass 1 (0 isn't valid) */
324         ret = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
325         /* should never fail */
326         WARN_ON_ONCE(ret);
327
328         if (ret) {
329  out_clear_te:
330                 spin_lock_bh(&mvm->time_event_lock);
331                 iwl_mvm_te_clear_data(mvm, te_data);
332                 spin_unlock_bh(&mvm->time_event_lock);
333         }
334         return ret;
335 }
336
337 void iwl_mvm_protect_session(struct iwl_mvm *mvm,
338                              struct ieee80211_vif *vif,
339                              u32 duration, u32 min_duration,
340                              u32 max_delay)
341 {
342         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
343         struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
344         struct iwl_time_event_cmd time_cmd = {};
345
346         lockdep_assert_held(&mvm->mutex);
347
348         if (te_data->running &&
349             time_after(te_data->end_jiffies, TU_TO_EXP_TIME(min_duration))) {
350                 IWL_DEBUG_TE(mvm, "We have enough time in the current TE: %u\n",
351                              jiffies_to_msecs(te_data->end_jiffies - jiffies));
352                 return;
353         }
354
355         if (te_data->running) {
356                 IWL_DEBUG_TE(mvm, "extend 0x%x: only %u ms left\n",
357                              te_data->uid,
358                              jiffies_to_msecs(te_data->end_jiffies - jiffies));
359                 /*
360                  * we don't have enough time
361                  * cancel the current TE and issue a new one
362                  * Of course it would be better to remove the old one only
363                  * when the new one is added, but we don't care if we are off
364                  * channel for a bit. All we need to do, is not to return
365                  * before we actually begin to be on the channel.
366                  */
367                 iwl_mvm_stop_session_protection(mvm, vif);
368         }
369
370         time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
371         time_cmd.id_and_color =
372                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
373         time_cmd.id = cpu_to_le32(TE_BSS_STA_AGGRESSIVE_ASSOC);
374
375         time_cmd.apply_time =
376                 cpu_to_le32(iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG));
377
378         time_cmd.max_frags = TE_V2_FRAG_NONE;
379         time_cmd.max_delay = cpu_to_le32(max_delay);
380         /* TODO: why do we need to interval = bi if it is not periodic? */
381         time_cmd.interval = cpu_to_le32(1);
382         time_cmd.duration = cpu_to_le32(duration);
383         time_cmd.repeat = 1;
384         time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
385                                       TE_V2_NOTIF_HOST_EVENT_END |
386                                       T2_V2_START_IMMEDIATELY);
387
388         iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
389 }
390
391 /*
392  * Explicit request to remove a time event. The removal of a time event needs to
393  * be synchronized with the flow of a time event's end notification, which also
394  * removes the time event from the op mode data structures.
395  */
396 void iwl_mvm_remove_time_event(struct iwl_mvm *mvm,
397                                struct iwl_mvm_vif *mvmvif,
398                                struct iwl_mvm_time_event_data *te_data)
399 {
400         struct iwl_time_event_cmd time_cmd = {};
401         u32 id, uid;
402         int ret;
403
404         /*
405          * It is possible that by the time we got to this point the time
406          * event was already removed.
407          */
408         spin_lock_bh(&mvm->time_event_lock);
409
410         /* Save time event uid before clearing its data */
411         uid = te_data->uid;
412         id = te_data->id;
413
414         /*
415          * The clear_data function handles time events that were already removed
416          */
417         iwl_mvm_te_clear_data(mvm, te_data);
418         spin_unlock_bh(&mvm->time_event_lock);
419
420         /*
421          * It is possible that by the time we try to remove it, the time event
422          * has already ended and removed. In such a case there is no need to
423          * send a removal command.
424          */
425         if (id == TE_MAX) {
426                 IWL_DEBUG_TE(mvm, "TE 0x%x has already ended\n", uid);
427                 return;
428         }
429
430         /* When we remove a TE, the UID is to be set in the id field */
431         time_cmd.id = cpu_to_le32(uid);
432         time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
433         time_cmd.id_and_color =
434                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
435
436         IWL_DEBUG_TE(mvm, "Removing TE 0x%x\n", le32_to_cpu(time_cmd.id));
437         ret = iwl_mvm_send_cmd_pdu(mvm, TIME_EVENT_CMD, 0,
438                                    sizeof(time_cmd), &time_cmd);
439         if (WARN_ON(ret))
440                 return;
441 }
442
443 void iwl_mvm_stop_session_protection(struct iwl_mvm *mvm,
444                                      struct ieee80211_vif *vif)
445 {
446         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
447         struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
448
449         lockdep_assert_held(&mvm->mutex);
450         iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
451 }
452
453 int iwl_mvm_start_p2p_roc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
454                           int duration, enum ieee80211_roc_type type)
455 {
456         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
457         struct iwl_mvm_time_event_data *te_data = &mvmvif->time_event_data;
458         struct iwl_time_event_cmd time_cmd = {};
459
460         lockdep_assert_held(&mvm->mutex);
461         if (te_data->running) {
462                 IWL_WARN(mvm, "P2P_DEVICE remain on channel already running\n");
463                 return -EBUSY;
464         }
465
466         /*
467          * Flush the done work, just in case it's still pending, so that
468          * the work it does can complete and we can accept new frames.
469          */
470         flush_work(&mvm->roc_done_wk);
471
472         time_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
473         time_cmd.id_and_color =
474                 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
475
476         switch (type) {
477         case IEEE80211_ROC_TYPE_NORMAL:
478                 time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_NORMAL);
479                 break;
480         case IEEE80211_ROC_TYPE_MGMT_TX:
481                 time_cmd.id = cpu_to_le32(IWL_MVM_ROC_TE_TYPE_MGMT_TX);
482                 break;
483         default:
484                 WARN_ONCE(1, "Got an invalid ROC type\n");
485                 return -EINVAL;
486         }
487
488         time_cmd.apply_time = cpu_to_le32(0);
489         time_cmd.interval = cpu_to_le32(1);
490
491         /*
492          * The P2P Device TEs can have lower priority than other events
493          * that are being scheduled by the driver/fw, and thus it might not be
494          * scheduled. To improve the chances of it being scheduled, allow them
495          * to be fragmented, and in addition allow them to be delayed.
496          */
497         time_cmd.max_frags = min(MSEC_TO_TU(duration)/50, TE_V2_FRAG_ENDLESS);
498         time_cmd.max_delay = cpu_to_le32(MSEC_TO_TU(duration/2));
499         time_cmd.duration = cpu_to_le32(MSEC_TO_TU(duration));
500         time_cmd.repeat = 1;
501         time_cmd.policy = cpu_to_le16(TE_V2_NOTIF_HOST_EVENT_START |
502                                       TE_V2_NOTIF_HOST_EVENT_END |
503                                       T2_V2_START_IMMEDIATELY);
504
505         return iwl_mvm_time_event_send_add(mvm, vif, te_data, &time_cmd);
506 }
507
508 void iwl_mvm_stop_p2p_roc(struct iwl_mvm *mvm)
509 {
510         struct iwl_mvm_vif *mvmvif;
511         struct iwl_mvm_time_event_data *te_data;
512
513         lockdep_assert_held(&mvm->mutex);
514
515         /*
516          * Iterate over the list of time events and find the time event that is
517          * associated with a P2P_DEVICE interface.
518          * This assumes that a P2P_DEVICE interface can have only a single time
519          * event at any given time and this time event coresponds to a ROC
520          * request
521          */
522         mvmvif = NULL;
523         spin_lock_bh(&mvm->time_event_lock);
524         list_for_each_entry(te_data, &mvm->time_event_list, list) {
525                 if (te_data->vif->type == NL80211_IFTYPE_P2P_DEVICE) {
526                         mvmvif = iwl_mvm_vif_from_mac80211(te_data->vif);
527                         break;
528                 }
529         }
530         spin_unlock_bh(&mvm->time_event_lock);
531
532         if (!mvmvif) {
533                 IWL_WARN(mvm, "P2P_DEVICE no remain on channel event\n");
534                 return;
535         }
536
537         iwl_mvm_remove_time_event(mvm, mvmvif, te_data);
538
539         iwl_mvm_roc_finished(mvm);
540 }