1*4882a593Smuzhiyun /******************************************************************************
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * This file is provided under a dual BSD/GPLv2 license. When using or
4*4882a593Smuzhiyun * redistributing this file, you may do so under either license.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * GPL LICENSE SUMMARY
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
9*4882a593Smuzhiyun * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
12*4882a593Smuzhiyun * it under the terms of version 2 of the GNU General Public License as
13*4882a593Smuzhiyun * published by the Free Software Foundation.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful, but
16*4882a593Smuzhiyun * WITHOUT ANY WARRANTY; without even the implied warranty of
17*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18*4882a593Smuzhiyun * General Public License for more details.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * The full GNU General Public License is included in this distribution
21*4882a593Smuzhiyun * in the file called COPYING.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Contact Information:
24*4882a593Smuzhiyun * Intel Linux Wireless <linuxwifi@intel.com>
25*4882a593Smuzhiyun * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * BSD LICENSE
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
30*4882a593Smuzhiyun * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
31*4882a593Smuzhiyun * All rights reserved.
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
34*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
35*4882a593Smuzhiyun * are met:
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * * Redistributions of source code must retain the above copyright
38*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
39*4882a593Smuzhiyun * * Redistributions in binary form must reproduce the above copyright
40*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in
41*4882a593Smuzhiyun * the documentation and/or other materials provided with the
42*4882a593Smuzhiyun * distribution.
43*4882a593Smuzhiyun * * Neither the name Intel Corporation nor the names of its
44*4882a593Smuzhiyun * contributors may be used to endorse or promote products derived
45*4882a593Smuzhiyun * from this software without specific prior written permission.
46*4882a593Smuzhiyun *
47*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
51*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun *****************************************************************************/
60*4882a593Smuzhiyun #include <linux/sched.h>
61*4882a593Smuzhiyun #include <linux/export.h>
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun #include "iwl-drv.h"
64*4882a593Smuzhiyun #include "notif-wait.h"
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun
iwl_notification_wait_init(struct iwl_notif_wait_data * notif_wait)67*4882a593Smuzhiyun void iwl_notification_wait_init(struct iwl_notif_wait_data *notif_wait)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun spin_lock_init(¬if_wait->notif_wait_lock);
70*4882a593Smuzhiyun INIT_LIST_HEAD(¬if_wait->notif_waits);
71*4882a593Smuzhiyun init_waitqueue_head(¬if_wait->notif_waitq);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun IWL_EXPORT_SYMBOL(iwl_notification_wait_init);
74*4882a593Smuzhiyun
iwl_notification_wait(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt)75*4882a593Smuzhiyun bool iwl_notification_wait(struct iwl_notif_wait_data *notif_wait,
76*4882a593Smuzhiyun struct iwl_rx_packet *pkt)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun bool triggered = false;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun if (!list_empty(¬if_wait->notif_waits)) {
81*4882a593Smuzhiyun struct iwl_notification_wait *w;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun spin_lock(¬if_wait->notif_wait_lock);
84*4882a593Smuzhiyun list_for_each_entry(w, ¬if_wait->notif_waits, list) {
85*4882a593Smuzhiyun int i;
86*4882a593Smuzhiyun bool found = false;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun * If it already finished (triggered) or has been
90*4882a593Smuzhiyun * aborted then don't evaluate it again to avoid races,
91*4882a593Smuzhiyun * Otherwise the function could be called again even
92*4882a593Smuzhiyun * though it returned true before
93*4882a593Smuzhiyun */
94*4882a593Smuzhiyun if (w->triggered || w->aborted)
95*4882a593Smuzhiyun continue;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun for (i = 0; i < w->n_cmds; i++) {
98*4882a593Smuzhiyun u16 rec_id = WIDE_ID(pkt->hdr.group_id,
99*4882a593Smuzhiyun pkt->hdr.cmd);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun if (w->cmds[i] == rec_id ||
102*4882a593Smuzhiyun (!iwl_cmd_groupid(w->cmds[i]) &&
103*4882a593Smuzhiyun DEF_ID(w->cmds[i]) == rec_id)) {
104*4882a593Smuzhiyun found = true;
105*4882a593Smuzhiyun break;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun if (!found)
109*4882a593Smuzhiyun continue;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun if (!w->fn || w->fn(notif_wait, pkt, w->fn_data)) {
112*4882a593Smuzhiyun w->triggered = true;
113*4882a593Smuzhiyun triggered = true;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun spin_unlock(¬if_wait->notif_wait_lock);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun return triggered;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun IWL_EXPORT_SYMBOL(iwl_notification_wait);
122*4882a593Smuzhiyun
iwl_abort_notification_waits(struct iwl_notif_wait_data * notif_wait)123*4882a593Smuzhiyun void iwl_abort_notification_waits(struct iwl_notif_wait_data *notif_wait)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun struct iwl_notification_wait *wait_entry;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun spin_lock(¬if_wait->notif_wait_lock);
128*4882a593Smuzhiyun list_for_each_entry(wait_entry, ¬if_wait->notif_waits, list)
129*4882a593Smuzhiyun wait_entry->aborted = true;
130*4882a593Smuzhiyun spin_unlock(¬if_wait->notif_wait_lock);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun wake_up_all(¬if_wait->notif_waitq);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun IWL_EXPORT_SYMBOL(iwl_abort_notification_waits);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun void
iwl_init_notification_wait(struct iwl_notif_wait_data * notif_wait,struct iwl_notification_wait * wait_entry,const u16 * cmds,int n_cmds,bool (* fn)(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data),void * fn_data)137*4882a593Smuzhiyun iwl_init_notification_wait(struct iwl_notif_wait_data *notif_wait,
138*4882a593Smuzhiyun struct iwl_notification_wait *wait_entry,
139*4882a593Smuzhiyun const u16 *cmds, int n_cmds,
140*4882a593Smuzhiyun bool (*fn)(struct iwl_notif_wait_data *notif_wait,
141*4882a593Smuzhiyun struct iwl_rx_packet *pkt, void *data),
142*4882a593Smuzhiyun void *fn_data)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun if (WARN_ON(n_cmds > MAX_NOTIF_CMDS))
145*4882a593Smuzhiyun n_cmds = MAX_NOTIF_CMDS;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun wait_entry->fn = fn;
148*4882a593Smuzhiyun wait_entry->fn_data = fn_data;
149*4882a593Smuzhiyun wait_entry->n_cmds = n_cmds;
150*4882a593Smuzhiyun memcpy(wait_entry->cmds, cmds, n_cmds * sizeof(u16));
151*4882a593Smuzhiyun wait_entry->triggered = false;
152*4882a593Smuzhiyun wait_entry->aborted = false;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun spin_lock_bh(¬if_wait->notif_wait_lock);
155*4882a593Smuzhiyun list_add(&wait_entry->list, ¬if_wait->notif_waits);
156*4882a593Smuzhiyun spin_unlock_bh(¬if_wait->notif_wait_lock);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun IWL_EXPORT_SYMBOL(iwl_init_notification_wait);
159*4882a593Smuzhiyun
iwl_remove_notification(struct iwl_notif_wait_data * notif_wait,struct iwl_notification_wait * wait_entry)160*4882a593Smuzhiyun void iwl_remove_notification(struct iwl_notif_wait_data *notif_wait,
161*4882a593Smuzhiyun struct iwl_notification_wait *wait_entry)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun spin_lock_bh(¬if_wait->notif_wait_lock);
164*4882a593Smuzhiyun list_del(&wait_entry->list);
165*4882a593Smuzhiyun spin_unlock_bh(¬if_wait->notif_wait_lock);
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun IWL_EXPORT_SYMBOL(iwl_remove_notification);
168*4882a593Smuzhiyun
iwl_wait_notification(struct iwl_notif_wait_data * notif_wait,struct iwl_notification_wait * wait_entry,unsigned long timeout)169*4882a593Smuzhiyun int iwl_wait_notification(struct iwl_notif_wait_data *notif_wait,
170*4882a593Smuzhiyun struct iwl_notification_wait *wait_entry,
171*4882a593Smuzhiyun unsigned long timeout)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun int ret;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun ret = wait_event_timeout(notif_wait->notif_waitq,
176*4882a593Smuzhiyun wait_entry->triggered || wait_entry->aborted,
177*4882a593Smuzhiyun timeout);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun iwl_remove_notification(notif_wait, wait_entry);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun if (wait_entry->aborted)
182*4882a593Smuzhiyun return -EIO;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /* return value is always >= 0 */
185*4882a593Smuzhiyun if (ret <= 0)
186*4882a593Smuzhiyun return -ETIMEDOUT;
187*4882a593Smuzhiyun return 0;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun IWL_EXPORT_SYMBOL(iwl_wait_notification);
190