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 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/bus.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
73 #include <sys/lock.h>
74 #include <sys/queue.h>
75 
76 #include "if_iwm_notif_wait.h"
77 
78 struct iwm_notif_wait_data {
79 	struct lock lk;
80 	STAILQ_HEAD(, iwm_notification_wait) list;
81 	struct iwm_softc *sc;
82 };
83 
84 struct iwm_notif_wait_data *
85 iwm_notification_wait_init(struct iwm_softc *sc)
86 {
87 	struct iwm_notif_wait_data *data;
88 
89 	data = kmalloc(sizeof(*data), M_DEVBUF, M_WAITOK | M_ZERO);
90 	if (data != NULL) {
91 		lockinit(&data->lk, "iwm_notif", 0, 0);
92 		STAILQ_INIT(&data->list);
93 		data->sc = sc;
94 	}
95 
96 	return data;
97 }
98 
99 void
100 iwm_notification_wait_free(struct iwm_notif_wait_data *notif_data)
101 {
102 	KKASSERT(STAILQ_EMPTY(&notif_data->list));
103 	lockuninit(&notif_data->lk);
104 	kfree(notif_data, M_DEVBUF);
105 }
106 
107 /* XXX Get rid of separate cmd argument, like in iwlwifi's code */
108 void
109 iwm_notification_wait_notify(struct iwm_notif_wait_data *notif_data,
110     uint16_t cmd, struct iwm_rx_packet *pkt)
111 {
112 	struct iwm_notification_wait *wait_entry;
113 
114 	lockmgr(&notif_data->lk, LK_EXCLUSIVE);
115 	STAILQ_FOREACH(wait_entry, &notif_data->list, entry) {
116 		int found = FALSE;
117 		int i;
118 
119 		/*
120 		 * If it already finished (triggered) or has been
121 		 * aborted then don't evaluate it again to avoid races,
122 		 * Otherwise the function could be called again even
123 		 * though it returned true before
124 		 */
125 		if (wait_entry->triggered || wait_entry->aborted)
126 			continue;
127 
128 		for (i = 0; i < wait_entry->n_cmds; i++) {
129 			if (cmd == wait_entry->cmds[i]) {
130 				found = TRUE;
131 				break;
132 			}
133 		}
134 		if (!found)
135 			continue;
136 
137 		if (!wait_entry->fn ||
138 		    wait_entry->fn(notif_data->sc, pkt, wait_entry->fn_data)) {
139 			wait_entry->triggered = 1;
140 			wakeup(wait_entry);
141 		}
142 	}
143 	lockmgr(&notif_data->lk, LK_RELEASE);
144 }
145 
146 void
147 iwm_abort_notification_waits(struct iwm_notif_wait_data *notif_data)
148 {
149 	struct iwm_notification_wait *wait_entry;
150 
151 	lockmgr(&notif_data->lk, LK_EXCLUSIVE);
152 	STAILQ_FOREACH(wait_entry, &notif_data->list, entry) {
153 		wait_entry->aborted = 1;
154 		wakeup(wait_entry);
155 	}
156 	lockmgr(&notif_data->lk, LK_RELEASE);
157 }
158 
159 void
160 iwm_init_notification_wait(struct iwm_notif_wait_data *notif_data,
161     struct iwm_notification_wait *wait_entry, const uint16_t *cmds, int n_cmds,
162     int (*fn)(struct iwm_softc *sc, struct iwm_rx_packet *pkt, void *data),
163     void *fn_data)
164 {
165 	KKASSERT(n_cmds <= IWM_MAX_NOTIF_CMDS);
166 	wait_entry->fn = fn;
167 	wait_entry->fn_data = fn_data;
168 	wait_entry->n_cmds = n_cmds;
169 	memcpy(wait_entry->cmds, cmds, n_cmds * sizeof(uint16_t));
170 	wait_entry->triggered = 0;
171 	wait_entry->aborted = 0;
172 
173 	lockmgr(&notif_data->lk, LK_EXCLUSIVE);
174 	STAILQ_INSERT_TAIL(&notif_data->list, wait_entry, entry);
175 	lockmgr(&notif_data->lk, LK_RELEASE);
176 }
177 
178 int
179 iwm_wait_notification(struct iwm_notif_wait_data *notif_data,
180     struct iwm_notification_wait *wait_entry, int timeout)
181 {
182 	int ret = 0;
183 
184 	lockmgr(&notif_data->lk, LK_EXCLUSIVE);
185 	if (!wait_entry->triggered && !wait_entry->aborted) {
186 		ret = lksleep(wait_entry, &notif_data->lk, 0, "iwm_notif",
187 		    timeout);
188 	}
189 	STAILQ_REMOVE(&notif_data->list, wait_entry, iwm_notification_wait,
190 	    entry);
191 	lockmgr(&notif_data->lk, LK_RELEASE);
192 
193 	return ret;
194 }
195 
196 void
197 iwm_remove_notification(struct iwm_notif_wait_data *notif_data,
198     struct iwm_notification_wait *wait_entry)
199 {
200 	lockmgr(&notif_data->lk, LK_EXCLUSIVE);
201 	STAILQ_REMOVE(&notif_data->list, wait_entry, iwm_notification_wait,
202 	    entry);
203 	lockmgr(&notif_data->lk, LK_RELEASE);
204 }
205