From owner-svn-src-head@freebsd.org Mon Feb 6 05:27:09 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 97C60CD2DE4; Mon, 6 Feb 2017 05:27:09 +0000 (UTC) (envelope-from adrian@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 60845E23; Mon, 6 Feb 2017 05:27:09 +0000 (UTC) (envelope-from adrian@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v165R87V083203; Mon, 6 Feb 2017 05:27:08 GMT (envelope-from adrian@FreeBSD.org) Received: (from adrian@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v165R8bO083196; Mon, 6 Feb 2017 05:27:08 GMT (envelope-from adrian@FreeBSD.org) Message-Id: <201702060527.v165R8bO083196@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: adrian set sender to adrian@FreeBSD.org using -f From: Adrian Chadd Date: Mon, 6 Feb 2017 05:27:08 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r313322 - in head/sys: conf dev/iwm modules/iwm X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 06 Feb 2017 05:27:09 -0000 Author: adrian Date: Mon Feb 6 05:27:07 2017 New Revision: 313322 URL: https://svnweb.freebsd.org/changeset/base/313322 Log: [iwm] Add implementation of the notification wait api from iwlwifi. Obtained from: Linux iwlwifi Obtained from: DragonflyBSD commit 94dc1dadceb57b688036211262d678bc6bbdde37 Added: head/sys/dev/iwm/if_iwm_notif_wait.c (contents, props changed) head/sys/dev/iwm/if_iwm_notif_wait.h (contents, props changed) Modified: head/sys/conf/files head/sys/dev/iwm/if_iwm.c head/sys/dev/iwm/if_iwmvar.h head/sys/modules/iwm/Makefile Modified: head/sys/conf/files ============================================================================== --- head/sys/conf/files Mon Feb 6 05:27:05 2017 (r313321) +++ head/sys/conf/files Mon Feb 6 05:27:07 2017 (r313322) @@ -1882,6 +1882,7 @@ dev/iwm/if_iwm.c optional iwm dev/iwm/if_iwm_binding.c optional iwm dev/iwm/if_iwm_led.c optional iwm dev/iwm/if_iwm_mac_ctxt.c optional iwm +dev/netif/iwm/if_iwm_notif_wait.c optional iwm dev/iwm/if_iwm_pcie_trans.c optional iwm dev/iwm/if_iwm_phy_ctxt.c optional iwm dev/iwm/if_iwm_phy_db.c optional iwm Modified: head/sys/dev/iwm/if_iwm.c ============================================================================== --- head/sys/dev/iwm/if_iwm.c Mon Feb 6 05:27:05 2017 (r313321) +++ head/sys/dev/iwm/if_iwm.c Mon Feb 6 05:27:07 2017 (r313322) @@ -153,6 +153,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -5277,6 +5278,8 @@ iwm_notif_intr(struct iwm_softc *sc) continue; } + iwm_notification_wait_notify(sc->sc_notif_wait, code, pkt); + switch (code) { case IWM_REPLY_RX_PHY_CMD: iwm_mvm_rx_rx_phy_cmd(sc, pkt, data); @@ -5896,6 +5899,12 @@ iwm_attach(device_t dev) callout_init_mtx(&sc->sc_led_blink_to, &sc->sc_mtx, 0); TASK_INIT(&sc->sc_es_task, 0, iwm_endscan_cb, sc); + sc->sc_notif_wait = iwm_notification_wait_init(sc); + if (sc->sc_notif_wait == NULL) { + device_printf(dev, "failed to init notification wait struct\n"); + goto fail; + } + /* Init phy db */ sc->sc_phy_db = iwm_phy_db_init(sc); if (!sc->sc_phy_db) { @@ -6383,6 +6392,11 @@ iwm_detach_local(struct iwm_softc *sc, i /* Finished with the hardware - detach things */ iwm_pci_detach(dev); + if (sc->sc_notif_wait != NULL) { + iwm_notification_wait_free(sc->sc_notif_wait); + sc->sc_notif_wait = NULL; + } + mbufq_drain(&sc->sc_snd); IWM_LOCK_DESTROY(sc); Added: head/sys/dev/iwm/if_iwm_notif_wait.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/iwm/if_iwm_notif_wait.c Mon Feb 6 05:27:07 2017 (r313322) @@ -0,0 +1,219 @@ +/*- + * Based on BSD-licensed source modules in the Linux iwlwifi driver, + * which were used as the reference documentation for this implementation. + * + ****************************************************************************** + * + * This file is provided under a dual BSD/GPLv2 license. When using or + * redistributing this file, you may do so under either license. + * + * GPL LICENSE SUMMARY + * + * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved. + * Copyright(c) 2015 Intel Deutschland GmbH + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, + * USA + * + * The full GNU General Public License is included in this distribution + * in the file called COPYING. + * + * Contact Information: + * Intel Linux Wireless + * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 + * + * BSD LICENSE + * + * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *****************************************************************************/ + +#include +__FBSDID("$FreeBSD$"); + +#include "opt_wlan.h" + +#include +#include +#include +#include +#include +#include +#include + +#include + +#define IWM_WAIT_LOCK_INIT(_n, _s) \ + mtx_init(&(_n)->lk_mtx, (_s), "iwm wait_notif", MTX_DEF); +#define IWM_WAIT_LOCK(_n) mtx_lock(&(_n)->lk_mtx) +#define IWM_WAIT_UNLOCK(_n) mtx_unlock(&(_n)->lk_mtx) +#define IWM_WAIT_LOCK_DESTROY(_n) mtx_destroy(&(_n)->lk_mtx) + +struct iwm_notif_wait_data { + struct mtx lk_mtx; + char lk_buf[32]; + STAILQ_HEAD(, iwm_notification_wait) list; + struct iwm_softc *sc; +}; + +struct iwm_notif_wait_data * +iwm_notification_wait_init(struct iwm_softc *sc) +{ + struct iwm_notif_wait_data *data; + + data = malloc(sizeof(*data), M_DEVBUF, M_NOWAIT | M_ZERO); + if (data != NULL) { + snprintf(data->lk_buf, 32, "iwm wait_notif"); + IWM_WAIT_LOCK_INIT(data, data->lk_buf); + STAILQ_INIT(&data->list); + data->sc = sc; + } + + return data; +} + +void +iwm_notification_wait_free(struct iwm_notif_wait_data *notif_data) +{ + KASSERT(STAILQ_EMPTY(¬if_data->list), ("notif list isn't empty")); + IWM_WAIT_LOCK_DESTROY(notif_data); + free(notif_data, M_DEVBUF); +} + +/* XXX Get rid of separate cmd argument, like in iwlwifi's code */ +void +iwm_notification_wait_notify(struct iwm_notif_wait_data *notif_data, + uint16_t cmd, struct iwm_rx_packet *pkt) +{ + struct iwm_notification_wait *wait_entry; + + IWM_WAIT_LOCK(notif_data); + STAILQ_FOREACH(wait_entry, ¬if_data->list, entry) { + int found = FALSE; + int i; + + /* + * If it already finished (triggered) or has been + * aborted then don't evaluate it again to avoid races, + * Otherwise the function could be called again even + * though it returned true before + */ + if (wait_entry->triggered || wait_entry->aborted) + continue; + + for (i = 0; i < wait_entry->n_cmds; i++) { + if (cmd == wait_entry->cmds[i]) { + found = TRUE; + break; + } + } + if (!found) + continue; + + if (!wait_entry->fn || + wait_entry->fn(notif_data->sc, pkt, wait_entry->fn_data)) { + wait_entry->triggered = 1; + wakeup(wait_entry); + } + } + IWM_WAIT_UNLOCK(notif_data); +} + +void +iwm_abort_notification_waits(struct iwm_notif_wait_data *notif_data) +{ + struct iwm_notification_wait *wait_entry; + + IWM_WAIT_LOCK(notif_data); + STAILQ_FOREACH(wait_entry, ¬if_data->list, entry) { + wait_entry->aborted = 1; + wakeup(wait_entry); + } + IWM_WAIT_UNLOCK(notif_data); +} + +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) +{ + KASSERT(n_cmds <= IWM_MAX_NOTIF_CMDS, + ("n_cmds %d is too large", n_cmds)); + wait_entry->fn = fn; + wait_entry->fn_data = fn_data; + wait_entry->n_cmds = n_cmds; + memcpy(wait_entry->cmds, cmds, n_cmds * sizeof(uint16_t)); + wait_entry->triggered = 0; + wait_entry->aborted = 0; + + IWM_WAIT_LOCK(notif_data); + STAILQ_INSERT_TAIL(¬if_data->list, wait_entry, entry); + IWM_WAIT_UNLOCK(notif_data); +} + +int +iwm_wait_notification(struct iwm_notif_wait_data *notif_data, + struct iwm_notification_wait *wait_entry, int timeout) +{ + int ret = 0; + + IWM_WAIT_LOCK(notif_data); + if (!wait_entry->triggered && !wait_entry->aborted) { + ret = msleep(wait_entry, ¬if_data->lk_mtx, 0, "iwm_notif", + timeout); + } + STAILQ_REMOVE(¬if_data->list, wait_entry, iwm_notification_wait, + entry); + IWM_WAIT_UNLOCK(notif_data); + + return ret; +} + +void +iwm_remove_notification(struct iwm_notif_wait_data *notif_data, + struct iwm_notification_wait *wait_entry) +{ + IWM_WAIT_LOCK(notif_data); + STAILQ_REMOVE(¬if_data->list, wait_entry, iwm_notification_wait, + entry); + IWM_WAIT_UNLOCK(notif_data); +} Added: head/sys/dev/iwm/if_iwm_notif_wait.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/iwm/if_iwm_notif_wait.h Mon Feb 6 05:27:07 2017 (r313322) @@ -0,0 +1,138 @@ +/*- + * Based on BSD-licensed source modules in the Linux iwlwifi driver, + * which were used as the reference documentation for this implementation. + * + ****************************************************************************** + * + * This file is provided under a dual BSD/GPLv2 license. When using or + * redistributing this file, you may do so under either license. + * + * GPL LICENSE SUMMARY + * + * Copyright(c) 2007 - 2014 Intel Corporation. All rights reserved. + * Copyright(c) 2015 Intel Deutschland GmbH + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, + * USA + * + * The full GNU General Public License is included in this distribution + * in the file called COPYING. + * + * Contact Information: + * Intel Linux Wireless + * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 + * + * BSD LICENSE + * + * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *****************************************************************************/ + +/* $FreeBSD$ */ + +#ifndef __IF_IWN_NOTIF_WAIT_H__ +#define __IF_IWN_NOTIF_WAIT_H__ + +#include + +#define IWM_MAX_NOTIF_CMDS 5 + +struct iwm_rx_packet; +struct iwm_softc; + +/** + * struct iwm_notification_wait - notification wait entry + * @entry: link for global list + * @fn: Function called with the notification. If the function + * returns true, the wait is over, if it returns false then + * the waiter stays blocked. If no function is given, any + * of the listed commands will unblock the waiter. + * @cmds: command IDs + * @n_cmds: number of command IDs + * @triggered: waiter should be woken up + * @aborted: wait was aborted + * + * This structure is not used directly, to wait for a + * notification declare it on the stack, and call + * iwm_init_notification_wait() with appropriate + * parameters. Then do whatever will cause the ucode + * to notify the driver, and to wait for that then + * call iwm_wait_notification(). + * + * Each notification is one-shot. If at some point we + * need to support multi-shot notifications (which + * can't be allocated on the stack) we need to modify + * the code for them. + */ +struct iwm_notification_wait { + STAILQ_ENTRY(iwm_notification_wait) entry; + + int (*fn)(struct iwm_softc *sc, struct iwm_rx_packet *pkt, void *data); + void *fn_data; + + uint16_t cmds[IWM_MAX_NOTIF_CMDS]; + uint8_t n_cmds; + int triggered, aborted; +}; + +/* caller functions */ +extern struct iwm_notif_wait_data *iwm_notification_wait_init( + struct iwm_softc *sc); +extern void iwm_notification_wait_free(struct iwm_notif_wait_data *notif_data); +extern void iwm_notification_wait_notify( + struct iwm_notif_wait_data *notif_data, uint16_t cmd, + struct iwm_rx_packet *pkt); +extern void iwm_abort_notification_waits( + struct iwm_notif_wait_data *notif_data); + +/* user functions */ +extern 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); +extern int iwm_wait_notification(struct iwm_notif_wait_data *notif_data, + struct iwm_notification_wait *wait_entry, int timeout); +extern void iwm_remove_notification(struct iwm_notif_wait_data *notif_data, + struct iwm_notification_wait *wait_entry); + +#endif /* __IF_IWN_NOTIF_WAIT_H__ */ Modified: head/sys/dev/iwm/if_iwmvar.h ============================================================================== --- head/sys/dev/iwm/if_iwmvar.h Mon Feb 6 05:27:05 2017 (r313321) +++ head/sys/dev/iwm/if_iwmvar.h Mon Feb 6 05:27:07 2017 (r313322) @@ -519,6 +519,8 @@ struct iwm_softc { struct iwm_tx_radiotap_header sc_txtap; int sc_max_rssi; + + struct iwm_notif_wait_data *sc_notif_wait; }; #define IWM_LOCK_INIT(_sc) \ Modified: head/sys/modules/iwm/Makefile ============================================================================== --- head/sys/modules/iwm/Makefile Mon Feb 6 05:27:05 2017 (r313321) +++ head/sys/modules/iwm/Makefile Mon Feb 6 05:27:07 2017 (r313322) @@ -6,7 +6,7 @@ KMOD= if_iwm # Main driver SRCS= if_iwm.c if_iwm_binding.c if_iwm_util.c if_iwm_phy_db.c SRCS+= if_iwm_mac_ctxt.c if_iwm_phy_ctxt.c if_iwm_time_event.c -SRCS+= if_iwm_power.c if_iwm_scan.c if_iwm_led.c +SRCS+= if_iwm_power.c if_iwm_scan.c if_iwm_led.c if_iwm_notif_wait.c # bus layer SRCS+= if_iwm_pcie_trans.c SRCS+= device_if.h bus_if.h pci_if.h opt_wlan.h