if_iwm - Partly sync if_iwm_binding.c to Linux iwlwifi code.
[dragonfly.git] / sys / dev / netif / iwm / if_iwm_notif_wait.c
blob8e726bc4e1297f24ae39cf5d893a583efcfec383
1 /*-
2 * Based on BSD-licensed source modules in the Linux iwlwifi driver,
3 * which were used as the reference documentation for this implementation.
5 ******************************************************************************
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.
10 * GPL LICENSE SUMMARY
12 * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
13 * Copyright(c) 2015 Intel Deutschland GmbH
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.
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.
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
29 * The full GNU General Public License is included in this distribution
30 * in the file called COPYING.
32 * Contact Information:
33 * Intel Linux Wireless <linuxwifi@intel.com>
34 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
36 * BSD LICENSE
38 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
39 * All rights reserved.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
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.
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.
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>
76 #include "if_iwm_notif_wait.h"
78 struct iwm_notif_wait_data {
79 struct lock lk;
80 STAILQ_HEAD(, iwm_notification_wait) list;
81 struct iwm_softc *sc;
84 struct iwm_notif_wait_data *
85 iwm_notification_wait_init(struct iwm_softc *sc)
87 struct iwm_notif_wait_data *data;
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;
96 return data;
99 void
100 iwm_notification_wait_free(struct iwm_notif_wait_data *notif_data)
102 KKASSERT(STAILQ_EMPTY(&notif_data->list));
103 lockuninit(&notif_data->lk);
104 kfree(notif_data, M_DEVBUF);
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)
112 struct iwm_notification_wait *wait_entry;
114 lockmgr(&notif_data->lk, LK_EXCLUSIVE);
115 STAILQ_FOREACH(wait_entry, &notif_data->list, entry) {
116 int found = FALSE;
117 int i;
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
125 if (wait_entry->triggered || wait_entry->aborted)
126 continue;
128 for (i = 0; i < wait_entry->n_cmds; i++) {
129 if (cmd == wait_entry->cmds[i]) {
130 found = TRUE;
131 break;
134 if (!found)
135 continue;
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);
143 lockmgr(&notif_data->lk, LK_RELEASE);
146 void
147 iwm_abort_notification_waits(struct iwm_notif_wait_data *notif_data)
149 struct iwm_notification_wait *wait_entry;
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);
156 lockmgr(&notif_data->lk, LK_RELEASE);
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)
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;
173 lockmgr(&notif_data->lk, LK_EXCLUSIVE);
174 STAILQ_INSERT_TAIL(&notif_data->list, wait_entry, entry);
175 lockmgr(&notif_data->lk, LK_RELEASE);
179 iwm_wait_notification(struct iwm_notif_wait_data *notif_data,
180 struct iwm_notification_wait *wait_entry, int timeout)
182 int ret = 0;
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);
189 STAILQ_REMOVE(&notif_data->list, wait_entry, iwm_notification_wait,
190 entry);
191 lockmgr(&notif_data->lk, LK_RELEASE);
193 return ret;
196 void
197 iwm_remove_notification(struct iwm_notif_wait_data *notif_data,
198 struct iwm_notification_wait *wait_entry)
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);