xref: /dflybsd-src/sys/dev/netif/iwm/if_iwm_time_event.c (revision 4cbc7cf96a5a52309aab9d8db3e0335042767af2)
124a8d46aSMatthew Dillon /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
224a8d46aSMatthew Dillon 
324a8d46aSMatthew Dillon /*
424a8d46aSMatthew Dillon  * Copyright (c) 2014 genua mbh <info@genua.de>
524a8d46aSMatthew Dillon  * Copyright (c) 2014 Fixup Software Ltd.
624a8d46aSMatthew Dillon  *
724a8d46aSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
824a8d46aSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
924a8d46aSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
1024a8d46aSMatthew Dillon  *
1124a8d46aSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1224a8d46aSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1324a8d46aSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1424a8d46aSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1524a8d46aSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1624a8d46aSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1724a8d46aSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1824a8d46aSMatthew Dillon  */
1924a8d46aSMatthew Dillon 
2024a8d46aSMatthew Dillon /*-
2124a8d46aSMatthew Dillon  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
2224a8d46aSMatthew Dillon  * which were used as the reference documentation for this implementation.
2324a8d46aSMatthew Dillon  *
2424a8d46aSMatthew Dillon  * Driver version we are currently based off of is
2524a8d46aSMatthew Dillon  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
2624a8d46aSMatthew Dillon  *
2724a8d46aSMatthew Dillon  ***********************************************************************
2824a8d46aSMatthew Dillon  *
2924a8d46aSMatthew Dillon  * This file is provided under a dual BSD/GPLv2 license.  When using or
3024a8d46aSMatthew Dillon  * redistributing this file, you may do so under either license.
3124a8d46aSMatthew Dillon  *
3224a8d46aSMatthew Dillon  * GPL LICENSE SUMMARY
3324a8d46aSMatthew Dillon  *
3424a8d46aSMatthew Dillon  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
3524a8d46aSMatthew Dillon  *
3624a8d46aSMatthew Dillon  * This program is free software; you can redistribute it and/or modify
3724a8d46aSMatthew Dillon  * it under the terms of version 2 of the GNU General Public License as
3824a8d46aSMatthew Dillon  * published by the Free Software Foundation.
3924a8d46aSMatthew Dillon  *
4024a8d46aSMatthew Dillon  * This program is distributed in the hope that it will be useful, but
4124a8d46aSMatthew Dillon  * WITHOUT ANY WARRANTY; without even the implied warranty of
4224a8d46aSMatthew Dillon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4324a8d46aSMatthew Dillon  * General Public License for more details.
4424a8d46aSMatthew Dillon  *
4524a8d46aSMatthew Dillon  * You should have received a copy of the GNU General Public License
4624a8d46aSMatthew Dillon  * along with this program; if not, write to the Free Software
4724a8d46aSMatthew Dillon  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
4824a8d46aSMatthew Dillon  * USA
4924a8d46aSMatthew Dillon  *
5024a8d46aSMatthew Dillon  * The full GNU General Public License is included in this distribution
5124a8d46aSMatthew Dillon  * in the file called COPYING.
5224a8d46aSMatthew Dillon  *
5324a8d46aSMatthew Dillon  * Contact Information:
5424a8d46aSMatthew Dillon  *  Intel Linux Wireless <ilw@linux.intel.com>
5524a8d46aSMatthew Dillon  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
5624a8d46aSMatthew Dillon  *
5724a8d46aSMatthew Dillon  *
5824a8d46aSMatthew Dillon  * BSD LICENSE
5924a8d46aSMatthew Dillon  *
6024a8d46aSMatthew Dillon  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
6124a8d46aSMatthew Dillon  * All rights reserved.
6224a8d46aSMatthew Dillon  *
6324a8d46aSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
6424a8d46aSMatthew Dillon  * modification, are permitted provided that the following conditions
6524a8d46aSMatthew Dillon  * are met:
6624a8d46aSMatthew Dillon  *
6724a8d46aSMatthew Dillon  *  * Redistributions of source code must retain the above copyright
6824a8d46aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
6924a8d46aSMatthew Dillon  *  * Redistributions in binary form must reproduce the above copyright
7024a8d46aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
7124a8d46aSMatthew Dillon  *    the documentation and/or other materials provided with the
7224a8d46aSMatthew Dillon  *    distribution.
7324a8d46aSMatthew Dillon  *  * Neither the name Intel Corporation nor the names of its
7424a8d46aSMatthew Dillon  *    contributors may be used to endorse or promote products derived
7524a8d46aSMatthew Dillon  *    from this software without specific prior written permission.
7624a8d46aSMatthew Dillon  *
7724a8d46aSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7824a8d46aSMatthew Dillon  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7924a8d46aSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8024a8d46aSMatthew Dillon  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8124a8d46aSMatthew Dillon  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8224a8d46aSMatthew Dillon  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8324a8d46aSMatthew Dillon  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8424a8d46aSMatthew Dillon  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8524a8d46aSMatthew Dillon  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8624a8d46aSMatthew Dillon  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8724a8d46aSMatthew Dillon  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8824a8d46aSMatthew Dillon  */
8924a8d46aSMatthew Dillon 
9024a8d46aSMatthew Dillon /*-
9124a8d46aSMatthew Dillon  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
9224a8d46aSMatthew Dillon  *
9324a8d46aSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
9424a8d46aSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
9524a8d46aSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
9624a8d46aSMatthew Dillon  *
9724a8d46aSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9824a8d46aSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9924a8d46aSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10024a8d46aSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
10124a8d46aSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
10224a8d46aSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
10324a8d46aSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
10424a8d46aSMatthew Dillon  */
10524a8d46aSMatthew Dillon #include <sys/param.h>
10624a8d46aSMatthew Dillon #include <sys/bus.h>
1076acbba79SMatthew Dillon #include <sys/conf.h>
10824a8d46aSMatthew Dillon #include <sys/endian.h>
10924a8d46aSMatthew Dillon #include <sys/firmware.h>
11024a8d46aSMatthew Dillon #include <sys/kernel.h>
11124a8d46aSMatthew Dillon #include <sys/malloc.h>
11224a8d46aSMatthew Dillon #include <sys/mbuf.h>
11324a8d46aSMatthew Dillon #include <sys/rman.h>
11424a8d46aSMatthew Dillon #include <sys/sysctl.h>
11524a8d46aSMatthew Dillon #include <sys/linker.h>
11624a8d46aSMatthew Dillon 
11724a8d46aSMatthew Dillon #include <machine/endian.h>
11824a8d46aSMatthew Dillon 
11945bc40b1SMatthew Dillon #include <bus/pci/pcivar.h>
12045bc40b1SMatthew Dillon #include <bus/pci/pcireg.h>
12124a8d46aSMatthew Dillon 
12224a8d46aSMatthew Dillon #include <net/bpf.h>
12324a8d46aSMatthew Dillon 
12424a8d46aSMatthew Dillon #include <net/if.h>
12524a8d46aSMatthew Dillon #include <net/if_var.h>
12624a8d46aSMatthew Dillon #include <net/if_arp.h>
12724a8d46aSMatthew Dillon #include <net/if_dl.h>
12824a8d46aSMatthew Dillon #include <net/if_media.h>
12924a8d46aSMatthew Dillon #include <net/if_types.h>
13024a8d46aSMatthew Dillon 
13124a8d46aSMatthew Dillon #include <netinet/in.h>
13224a8d46aSMatthew Dillon #include <netinet/in_systm.h>
13324a8d46aSMatthew Dillon #include <netinet/if_ether.h>
13424a8d46aSMatthew Dillon #include <netinet/ip.h>
13524a8d46aSMatthew Dillon 
13645bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_var.h>
13745bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_regdomain.h>
13845bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_ratectl.h>
13945bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_radiotap.h>
14024a8d46aSMatthew Dillon 
14145bc40b1SMatthew Dillon #include "if_iwmreg.h"
14245bc40b1SMatthew Dillon #include "if_iwmvar.h"
14345bc40b1SMatthew Dillon #include "if_iwm_debug.h"
14445bc40b1SMatthew Dillon #include "if_iwm_util.h"
145706a3044SImre Vadász #include "if_iwm_notif_wait.h"
14645bc40b1SMatthew Dillon #include "if_iwm_pcie_trans.h"
14745bc40b1SMatthew Dillon #include "if_iwm_time_event.h"
14824a8d46aSMatthew Dillon 
149e8cb7158SImre Vadász #define TU_TO_HZ(tu)	(((uint64_t)(tu) * 1024 * hz) / 1000000)
150e8cb7158SImre Vadász 
151e8cb7158SImre Vadász static void
iwm_te_clear_data(struct iwm_softc * sc)1526acbba79SMatthew Dillon iwm_te_clear_data(struct iwm_softc *sc)
153e8cb7158SImre Vadász {
154e8cb7158SImre Vadász 	sc->sc_time_event_uid = 0;
155e8cb7158SImre Vadász 	sc->sc_time_event_duration = 0;
156e8cb7158SImre Vadász 	sc->sc_time_event_end_ticks = 0;
157e8cb7158SImre Vadász 	sc->sc_flags &= ~IWM_FLAG_TE_ACTIVE;
158e8cb7158SImre Vadász }
159e8cb7158SImre Vadász 
16024a8d46aSMatthew Dillon /*
161e8cb7158SImre Vadász  * Handles a FW notification for an event that is known to the driver.
162e8cb7158SImre Vadász  *
163e8cb7158SImre Vadász  * @mvm: the mvm component
164e8cb7158SImre Vadász  * @te_data: the time event data
165e8cb7158SImre Vadász  * @notif: the notification data corresponding the time event data.
16624a8d46aSMatthew Dillon  */
167e8cb7158SImre Vadász static void
iwm_te_handle_notif(struct iwm_softc * sc,struct iwm_time_event_notif * notif)1686acbba79SMatthew Dillon iwm_te_handle_notif(struct iwm_softc *sc,
169e8cb7158SImre Vadász     struct iwm_time_event_notif *notif)
170e8cb7158SImre Vadász {
171e8cb7158SImre Vadász 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
172e8cb7158SImre Vadász 	    "Handle time event notif - UID = 0x%x action %d\n",
173e8cb7158SImre Vadász 	    le32toh(notif->unique_id),
174e8cb7158SImre Vadász 	    le32toh(notif->action));
175e8cb7158SImre Vadász 
176e8cb7158SImre Vadász 	if (!le32toh(notif->status)) {
177*4cbc7cf9SMichael Neumann #ifdef IWM_DEBUG
178e8cb7158SImre Vadász 		const char *msg;
179e8cb7158SImre Vadász 
180e8cb7158SImre Vadász 		if (notif->action & htole32(IWM_TE_V2_NOTIF_HOST_EVENT_START))
181e8cb7158SImre Vadász 			msg = "Time Event start notification failure";
182e8cb7158SImre Vadász 		else
183e8cb7158SImre Vadász 			msg = "Time Event end notification failure";
184e8cb7158SImre Vadász 
185e8cb7158SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_TE, "%s\n", msg);
186*4cbc7cf9SMichael Neumann #endif
187e8cb7158SImre Vadász 	}
188e8cb7158SImre Vadász 
189e8cb7158SImre Vadász 	if (le32toh(notif->action) & IWM_TE_V2_NOTIF_HOST_EVENT_END) {
190e8cb7158SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
191e8cb7158SImre Vadász 		    "TE ended - current time %d, estimated end %d\n",
192e8cb7158SImre Vadász 		    ticks, sc->sc_time_event_end_ticks);
193e8cb7158SImre Vadász 
1946acbba79SMatthew Dillon 		iwm_te_clear_data(sc);
195e8cb7158SImre Vadász 	} else if (le32toh(notif->action) & IWM_TE_V2_NOTIF_HOST_EVENT_START) {
196e8cb7158SImre Vadász 		sc->sc_time_event_end_ticks =
197e8cb7158SImre Vadász 		    ticks + TU_TO_HZ(sc->sc_time_event_duration);
198e8cb7158SImre Vadász 	} else {
199e8cb7158SImre Vadász 		device_printf(sc->sc_dev, "Got TE with unknown action\n");
200e8cb7158SImre Vadász 	}
201e8cb7158SImre Vadász }
202e8cb7158SImre Vadász 
203e8cb7158SImre Vadász /*
204e8cb7158SImre Vadász  * The Rx handler for time event notifications
205e8cb7158SImre Vadász  */
206e8cb7158SImre Vadász void
iwm_rx_time_event_notif(struct iwm_softc * sc,struct iwm_rx_packet * pkt)2076acbba79SMatthew Dillon iwm_rx_time_event_notif(struct iwm_softc *sc, struct iwm_rx_packet *pkt)
208e8cb7158SImre Vadász {
209e8cb7158SImre Vadász 	struct iwm_time_event_notif *notif = (void *)pkt->data;
210e8cb7158SImre Vadász 
211e8cb7158SImre Vadász 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
212e8cb7158SImre Vadász 	    "Time event notification - UID = 0x%x action %d\n",
213e8cb7158SImre Vadász 	    le32toh(notif->unique_id),
214e8cb7158SImre Vadász 	    le32toh(notif->action));
215e8cb7158SImre Vadász 
2166acbba79SMatthew Dillon 	iwm_te_handle_notif(sc, notif);
217e8cb7158SImre Vadász }
21824a8d46aSMatthew Dillon 
21924a8d46aSMatthew Dillon static int
iwm_te_notif(struct iwm_softc * sc,struct iwm_rx_packet * pkt,void * data)2206acbba79SMatthew Dillon iwm_te_notif(struct iwm_softc *sc, struct iwm_rx_packet *pkt,
221706a3044SImre Vadász     void *data)
222706a3044SImre Vadász {
223706a3044SImre Vadász 	struct iwm_time_event_notif *resp;
224706a3044SImre Vadász 	int resp_len = iwm_rx_packet_payload_len(pkt);
225706a3044SImre Vadász 
226706a3044SImre Vadász 	if (pkt->hdr.code != IWM_TIME_EVENT_NOTIFICATION ||
227706a3044SImre Vadász 	    resp_len != sizeof(*resp)) {
228706a3044SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
229706a3044SImre Vadász 		    "Invalid TIME_EVENT_NOTIFICATION response\n");
230706a3044SImre Vadász 		return 1;
231706a3044SImre Vadász 	}
232706a3044SImre Vadász 
233706a3044SImre Vadász 	resp = (void *)pkt->data;
234706a3044SImre Vadász 
235706a3044SImre Vadász 	/* te_data->uid is already set in the TIME_EVENT_CMD response */
236706a3044SImre Vadász 	if (le32toh(resp->unique_id) != sc->sc_time_event_uid)
237706a3044SImre Vadász 		return false;
238706a3044SImre Vadász 
239706a3044SImre Vadász 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
240706a3044SImre Vadász 	    "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
241706a3044SImre Vadász 	    sc->sc_time_event_uid);
242706a3044SImre Vadász 	if (!resp->status) {
243706a3044SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
244706a3044SImre Vadász 		    "TIME_EVENT_NOTIFICATION received but not executed\n");
245706a3044SImre Vadász 	}
246706a3044SImre Vadász 
247706a3044SImre Vadász 	return 1;
248706a3044SImre Vadász }
249706a3044SImre Vadász 
250706a3044SImre Vadász static int
iwm_time_event_response(struct iwm_softc * sc,struct iwm_rx_packet * pkt,void * data)2516acbba79SMatthew Dillon iwm_time_event_response(struct iwm_softc *sc, struct iwm_rx_packet *pkt,
252706a3044SImre Vadász     void *data)
253706a3044SImre Vadász {
254706a3044SImre Vadász 	struct iwm_time_event_resp *resp;
255706a3044SImre Vadász 	int resp_len = iwm_rx_packet_payload_len(pkt);
256706a3044SImre Vadász 
257706a3044SImre Vadász 	if (pkt->hdr.code != IWM_TIME_EVENT_CMD ||
258706a3044SImre Vadász 	    resp_len != sizeof(*resp)) {
259706a3044SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
260706a3044SImre Vadász 		    "Invalid TIME_EVENT_CMD response\n");
261706a3044SImre Vadász 		return 1;
262706a3044SImre Vadász 	}
263706a3044SImre Vadász 
264706a3044SImre Vadász 	resp = (void *)pkt->data;
265706a3044SImre Vadász 
266706a3044SImre Vadász 	/* we should never get a response to another TIME_EVENT_CMD here */
267706a3044SImre Vadász 	if (le32toh(resp->id) != IWM_TE_BSS_STA_AGGRESSIVE_ASSOC) {
268706a3044SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
269706a3044SImre Vadász 		    "Got TIME_EVENT_CMD response with wrong id: %d\n",
270706a3044SImre Vadász 		    le32toh(resp->id));
271706a3044SImre Vadász 		return 0;
272706a3044SImre Vadász 	}
273706a3044SImre Vadász 
274706a3044SImre Vadász 	sc->sc_time_event_uid = le32toh(resp->unique_id);
275706a3044SImre Vadász 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
276706a3044SImre Vadász 	    "TIME_EVENT_CMD response - UID = 0x%x\n", sc->sc_time_event_uid);
277706a3044SImre Vadász 	return 1;
278706a3044SImre Vadász }
279706a3044SImre Vadász 
280706a3044SImre Vadász 
281706a3044SImre Vadász /* XXX Use the te_data function argument properly, like in iwlwifi's code. */
282706a3044SImre Vadász 
283706a3044SImre Vadász static int
iwm_time_event_send_add(struct iwm_softc * sc,struct iwm_vap * ivp,void * te_data,struct iwm_time_event_cmd * te_cmd)2846acbba79SMatthew Dillon iwm_time_event_send_add(struct iwm_softc *sc, struct iwm_vap *ivp,
285d078c812SImre Vadász 	void *te_data, struct iwm_time_event_cmd *te_cmd)
28624a8d46aSMatthew Dillon {
287706a3044SImre Vadász 	static const uint16_t time_event_response[] = { IWM_TIME_EVENT_CMD };
288706a3044SImre Vadász 	struct iwm_notification_wait wait_time_event;
28924a8d46aSMatthew Dillon 	int ret;
29024a8d46aSMatthew Dillon 
291706a3044SImre Vadász 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
29224a8d46aSMatthew Dillon 	    "Add new TE, duration %d TU\n", le32toh(te_cmd->duration));
29324a8d46aSMatthew Dillon 
294e8cb7158SImre Vadász 	sc->sc_time_event_duration = le32toh(te_cmd->duration);
295e8cb7158SImre Vadász 
296706a3044SImre Vadász 	/*
297706a3044SImre Vadász 	 * Use a notification wait, which really just processes the
298706a3044SImre Vadász 	 * command response and doesn't wait for anything, in order
299706a3044SImre Vadász 	 * to be able to process the response and get the UID inside
300706a3044SImre Vadász 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
301706a3044SImre Vadász 	 * stores the buffer and then wakes up this thread, by which
302706a3044SImre Vadász 	 * time another notification (that the time event started)
303706a3044SImre Vadász 	 * might already be processed unsuccessfully.
304706a3044SImre Vadász 	 */
305706a3044SImre Vadász 	iwm_init_notification_wait(sc->sc_notif_wait, &wait_time_event,
306706a3044SImre Vadász 				   time_event_response,
3076acbba79SMatthew Dillon 				   nitems(time_event_response),
3086acbba79SMatthew Dillon 				   iwm_time_event_response, /*te_data*/NULL);
309706a3044SImre Vadász 
3106acbba79SMatthew Dillon 	ret = iwm_send_cmd_pdu(sc, IWM_TIME_EVENT_CMD, 0, sizeof(*te_cmd),
311706a3044SImre Vadász 	    te_cmd);
31224a8d46aSMatthew Dillon 	if (ret) {
313706a3044SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
31424a8d46aSMatthew Dillon 		    "%s: Couldn't send IWM_TIME_EVENT_CMD: %d\n",
31524a8d46aSMatthew Dillon 		    __func__, ret);
316706a3044SImre Vadász 		iwm_remove_notification(sc->sc_notif_wait, &wait_time_event);
317706a3044SImre Vadász 		return ret;
318706a3044SImre Vadász 	}
319706a3044SImre Vadász 
320706a3044SImre Vadász 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
321706a3044SImre Vadász 	IWM_UNLOCK(sc);
322706a3044SImre Vadász 	ret = iwm_wait_notification(sc->sc_notif_wait, &wait_time_event, 1);
323706a3044SImre Vadász 	IWM_LOCK(sc);
324706a3044SImre Vadász 	/* should never fail */
325706a3044SImre Vadász 	if (ret) {
326706a3044SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
327706a3044SImre Vadász 		    "%s: Failed to get response for IWM_TIME_EVENT_CMD: %d\n",
328706a3044SImre Vadász 		    __func__, ret);
32924a8d46aSMatthew Dillon 	}
33024a8d46aSMatthew Dillon 
33124a8d46aSMatthew Dillon 	return ret;
33224a8d46aSMatthew Dillon }
33324a8d46aSMatthew Dillon 
33424a8d46aSMatthew Dillon void
iwm_protect_session(struct iwm_softc * sc,struct iwm_vap * ivp,uint32_t duration,uint32_t max_delay,boolean_t wait_for_notif)3356acbba79SMatthew Dillon iwm_protect_session(struct iwm_softc *sc, struct iwm_vap *ivp,
336706a3044SImre Vadász 	uint32_t duration, uint32_t max_delay, boolean_t wait_for_notif)
33724a8d46aSMatthew Dillon {
338706a3044SImre Vadász 	const uint16_t te_notif_response[] = { IWM_TIME_EVENT_NOTIFICATION };
339706a3044SImre Vadász 	struct iwm_notification_wait wait_te_notif;
340d078c812SImre Vadász 	struct iwm_time_event_cmd time_cmd = {};
34124a8d46aSMatthew Dillon 
342706a3044SImre Vadász 	/* Do nothing if a time event is already scheduled. */
343706a3044SImre Vadász 	if (sc->sc_flags & IWM_FLAG_TE_ACTIVE)
344706a3044SImre Vadász 		return;
345706a3044SImre Vadász 
34624a8d46aSMatthew Dillon 	time_cmd.action = htole32(IWM_FW_CTXT_ACTION_ADD);
34724a8d46aSMatthew Dillon 	time_cmd.id_and_color =
348f16ef749SImre Vadász 	    htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id, ivp->color));
34924a8d46aSMatthew Dillon 	time_cmd.id = htole32(IWM_TE_BSS_STA_AGGRESSIVE_ASSOC);
35024a8d46aSMatthew Dillon 
351edfc8a07SImre Vadász 	time_cmd.apply_time = htole32(0);
35224a8d46aSMatthew Dillon 
35324a8d46aSMatthew Dillon 	time_cmd.max_frags = IWM_TE_V2_FRAG_NONE;
35424a8d46aSMatthew Dillon 	time_cmd.max_delay = htole32(max_delay);
35524a8d46aSMatthew Dillon 	/* TODO: why do we need to interval = bi if it is not periodic? */
35624a8d46aSMatthew Dillon 	time_cmd.interval = htole32(1);
35724a8d46aSMatthew Dillon 	time_cmd.duration = htole32(duration);
35824a8d46aSMatthew Dillon 	time_cmd.repeat = 1;
3596acbba79SMatthew Dillon 	time_cmd.policy
3606acbba79SMatthew Dillon 	    = htole16(IWM_TE_V2_NOTIF_HOST_EVENT_START |
361edfc8a07SImre Vadász 	        IWM_TE_V2_NOTIF_HOST_EVENT_END |
362edfc8a07SImre Vadász 		IWM_T2_V2_START_IMMEDIATELY);
36324a8d46aSMatthew Dillon 
364706a3044SImre Vadász 	if (!wait_for_notif) {
3656acbba79SMatthew Dillon 		iwm_time_event_send_add(sc, ivp, /*te_data*/NULL, &time_cmd);
366706a3044SImre Vadász 		DELAY(100);
367706a3044SImre Vadász 		sc->sc_flags |= IWM_FLAG_TE_ACTIVE;
368706a3044SImre Vadász 		return;
369706a3044SImre Vadász 	}
370706a3044SImre Vadász 
371706a3044SImre Vadász 	/*
372706a3044SImre Vadász 	 * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
373706a3044SImre Vadász 	 * right after we send the time event
374706a3044SImre Vadász 	 */
375706a3044SImre Vadász 	iwm_init_notification_wait(sc->sc_notif_wait, &wait_te_notif,
3766acbba79SMatthew Dillon 	    te_notif_response, nitems(te_notif_response),
3776acbba79SMatthew Dillon 	    iwm_te_notif, /*te_data*/NULL);
378706a3044SImre Vadász 
379706a3044SImre Vadász 	/* If TE was sent OK - wait for the notification that started */
3806acbba79SMatthew Dillon 	if (iwm_time_event_send_add(sc, ivp, /*te_data*/NULL, &time_cmd)) {
381706a3044SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
382706a3044SImre Vadász 		    "%s: Failed to add TE to protect session\n", __func__);
383706a3044SImre Vadász 		iwm_remove_notification(sc->sc_notif_wait, &wait_te_notif);
384706a3044SImre Vadász 	} else {
385706a3044SImre Vadász 		sc->sc_flags |= IWM_FLAG_TE_ACTIVE;
386706a3044SImre Vadász 		IWM_UNLOCK(sc);
387706a3044SImre Vadász 		if (iwm_wait_notification(sc->sc_notif_wait, &wait_te_notif,
388706a3044SImre Vadász 		    TU_TO_HZ(max_delay))) {
389706a3044SImre Vadász 			IWM_DPRINTF(sc, IWM_DEBUG_TE,
390706a3044SImre Vadász 			    "%s: Failed to protect session until TE\n",
391706a3044SImre Vadász 			    __func__);
392706a3044SImre Vadász 		}
393706a3044SImre Vadász 		IWM_LOCK(sc);
394706a3044SImre Vadász 	}
395706a3044SImre Vadász }
396706a3044SImre Vadász 
397706a3044SImre Vadász void
iwm_stop_session_protection(struct iwm_softc * sc,struct iwm_vap * ivp)3986acbba79SMatthew Dillon iwm_stop_session_protection(struct iwm_softc *sc, struct iwm_vap *ivp)
399706a3044SImre Vadász {
400706a3044SImre Vadász 	struct iwm_time_event_cmd time_cmd = {};
401706a3044SImre Vadász 
402706a3044SImre Vadász 	/* Do nothing if the time event has already ended. */
403706a3044SImre Vadász 	if ((sc->sc_flags & IWM_FLAG_TE_ACTIVE) == 0)
404706a3044SImre Vadász 		return;
405706a3044SImre Vadász 
406706a3044SImre Vadász 	time_cmd.id = htole32(sc->sc_time_event_uid);
407706a3044SImre Vadász 	time_cmd.action = htole32(IWM_FW_CTXT_ACTION_REMOVE);
408706a3044SImre Vadász 	time_cmd.id_and_color =
409706a3044SImre Vadász 	    htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id, ivp->color));
410706a3044SImre Vadász 
411706a3044SImre Vadász 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
412706a3044SImre Vadász 	    "%s: Removing TE 0x%x\n", __func__, le32toh(time_cmd.id));
4136acbba79SMatthew Dillon 	if (iwm_send_cmd_pdu(sc, IWM_TIME_EVENT_CMD, 0, sizeof(time_cmd),
414706a3044SImre Vadász 	    &time_cmd) == 0)
4156acbba79SMatthew Dillon 		iwm_te_clear_data(sc);
416706a3044SImre Vadász 
417706a3044SImre Vadász 	DELAY(100);
41824a8d46aSMatthew Dillon }
419