xref: /freebsd/sys/dev/iwm/if_iwm_notif_wait.c (revision 02e9120893770924227138ba49df1edb3896112a)
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/cdefs.h>
70 #include "opt_wlan.h"
71 #include "opt_iwm.h"
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/bus.h>
76 #include <sys/kernel.h>
77 #include <sys/lock.h>
78 #include <sys/malloc.h>
79 #include <sys/mutex.h>
80 #include <sys/queue.h>
81 
82 #include <dev/iwm/if_iwm_notif_wait.h>
83 
84 #define	IWM_WAIT_LOCK_INIT(_n, _s) \
85 	mtx_init(&(_n)->lk_mtx, (_s), "iwm wait_notif", MTX_DEF);
86 #define	IWM_WAIT_LOCK(_n)		mtx_lock(&(_n)->lk_mtx)
87 #define	IWM_WAIT_UNLOCK(_n)		mtx_unlock(&(_n)->lk_mtx)
88 #define	IWM_WAIT_LOCK_DESTROY(_n)	mtx_destroy(&(_n)->lk_mtx)
89 
90 struct iwm_notif_wait_data {
91 	struct mtx lk_mtx;
92 	char lk_buf[32];
93 	STAILQ_HEAD(, iwm_notification_wait) list;
94 	struct iwm_softc *sc;
95 };
96 
97 struct iwm_notif_wait_data *
98 iwm_notification_wait_init(struct iwm_softc *sc)
99 {
100 	struct iwm_notif_wait_data *data;
101 
102 	data = malloc(sizeof(*data), M_DEVBUF, M_NOWAIT | M_ZERO);
103 	if (data != NULL) {
104 		snprintf(data->lk_buf, 32, "iwm wait_notif");
105 		IWM_WAIT_LOCK_INIT(data, data->lk_buf);
106 		STAILQ_INIT(&data->list);
107 		data->sc = sc;
108 	}
109 
110 	return data;
111 }
112 
113 void
114 iwm_notification_wait_free(struct iwm_notif_wait_data *notif_data)
115 {
116 	KASSERT(STAILQ_EMPTY(&notif_data->list), ("notif list isn't empty"));
117 	IWM_WAIT_LOCK_DESTROY(notif_data);
118 	free(notif_data, M_DEVBUF);
119 }
120 
121 /* XXX Get rid of separate cmd argument, like in iwlwifi's code */
122 void
123 iwm_notification_wait_notify(struct iwm_notif_wait_data *notif_data,
124     uint16_t cmd, struct iwm_rx_packet *pkt)
125 {
126 	struct iwm_notification_wait *wait_entry;
127 
128 	IWM_WAIT_LOCK(notif_data);
129 	STAILQ_FOREACH(wait_entry, &notif_data->list, entry) {
130 		int found = FALSE;
131 		int i;
132 
133 		/*
134 		 * If it already finished (triggered) or has been
135 		 * aborted then don't evaluate it again to avoid races,
136 		 * Otherwise the function could be called again even
137 		 * though it returned true before
138 		 */
139 		if (wait_entry->triggered || wait_entry->aborted)
140 			continue;
141 
142 		for (i = 0; i < wait_entry->n_cmds; i++) {
143 			if (cmd == wait_entry->cmds[i]) {
144 				found = TRUE;
145 				break;
146 			}
147 		}
148 		if (!found)
149 			continue;
150 
151 		if (!wait_entry->fn ||
152 		    wait_entry->fn(notif_data->sc, pkt, wait_entry->fn_data)) {
153 			wait_entry->triggered = 1;
154 			wakeup(wait_entry);
155 		}
156 	}
157 	IWM_WAIT_UNLOCK(notif_data);
158 }
159 
160 void
161 iwm_abort_notification_waits(struct iwm_notif_wait_data *notif_data)
162 {
163 	struct iwm_notification_wait *wait_entry;
164 
165 	IWM_WAIT_LOCK(notif_data);
166 	STAILQ_FOREACH(wait_entry, &notif_data->list, entry) {
167 		wait_entry->aborted = 1;
168 		wakeup(wait_entry);
169 	}
170 	IWM_WAIT_UNLOCK(notif_data);
171 }
172 
173 void
174 iwm_init_notification_wait(struct iwm_notif_wait_data *notif_data,
175     struct iwm_notification_wait *wait_entry, const uint16_t *cmds, int n_cmds,
176     int (*fn)(struct iwm_softc *sc, struct iwm_rx_packet *pkt, void *data),
177     void *fn_data)
178 {
179 	KASSERT(n_cmds <= IWM_MAX_NOTIF_CMDS,
180 	    ("n_cmds %d is too large", n_cmds));
181 	wait_entry->fn = fn;
182 	wait_entry->fn_data = fn_data;
183 	wait_entry->n_cmds = n_cmds;
184 	memcpy(wait_entry->cmds, cmds, n_cmds * sizeof(uint16_t));
185 	wait_entry->triggered = 0;
186 	wait_entry->aborted = 0;
187 
188 	IWM_WAIT_LOCK(notif_data);
189 	STAILQ_INSERT_TAIL(&notif_data->list, wait_entry, entry);
190 	IWM_WAIT_UNLOCK(notif_data);
191 }
192 
193 int
194 iwm_wait_notification(struct iwm_notif_wait_data *notif_data,
195     struct iwm_notification_wait *wait_entry, int timeout)
196 {
197 	int ret = 0;
198 
199 	IWM_WAIT_LOCK(notif_data);
200 	if (!wait_entry->triggered && !wait_entry->aborted) {
201 		ret = msleep(wait_entry, &notif_data->lk_mtx, 0, "iwm_notif",
202 		    timeout);
203 	}
204 	STAILQ_REMOVE(&notif_data->list, wait_entry, iwm_notification_wait,
205 	    entry);
206 	IWM_WAIT_UNLOCK(notif_data);
207 
208 	return ret;
209 }
210 
211 void
212 iwm_remove_notification(struct iwm_notif_wait_data *notif_data,
213     struct iwm_notification_wait *wait_entry)
214 {
215 	IWM_WAIT_LOCK(notif_data);
216 	STAILQ_REMOVE(&notif_data->list, wait_entry, iwm_notification_wait,
217 	    entry);
218 	IWM_WAIT_UNLOCK(notif_data);
219 }
220