1 /*-
2  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
3  * which were used as the reference documentation for this implementation.
4  *
5  ******************************************************************************
6  *
7  * This file is provided under a dual BSD/GPLv2 license.  When using or
8  * redistributing this file, you may do so under either license.
9  *
10  * GPL LICENSE SUMMARY
11  *
12  * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
13  * Copyright(c) 2015 Intel Deutschland GmbH
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of version 2 of the GNU General Public License as
17  * published by the Free Software Foundation.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
27  * USA
28  *
29  * The full GNU General Public License is included in this distribution
30  * in the file called COPYING.
31  *
32  * Contact Information:
33  *  Intel Linux Wireless <linuxwifi@intel.com>
34  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
35  *
36  * BSD LICENSE
37  *
38  * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
39  * All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  *
45  *  * Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  *  * Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in
49  *    the documentation and/or other materials provided with the
50  *    distribution.
51  *  * Neither the name Intel Corporation nor the names of its
52  *    contributors may be used to endorse or promote products derived
53  *    from this software without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
56  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
57  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
58  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
59  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
60  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
61  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
62  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
63  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
65  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66  *
67  *****************************************************************************/
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/bus.h>
72 #include <sys/kernel.h>
73 #include <sys/lock.h>
74 #include <sys/malloc.h>
75 #include <sys/queue.h>
76 
77 #include "if_iwm_notif_wait.h"
78 
79 #define	IWM_WAIT_LOCK_INIT(_n, _s) \
80 	lockinit(&(_n)->lk_lk, (_s), 0, 0)
81 #define	IWM_WAIT_LOCK(_n)		lockmgr(&(_n)->lk_lk, LK_EXCLUSIVE)
82 #define	IWM_WAIT_UNLOCK(_n)		lockmgr(&(_n)->lk_lk, LK_RELEASE)
83 #define	IWM_WAIT_LOCK_DESTROY(_n)	lockuninit(&(_n)->lk_lk)
84 
85 struct iwm_notif_wait_data {
86 	struct lock lk_lk;
87 	char lk_buf[32];
88 	STAILQ_HEAD(, iwm_notification_wait) list;
89 	struct iwm_softc *sc;
90 };
91 
92 struct iwm_notif_wait_data *
93 iwm_notification_wait_init(struct iwm_softc *sc)
94 {
95 	struct iwm_notif_wait_data *data;
96 
97 	data = kmalloc(sizeof(*data), M_DEVBUF, M_INTWAIT | M_ZERO);
98 	if (data != NULL) {
99 		ksnprintf(data->lk_buf, 32, "iwm wait_notif");
100 		IWM_WAIT_LOCK_INIT(data, data->lk_buf);
101 		STAILQ_INIT(&data->list);
102 		data->sc = sc;
103 	}
104 
105 	return data;
106 }
107 
108 void
109 iwm_notification_wait_free(struct iwm_notif_wait_data *notif_data)
110 {
111 	KASSERT(STAILQ_EMPTY(&notif_data->list), ("notif list isn't empty"));
112 	IWM_WAIT_LOCK_DESTROY(notif_data);
113 	kfree(notif_data, M_DEVBUF);
114 }
115 
116 /* XXX Get rid of separate cmd argument, like in iwlwifi's code */
117 void
118 iwm_notification_wait_notify(struct iwm_notif_wait_data *notif_data,
119     uint16_t cmd, struct iwm_rx_packet *pkt)
120 {
121 	struct iwm_notification_wait *wait_entry;
122 
123 	IWM_WAIT_LOCK(notif_data);
124 	STAILQ_FOREACH(wait_entry, &notif_data->list, entry) {
125 		int found = FALSE;
126 		int i;
127 
128 		/*
129 		 * If it already finished (triggered) or has been
130 		 * aborted then don't evaluate it again to avoid races,
131 		 * Otherwise the function could be called again even
132 		 * though it returned true before
133 		 */
134 		if (wait_entry->triggered || wait_entry->aborted)
135 			continue;
136 
137 		for (i = 0; i < wait_entry->n_cmds; i++) {
138 			if (cmd == wait_entry->cmds[i]) {
139 				found = TRUE;
140 				break;
141 			}
142 		}
143 		if (!found)
144 			continue;
145 
146 		if (!wait_entry->fn ||
147 		    wait_entry->fn(notif_data->sc, pkt, wait_entry->fn_data)) {
148 			wait_entry->triggered = 1;
149 			wakeup(wait_entry);
150 		}
151 	}
152 	IWM_WAIT_UNLOCK(notif_data);
153 }
154 
155 void
156 iwm_abort_notification_waits(struct iwm_notif_wait_data *notif_data)
157 {
158 	struct iwm_notification_wait *wait_entry;
159 
160 	IWM_WAIT_LOCK(notif_data);
161 	STAILQ_FOREACH(wait_entry, &notif_data->list, entry) {
162 		wait_entry->aborted = 1;
163 		wakeup(wait_entry);
164 	}
165 	IWM_WAIT_UNLOCK(notif_data);
166 }
167 
168 void
169 iwm_init_notification_wait(struct iwm_notif_wait_data *notif_data,
170     struct iwm_notification_wait *wait_entry, const uint16_t *cmds, int n_cmds,
171     int (*fn)(struct iwm_softc *sc, struct iwm_rx_packet *pkt, void *data),
172     void *fn_data)
173 {
174 	KASSERT(n_cmds <= IWM_MAX_NOTIF_CMDS,
175 	    ("n_cmds %d is too large", n_cmds));
176 	wait_entry->fn = fn;
177 	wait_entry->fn_data = fn_data;
178 	wait_entry->n_cmds = n_cmds;
179 	memcpy(wait_entry->cmds, cmds, n_cmds * sizeof(uint16_t));
180 	wait_entry->triggered = 0;
181 	wait_entry->aborted = 0;
182 
183 	IWM_WAIT_LOCK(notif_data);
184 	STAILQ_INSERT_TAIL(&notif_data->list, wait_entry, entry);
185 	IWM_WAIT_UNLOCK(notif_data);
186 }
187 
188 int
189 iwm_wait_notification(struct iwm_notif_wait_data *notif_data,
190     struct iwm_notification_wait *wait_entry, int timeout)
191 {
192 	int ret = 0;
193 
194 	IWM_WAIT_LOCK(notif_data);
195 	if (!wait_entry->triggered && !wait_entry->aborted) {
196 		ret = lksleep(wait_entry, &notif_data->lk_lk, 0, "iwm_notif",
197 		    timeout);
198 	}
199 	STAILQ_REMOVE(&notif_data->list, wait_entry, iwm_notification_wait,
200 	    entry);
201 	IWM_WAIT_UNLOCK(notif_data);
202 
203 	return ret;
204 }
205 
206 void
207 iwm_remove_notification(struct iwm_notif_wait_data *notif_data,
208     struct iwm_notification_wait *wait_entry)
209 {
210 	IWM_WAIT_LOCK(notif_data);
211 	STAILQ_REMOVE(&notif_data->list, wait_entry, iwm_notification_wait,
212 	    entry);
213 	IWM_WAIT_UNLOCK(notif_data);
214 }
215