blob: 8aa1f2b7fdfc5ce16b7fc4c0914be994fb799ef3 [file] [log] [blame]
Johannes Berg4bd14dd2012-03-06 13:30:58 -08001/******************************************************************************
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 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
Sara Sharon6eb031d2015-07-13 14:50:47 +03009 * Copyright(c) 2015 Intel Deutschland GmbH
Johannes Berg4bd14dd2012-03-06 13:30:58 -080010 *
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
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020026 * in the file called COPYING.
Johannes Berg4bd14dd2012-03-06 13:30:58 -080027 *
28 * Contact Information:
Emmanuel Grumbachd01c5362015-11-17 15:39:56 +020029 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg4bd14dd2012-03-06 13:30:58 -080030 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
Johannes Berg4bd14dd2012-03-06 13:30:58 -080035 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 *
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
46 * distribution.
47 * * Neither the name Intel Corporation nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *
63 *****************************************************************************/
64#include <linux/sched.h>
Johannes Bergac91f912012-05-16 22:54:28 +020065#include <linux/export.h>
Johannes Berg4bd14dd2012-03-06 13:30:58 -080066
Johannes Berg48e29342013-03-01 00:13:33 +010067#include "iwl-drv.h"
Johannes Berg4bd14dd2012-03-06 13:30:58 -080068#include "iwl-notif-wait.h"
69
70
71void iwl_notification_wait_init(struct iwl_notif_wait_data *notif_wait)
72{
73 spin_lock_init(&notif_wait->notif_wait_lock);
74 INIT_LIST_HEAD(&notif_wait->notif_waits);
75 init_waitqueue_head(&notif_wait->notif_waitq);
76}
Johannes Berg48e29342013-03-01 00:13:33 +010077IWL_EXPORT_SYMBOL(iwl_notification_wait_init);
Johannes Berg4bd14dd2012-03-06 13:30:58 -080078
79void iwl_notification_wait_notify(struct iwl_notif_wait_data *notif_wait,
80 struct iwl_rx_packet *pkt)
81{
Johannes Bergdb662d42012-03-15 13:26:44 -070082 bool triggered = false;
83
Johannes Berg4bd14dd2012-03-06 13:30:58 -080084 if (!list_empty(&notif_wait->notif_waits)) {
85 struct iwl_notification_wait *w;
86
87 spin_lock(&notif_wait->notif_wait_lock);
88 list_for_each_entry(w, &notif_wait->notif_waits, list) {
Johannes Bergdb662d42012-03-15 13:26:44 -070089 int i;
90 bool found = false;
91
92 /*
93 * If it already finished (triggered) or has been
94 * aborted then don't evaluate it again to avoid races,
95 * Otherwise the function could be called again even
96 * though it returned true before
97 */
98 if (w->triggered || w->aborted)
Johannes Berg4bd14dd2012-03-06 13:30:58 -080099 continue;
Johannes Bergdb662d42012-03-15 13:26:44 -0700100
101 for (i = 0; i < w->n_cmds; i++) {
Sara Sharon6eb031d2015-07-13 14:50:47 +0300102 if (w->cmds[i] ==
103 WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
Johannes Bergdb662d42012-03-15 13:26:44 -0700104 found = true;
105 break;
106 }
107 }
108 if (!found)
109 continue;
110
111 if (!w->fn || w->fn(notif_wait, pkt, w->fn_data)) {
112 w->triggered = true;
113 triggered = true;
114 }
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800115 }
116 spin_unlock(&notif_wait->notif_wait_lock);
117
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800118 }
Johannes Bergdb662d42012-03-15 13:26:44 -0700119
120 if (triggered)
121 wake_up_all(&notif_wait->notif_waitq);
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800122}
Johannes Berg48e29342013-03-01 00:13:33 +0100123IWL_EXPORT_SYMBOL(iwl_notification_wait_notify);
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800124
125void iwl_abort_notification_waits(struct iwl_notif_wait_data *notif_wait)
126{
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800127 struct iwl_notification_wait *wait_entry;
128
Emmanuel Grumbach3595c002012-06-17 16:12:42 +0300129 spin_lock(&notif_wait->notif_wait_lock);
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800130 list_for_each_entry(wait_entry, &notif_wait->notif_waits, list)
131 wait_entry->aborted = true;
Emmanuel Grumbach3595c002012-06-17 16:12:42 +0300132 spin_unlock(&notif_wait->notif_wait_lock);
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800133
134 wake_up_all(&notif_wait->notif_waitq);
135}
Johannes Berg48e29342013-03-01 00:13:33 +0100136IWL_EXPORT_SYMBOL(iwl_abort_notification_waits);
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800137
138void
139iwl_init_notification_wait(struct iwl_notif_wait_data *notif_wait,
140 struct iwl_notification_wait *wait_entry,
Sara Sharon6eb031d2015-07-13 14:50:47 +0300141 const u16 *cmds, int n_cmds,
Johannes Bergdb662d42012-03-15 13:26:44 -0700142 bool (*fn)(struct iwl_notif_wait_data *notif_wait,
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800143 struct iwl_rx_packet *pkt, void *data),
144 void *fn_data)
145{
Johannes Bergdb662d42012-03-15 13:26:44 -0700146 if (WARN_ON(n_cmds > MAX_NOTIF_CMDS))
147 n_cmds = MAX_NOTIF_CMDS;
148
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800149 wait_entry->fn = fn;
150 wait_entry->fn_data = fn_data;
Johannes Bergdb662d42012-03-15 13:26:44 -0700151 wait_entry->n_cmds = n_cmds;
Sara Sharon6eb031d2015-07-13 14:50:47 +0300152 memcpy(wait_entry->cmds, cmds, n_cmds * sizeof(u16));
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800153 wait_entry->triggered = false;
154 wait_entry->aborted = false;
155
156 spin_lock_bh(&notif_wait->notif_wait_lock);
157 list_add(&wait_entry->list, &notif_wait->notif_waits);
158 spin_unlock_bh(&notif_wait->notif_wait_lock);
159}
Johannes Berg48e29342013-03-01 00:13:33 +0100160IWL_EXPORT_SYMBOL(iwl_init_notification_wait);
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800161
162int iwl_wait_notification(struct iwl_notif_wait_data *notif_wait,
163 struct iwl_notification_wait *wait_entry,
164 unsigned long timeout)
165{
166 int ret;
167
168 ret = wait_event_timeout(notif_wait->notif_waitq,
169 wait_entry->triggered || wait_entry->aborted,
170 timeout);
171
172 spin_lock_bh(&notif_wait->notif_wait_lock);
173 list_del(&wait_entry->list);
174 spin_unlock_bh(&notif_wait->notif_wait_lock);
175
176 if (wait_entry->aborted)
177 return -EIO;
178
179 /* return value is always >= 0 */
180 if (ret <= 0)
181 return -ETIMEDOUT;
182 return 0;
183}
Johannes Berg48e29342013-03-01 00:13:33 +0100184IWL_EXPORT_SYMBOL(iwl_wait_notification);
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800185
186void iwl_remove_notification(struct iwl_notif_wait_data *notif_wait,
187 struct iwl_notification_wait *wait_entry)
188{
189 spin_lock_bh(&notif_wait->notif_wait_lock);
190 list_del(&wait_entry->list);
191 spin_unlock_bh(&notif_wait->notif_wait_lock);
192}
Johannes Berg48e29342013-03-01 00:13:33 +0100193IWL_EXPORT_SYMBOL(iwl_remove_notification);