16e39c650SAdrian Chadd /*-
26e39c650SAdrian Chadd * Based on BSD-licensed source modules in the Linux iwlwifi driver,
36e39c650SAdrian Chadd * which were used as the reference documentation for this implementation.
46e39c650SAdrian Chadd *
56e39c650SAdrian Chadd ******************************************************************************
66e39c650SAdrian Chadd *
76e39c650SAdrian Chadd * This file is provided under a dual BSD/GPLv2 license. When using or
86e39c650SAdrian Chadd * redistributing this file, you may do so under either license.
96e39c650SAdrian Chadd *
106e39c650SAdrian Chadd * GPL LICENSE SUMMARY
116e39c650SAdrian Chadd *
126e39c650SAdrian Chadd * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved.
136e39c650SAdrian Chadd * Copyright(c) 2015 Intel Deutschland GmbH
146e39c650SAdrian Chadd *
156e39c650SAdrian Chadd * This program is free software; you can redistribute it and/or modify
166e39c650SAdrian Chadd * it under the terms of version 2 of the GNU General Public License as
176e39c650SAdrian Chadd * published by the Free Software Foundation.
186e39c650SAdrian Chadd *
196e39c650SAdrian Chadd * This program is distributed in the hope that it will be useful, but
206e39c650SAdrian Chadd * WITHOUT ANY WARRANTY; without even the implied warranty of
216e39c650SAdrian Chadd * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
226e39c650SAdrian Chadd * General Public License for more details.
236e39c650SAdrian Chadd *
246e39c650SAdrian Chadd * You should have received a copy of the GNU General Public License
256e39c650SAdrian Chadd * along with this program; if not, write to the Free Software
266e39c650SAdrian Chadd * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
276e39c650SAdrian Chadd * USA
286e39c650SAdrian Chadd *
296e39c650SAdrian Chadd * The full GNU General Public License is included in this distribution
306e39c650SAdrian Chadd * in the file called COPYING.
316e39c650SAdrian Chadd *
326e39c650SAdrian Chadd * Contact Information:
336e39c650SAdrian Chadd * Intel Linux Wireless <linuxwifi@intel.com>
346e39c650SAdrian Chadd * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
356e39c650SAdrian Chadd *
366e39c650SAdrian Chadd * BSD LICENSE
376e39c650SAdrian Chadd *
386e39c650SAdrian Chadd * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
396e39c650SAdrian Chadd * All rights reserved.
406e39c650SAdrian Chadd *
416e39c650SAdrian Chadd * Redistribution and use in source and binary forms, with or without
426e39c650SAdrian Chadd * modification, are permitted provided that the following conditions
436e39c650SAdrian Chadd * are met:
446e39c650SAdrian Chadd *
456e39c650SAdrian Chadd * * Redistributions of source code must retain the above copyright
466e39c650SAdrian Chadd * notice, this list of conditions and the following disclaimer.
476e39c650SAdrian Chadd * * Redistributions in binary form must reproduce the above copyright
486e39c650SAdrian Chadd * notice, this list of conditions and the following disclaimer in
496e39c650SAdrian Chadd * the documentation and/or other materials provided with the
506e39c650SAdrian Chadd * distribution.
516e39c650SAdrian Chadd * * Neither the name Intel Corporation nor the names of its
526e39c650SAdrian Chadd * contributors may be used to endorse or promote products derived
536e39c650SAdrian Chadd * from this software without specific prior written permission.
546e39c650SAdrian Chadd *
556e39c650SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
566e39c650SAdrian Chadd * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
576e39c650SAdrian Chadd * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
586e39c650SAdrian Chadd * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
596e39c650SAdrian Chadd * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
606e39c650SAdrian Chadd * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
616e39c650SAdrian Chadd * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
626e39c650SAdrian Chadd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
636e39c650SAdrian Chadd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
646e39c650SAdrian Chadd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
656e39c650SAdrian Chadd * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
666e39c650SAdrian Chadd *
676e39c650SAdrian Chadd *****************************************************************************/
686e39c650SAdrian Chadd
696e39c650SAdrian Chadd #include <sys/cdefs.h>
706e39c650SAdrian Chadd #include "opt_wlan.h"
71616201d1SAdrian Chadd #include "opt_iwm.h"
726e39c650SAdrian Chadd
736e39c650SAdrian Chadd #include <sys/param.h>
746e39c650SAdrian Chadd #include <sys/systm.h>
756e39c650SAdrian Chadd #include <sys/bus.h>
766e39c650SAdrian Chadd #include <sys/kernel.h>
776e39c650SAdrian Chadd #include <sys/lock.h>
78*e2e050c8SConrad Meyer #include <sys/malloc.h>
79*e2e050c8SConrad Meyer #include <sys/mutex.h>
806e39c650SAdrian Chadd #include <sys/queue.h>
816e39c650SAdrian Chadd
826e39c650SAdrian Chadd #include <dev/iwm/if_iwm_notif_wait.h>
836e39c650SAdrian Chadd
846e39c650SAdrian Chadd #define IWM_WAIT_LOCK_INIT(_n, _s) \
856e39c650SAdrian Chadd mtx_init(&(_n)->lk_mtx, (_s), "iwm wait_notif", MTX_DEF);
866e39c650SAdrian Chadd #define IWM_WAIT_LOCK(_n) mtx_lock(&(_n)->lk_mtx)
876e39c650SAdrian Chadd #define IWM_WAIT_UNLOCK(_n) mtx_unlock(&(_n)->lk_mtx)
886e39c650SAdrian Chadd #define IWM_WAIT_LOCK_DESTROY(_n) mtx_destroy(&(_n)->lk_mtx)
896e39c650SAdrian Chadd
906e39c650SAdrian Chadd struct iwm_notif_wait_data {
916e39c650SAdrian Chadd struct mtx lk_mtx;
926e39c650SAdrian Chadd char lk_buf[32];
936e39c650SAdrian Chadd STAILQ_HEAD(, iwm_notification_wait) list;
946e39c650SAdrian Chadd struct iwm_softc *sc;
956e39c650SAdrian Chadd };
966e39c650SAdrian Chadd
976e39c650SAdrian Chadd struct iwm_notif_wait_data *
iwm_notification_wait_init(struct iwm_softc * sc)986e39c650SAdrian Chadd iwm_notification_wait_init(struct iwm_softc *sc)
996e39c650SAdrian Chadd {
1006e39c650SAdrian Chadd struct iwm_notif_wait_data *data;
1016e39c650SAdrian Chadd
1026e39c650SAdrian Chadd data = malloc(sizeof(*data), M_DEVBUF, M_NOWAIT | M_ZERO);
1036e39c650SAdrian Chadd if (data != NULL) {
1046e39c650SAdrian Chadd snprintf(data->lk_buf, 32, "iwm wait_notif");
1056e39c650SAdrian Chadd IWM_WAIT_LOCK_INIT(data, data->lk_buf);
1066e39c650SAdrian Chadd STAILQ_INIT(&data->list);
1076e39c650SAdrian Chadd data->sc = sc;
1086e39c650SAdrian Chadd }
1096e39c650SAdrian Chadd
1106e39c650SAdrian Chadd return data;
1116e39c650SAdrian Chadd }
1126e39c650SAdrian Chadd
1136e39c650SAdrian Chadd void
iwm_notification_wait_free(struct iwm_notif_wait_data * notif_data)1146e39c650SAdrian Chadd iwm_notification_wait_free(struct iwm_notif_wait_data *notif_data)
1156e39c650SAdrian Chadd {
1166e39c650SAdrian Chadd KASSERT(STAILQ_EMPTY(¬if_data->list), ("notif list isn't empty"));
1176e39c650SAdrian Chadd IWM_WAIT_LOCK_DESTROY(notif_data);
1186e39c650SAdrian Chadd free(notif_data, M_DEVBUF);
1196e39c650SAdrian Chadd }
1206e39c650SAdrian Chadd
1216e39c650SAdrian Chadd /* XXX Get rid of separate cmd argument, like in iwlwifi's code */
1226e39c650SAdrian Chadd void
iwm_notification_wait_notify(struct iwm_notif_wait_data * notif_data,uint16_t cmd,struct iwm_rx_packet * pkt)1236e39c650SAdrian Chadd iwm_notification_wait_notify(struct iwm_notif_wait_data *notif_data,
1246e39c650SAdrian Chadd uint16_t cmd, struct iwm_rx_packet *pkt)
1256e39c650SAdrian Chadd {
1266e39c650SAdrian Chadd struct iwm_notification_wait *wait_entry;
1276e39c650SAdrian Chadd
1286e39c650SAdrian Chadd IWM_WAIT_LOCK(notif_data);
1296e39c650SAdrian Chadd STAILQ_FOREACH(wait_entry, ¬if_data->list, entry) {
1306e39c650SAdrian Chadd int found = FALSE;
1316e39c650SAdrian Chadd int i;
1326e39c650SAdrian Chadd
1336e39c650SAdrian Chadd /*
1346e39c650SAdrian Chadd * If it already finished (triggered) or has been
1356e39c650SAdrian Chadd * aborted then don't evaluate it again to avoid races,
1366e39c650SAdrian Chadd * Otherwise the function could be called again even
1376e39c650SAdrian Chadd * though it returned true before
1386e39c650SAdrian Chadd */
1396e39c650SAdrian Chadd if (wait_entry->triggered || wait_entry->aborted)
1406e39c650SAdrian Chadd continue;
1416e39c650SAdrian Chadd
1426e39c650SAdrian Chadd for (i = 0; i < wait_entry->n_cmds; i++) {
1436e39c650SAdrian Chadd if (cmd == wait_entry->cmds[i]) {
1446e39c650SAdrian Chadd found = TRUE;
1456e39c650SAdrian Chadd break;
1466e39c650SAdrian Chadd }
1476e39c650SAdrian Chadd }
1486e39c650SAdrian Chadd if (!found)
1496e39c650SAdrian Chadd continue;
1506e39c650SAdrian Chadd
1516e39c650SAdrian Chadd if (!wait_entry->fn ||
1526e39c650SAdrian Chadd wait_entry->fn(notif_data->sc, pkt, wait_entry->fn_data)) {
1536e39c650SAdrian Chadd wait_entry->triggered = 1;
1546e39c650SAdrian Chadd wakeup(wait_entry);
1556e39c650SAdrian Chadd }
1566e39c650SAdrian Chadd }
1576e39c650SAdrian Chadd IWM_WAIT_UNLOCK(notif_data);
1586e39c650SAdrian Chadd }
1596e39c650SAdrian Chadd
1606e39c650SAdrian Chadd void
iwm_abort_notification_waits(struct iwm_notif_wait_data * notif_data)1616e39c650SAdrian Chadd iwm_abort_notification_waits(struct iwm_notif_wait_data *notif_data)
1626e39c650SAdrian Chadd {
1636e39c650SAdrian Chadd struct iwm_notification_wait *wait_entry;
1646e39c650SAdrian Chadd
1656e39c650SAdrian Chadd IWM_WAIT_LOCK(notif_data);
1666e39c650SAdrian Chadd STAILQ_FOREACH(wait_entry, ¬if_data->list, entry) {
1676e39c650SAdrian Chadd wait_entry->aborted = 1;
1686e39c650SAdrian Chadd wakeup(wait_entry);
1696e39c650SAdrian Chadd }
1706e39c650SAdrian Chadd IWM_WAIT_UNLOCK(notif_data);
1716e39c650SAdrian Chadd }
1726e39c650SAdrian Chadd
1736e39c650SAdrian Chadd void
iwm_init_notification_wait(struct iwm_notif_wait_data * notif_data,struct iwm_notification_wait * wait_entry,const uint16_t * cmds,int n_cmds,int (* fn)(struct iwm_softc * sc,struct iwm_rx_packet * pkt,void * data),void * fn_data)1746e39c650SAdrian Chadd iwm_init_notification_wait(struct iwm_notif_wait_data *notif_data,
1756e39c650SAdrian Chadd struct iwm_notification_wait *wait_entry, const uint16_t *cmds, int n_cmds,
1766e39c650SAdrian Chadd int (*fn)(struct iwm_softc *sc, struct iwm_rx_packet *pkt, void *data),
1776e39c650SAdrian Chadd void *fn_data)
1786e39c650SAdrian Chadd {
1796e39c650SAdrian Chadd KASSERT(n_cmds <= IWM_MAX_NOTIF_CMDS,
1806e39c650SAdrian Chadd ("n_cmds %d is too large", n_cmds));
1816e39c650SAdrian Chadd wait_entry->fn = fn;
1826e39c650SAdrian Chadd wait_entry->fn_data = fn_data;
1836e39c650SAdrian Chadd wait_entry->n_cmds = n_cmds;
1846e39c650SAdrian Chadd memcpy(wait_entry->cmds, cmds, n_cmds * sizeof(uint16_t));
1856e39c650SAdrian Chadd wait_entry->triggered = 0;
1866e39c650SAdrian Chadd wait_entry->aborted = 0;
1876e39c650SAdrian Chadd
1886e39c650SAdrian Chadd IWM_WAIT_LOCK(notif_data);
1896e39c650SAdrian Chadd STAILQ_INSERT_TAIL(¬if_data->list, wait_entry, entry);
1906e39c650SAdrian Chadd IWM_WAIT_UNLOCK(notif_data);
1916e39c650SAdrian Chadd }
1926e39c650SAdrian Chadd
1936e39c650SAdrian Chadd int
iwm_wait_notification(struct iwm_notif_wait_data * notif_data,struct iwm_notification_wait * wait_entry,int timeout)1946e39c650SAdrian Chadd iwm_wait_notification(struct iwm_notif_wait_data *notif_data,
1956e39c650SAdrian Chadd struct iwm_notification_wait *wait_entry, int timeout)
1966e39c650SAdrian Chadd {
1976e39c650SAdrian Chadd int ret = 0;
1986e39c650SAdrian Chadd
1996e39c650SAdrian Chadd IWM_WAIT_LOCK(notif_data);
2006e39c650SAdrian Chadd if (!wait_entry->triggered && !wait_entry->aborted) {
2016e39c650SAdrian Chadd ret = msleep(wait_entry, ¬if_data->lk_mtx, 0, "iwm_notif",
2026e39c650SAdrian Chadd timeout);
2036e39c650SAdrian Chadd }
2046e39c650SAdrian Chadd STAILQ_REMOVE(¬if_data->list, wait_entry, iwm_notification_wait,
2056e39c650SAdrian Chadd entry);
2066e39c650SAdrian Chadd IWM_WAIT_UNLOCK(notif_data);
2076e39c650SAdrian Chadd
2086e39c650SAdrian Chadd return ret;
2096e39c650SAdrian Chadd }
2106e39c650SAdrian Chadd
2116e39c650SAdrian Chadd void
iwm_remove_notification(struct iwm_notif_wait_data * notif_data,struct iwm_notification_wait * wait_entry)2126e39c650SAdrian Chadd iwm_remove_notification(struct iwm_notif_wait_data *notif_data,
2136e39c650SAdrian Chadd struct iwm_notification_wait *wait_entry)
2146e39c650SAdrian Chadd {
2156e39c650SAdrian Chadd IWM_WAIT_LOCK(notif_data);
2166e39c650SAdrian Chadd STAILQ_REMOVE(¬if_data->list, wait_entry, iwm_notification_wait,
2176e39c650SAdrian Chadd entry);
2186e39c650SAdrian Chadd IWM_WAIT_UNLOCK(notif_data);
2196e39c650SAdrian Chadd }
220