xref: /freebsd/sys/dev/iwm/if_iwm_time_event.c (revision 9768746b)
1 /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
2 
3 /*
4  * Copyright (c) 2014 genua mbh <info@genua.de>
5  * Copyright (c) 2014 Fixup Software Ltd.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*-
21  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22  * which were used as the reference documentation for this implementation.
23  *
24  * Driver version we are currently based off of is
25  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
26  *
27  ***********************************************************************
28  *
29  * This file is provided under a dual BSD/GPLv2 license.  When using or
30  * redistributing this file, you may do so under either license.
31  *
32  * GPL LICENSE SUMMARY
33  *
34  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
35  *
36  * This program is free software; you can redistribute it and/or modify
37  * it under the terms of version 2 of the GNU General Public License as
38  * published by the Free Software Foundation.
39  *
40  * This program is distributed in the hope that it will be useful, but
41  * WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43  * General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
48  * USA
49  *
50  * The full GNU General Public License is included in this distribution
51  * in the file called COPYING.
52  *
53  * Contact Information:
54  *  Intel Linux Wireless <ilw@linux.intel.com>
55  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56  *
57  *
58  * BSD LICENSE
59  *
60  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61  * All rights reserved.
62  *
63  * Redistribution and use in source and binary forms, with or without
64  * modification, are permitted provided that the following conditions
65  * are met:
66  *
67  *  * Redistributions of source code must retain the above copyright
68  *    notice, this list of conditions and the following disclaimer.
69  *  * Redistributions in binary form must reproduce the above copyright
70  *    notice, this list of conditions and the following disclaimer in
71  *    the documentation and/or other materials provided with the
72  *    distribution.
73  *  * Neither the name Intel Corporation nor the names of its
74  *    contributors may be used to endorse or promote products derived
75  *    from this software without specific prior written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88  */
89 
90 /*-
91  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
92  *
93  * Permission to use, copy, modify, and distribute this software for any
94  * purpose with or without fee is hereby granted, provided that the above
95  * copyright notice and this permission notice appear in all copies.
96  *
97  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
99  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
100  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
101  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
102  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
103  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
104  */
105 #include <sys/cdefs.h>
106 __FBSDID("$FreeBSD$");
107 
108 #include "opt_wlan.h"
109 #include "opt_iwm.h"
110 
111 #include <sys/param.h>
112 #include <sys/bus.h>
113 #include <sys/conf.h>
114 #include <sys/endian.h>
115 #include <sys/firmware.h>
116 #include <sys/kernel.h>
117 #include <sys/malloc.h>
118 #include <sys/mbuf.h>
119 #include <sys/mutex.h>
120 #include <sys/module.h>
121 #include <sys/proc.h>
122 #include <sys/rman.h>
123 #include <sys/socket.h>
124 #include <sys/sockio.h>
125 #include <sys/sysctl.h>
126 #include <sys/linker.h>
127 
128 #include <machine/bus.h>
129 #include <machine/endian.h>
130 #include <machine/resource.h>
131 
132 #include <dev/pci/pcivar.h>
133 #include <dev/pci/pcireg.h>
134 
135 #include <net/bpf.h>
136 
137 #include <net/if.h>
138 #include <net/if_var.h>
139 #include <net/if_arp.h>
140 #include <net/if_dl.h>
141 #include <net/if_media.h>
142 #include <net/if_types.h>
143 
144 #include <netinet/in.h>
145 #include <netinet/in_systm.h>
146 #include <netinet/if_ether.h>
147 #include <netinet/ip.h>
148 
149 #include <net80211/ieee80211_var.h>
150 #include <net80211/ieee80211_regdomain.h>
151 #include <net80211/ieee80211_ratectl.h>
152 #include <net80211/ieee80211_radiotap.h>
153 
154 #include <dev/iwm/if_iwmreg.h>
155 #include <dev/iwm/if_iwmvar.h>
156 #include <dev/iwm/if_iwm_debug.h>
157 #include <dev/iwm/if_iwm_util.h>
158 #include <dev/iwm/if_iwm_notif_wait.h>
159 #include <dev/iwm/if_iwm_pcie_trans.h>
160 #include <dev/iwm/if_iwm_time_event.h>
161 
162 #define TU_TO_HZ(tu)	(((uint64_t)(tu) * 1024 * hz) / 1000000)
163 
164 static void
165 iwm_te_clear_data(struct iwm_softc *sc)
166 {
167 	sc->sc_time_event_uid = 0;
168 	sc->sc_time_event_duration = 0;
169 	sc->sc_time_event_end_ticks = 0;
170 	sc->sc_flags &= ~IWM_FLAG_TE_ACTIVE;
171 }
172 
173 /*
174  * Handles a FW notification for an event that is known to the driver.
175  *
176  * @mvm: the mvm component
177  * @te_data: the time event data
178  * @notif: the notification data corresponding the time event data.
179  */
180 static void
181 iwm_te_handle_notif(struct iwm_softc *sc,
182     struct iwm_time_event_notif *notif)
183 {
184 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
185 	    "Handle time event notif - UID = 0x%x action %d\n",
186 	    le32toh(notif->unique_id),
187 	    le32toh(notif->action));
188 
189 	if (!le32toh(notif->status)) {
190 #ifdef IWM_DEBUG
191 		const char *msg;
192 
193 		if (notif->action & htole32(IWM_TE_V2_NOTIF_HOST_EVENT_START))
194 			msg = "Time Event start notification failure";
195 		else
196 			msg = "Time Event end notification failure";
197 
198 		IWM_DPRINTF(sc, IWM_DEBUG_TE, "%s\n", msg);
199 #endif
200 	}
201 
202 	if (le32toh(notif->action) & IWM_TE_V2_NOTIF_HOST_EVENT_END) {
203 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
204 		    "TE ended - current time %d, estimated end %d\n",
205 		    ticks, sc->sc_time_event_end_ticks);
206 
207 		iwm_te_clear_data(sc);
208 	} else if (le32toh(notif->action) & IWM_TE_V2_NOTIF_HOST_EVENT_START) {
209 		sc->sc_time_event_end_ticks =
210 		    ticks + TU_TO_HZ(sc->sc_time_event_duration);
211 	} else {
212 		device_printf(sc->sc_dev, "Got TE with unknown action\n");
213 	}
214 }
215 
216 /*
217  * The Rx handler for time event notifications
218  */
219 void
220 iwm_rx_time_event_notif(struct iwm_softc *sc, struct iwm_rx_packet *pkt)
221 {
222 	struct iwm_time_event_notif *notif = (void *)pkt->data;
223 
224 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
225 	    "Time event notification - UID = 0x%x action %d\n",
226 	    le32toh(notif->unique_id),
227 	    le32toh(notif->action));
228 
229 	iwm_te_handle_notif(sc, notif);
230 }
231 
232 static int
233 iwm_te_notif(struct iwm_softc *sc, struct iwm_rx_packet *pkt,
234     void *data)
235 {
236 	struct iwm_time_event_notif *resp;
237 	int resp_len = iwm_rx_packet_payload_len(pkt);
238 
239 	if (pkt->hdr.code != IWM_TIME_EVENT_NOTIFICATION ||
240 	    resp_len != sizeof(*resp)) {
241 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
242 		    "Invalid TIME_EVENT_NOTIFICATION response\n");
243 		return 1;
244 	}
245 
246 	resp = (void *)pkt->data;
247 
248 	/* te_data->uid is already set in the TIME_EVENT_CMD response */
249 	if (le32toh(resp->unique_id) != sc->sc_time_event_uid)
250 		return false;
251 
252 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
253 	    "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n",
254 	    sc->sc_time_event_uid);
255 	if (!resp->status) {
256 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
257 		    "TIME_EVENT_NOTIFICATION received but not executed\n");
258 	}
259 
260 	return 1;
261 }
262 
263 static int
264 iwm_time_event_response(struct iwm_softc *sc, struct iwm_rx_packet *pkt,
265     void *data)
266 {
267 	struct iwm_time_event_resp *resp;
268 	int resp_len = iwm_rx_packet_payload_len(pkt);
269 
270 	if (pkt->hdr.code != IWM_TIME_EVENT_CMD ||
271 	    resp_len != sizeof(*resp)) {
272 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
273 		    "Invalid TIME_EVENT_CMD response\n");
274 		return 1;
275 	}
276 
277 	resp = (void *)pkt->data;
278 
279 	/* we should never get a response to another TIME_EVENT_CMD here */
280 	if (le32toh(resp->id) != IWM_TE_BSS_STA_AGGRESSIVE_ASSOC) {
281 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
282 		    "Got TIME_EVENT_CMD response with wrong id: %d\n",
283 		    le32toh(resp->id));
284 		return 0;
285 	}
286 
287 	sc->sc_time_event_uid = le32toh(resp->unique_id);
288 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
289 	    "TIME_EVENT_CMD response - UID = 0x%x\n", sc->sc_time_event_uid);
290 	return 1;
291 }
292 
293 
294 /* XXX Use the te_data function argument properly, like in iwlwifi's code. */
295 
296 static int
297 iwm_time_event_send_add(struct iwm_softc *sc, struct iwm_vap *ivp,
298 	void *te_data, struct iwm_time_event_cmd *te_cmd)
299 {
300 	static const uint16_t time_event_response[] = { IWM_TIME_EVENT_CMD };
301 	struct iwm_notification_wait wait_time_event;
302 	int ret;
303 
304 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
305 	    "Add new TE, duration %d TU\n", le32toh(te_cmd->duration));
306 
307 	sc->sc_time_event_duration = le32toh(te_cmd->duration);
308 
309 	/*
310 	 * Use a notification wait, which really just processes the
311 	 * command response and doesn't wait for anything, in order
312 	 * to be able to process the response and get the UID inside
313 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
314 	 * stores the buffer and then wakes up this thread, by which
315 	 * time another notification (that the time event started)
316 	 * might already be processed unsuccessfully.
317 	 */
318 	iwm_init_notification_wait(sc->sc_notif_wait, &wait_time_event,
319 				   time_event_response,
320 				   nitems(time_event_response),
321 				   iwm_time_event_response, /*te_data*/NULL);
322 
323 	ret = iwm_send_cmd_pdu(sc, IWM_TIME_EVENT_CMD, 0, sizeof(*te_cmd),
324 	    te_cmd);
325 	if (ret) {
326 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
327 		    "%s: Couldn't send IWM_TIME_EVENT_CMD: %d\n",
328 		    __func__, ret);
329 		iwm_remove_notification(sc->sc_notif_wait, &wait_time_event);
330 		return ret;
331 	}
332 
333 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
334 	IWM_UNLOCK(sc);
335 	ret = iwm_wait_notification(sc->sc_notif_wait, &wait_time_event, 1);
336 	IWM_LOCK(sc);
337 	/* should never fail */
338 	if (ret) {
339 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
340 		    "%s: Failed to get response for IWM_TIME_EVENT_CMD: %d\n",
341 		    __func__, ret);
342 	}
343 
344 	return ret;
345 }
346 
347 void
348 iwm_protect_session(struct iwm_softc *sc, struct iwm_vap *ivp,
349 	uint32_t duration, uint32_t max_delay, boolean_t wait_for_notif)
350 {
351 	const uint16_t te_notif_response[] = { IWM_TIME_EVENT_NOTIFICATION };
352 	struct iwm_notification_wait wait_te_notif;
353 	struct iwm_time_event_cmd time_cmd = {};
354 
355 	/* Do nothing if a time event is already scheduled. */
356 	if (sc->sc_flags & IWM_FLAG_TE_ACTIVE)
357 		return;
358 
359 	time_cmd.action = htole32(IWM_FW_CTXT_ACTION_ADD);
360 	time_cmd.id_and_color =
361 	    htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id, ivp->color));
362 	time_cmd.id = htole32(IWM_TE_BSS_STA_AGGRESSIVE_ASSOC);
363 
364 	time_cmd.apply_time = htole32(0);
365 
366 	time_cmd.max_frags = IWM_TE_V2_FRAG_NONE;
367 	time_cmd.max_delay = htole32(max_delay);
368 	/* TODO: why do we need to interval = bi if it is not periodic? */
369 	time_cmd.interval = htole32(1);
370 	time_cmd.duration = htole32(duration);
371 	time_cmd.repeat = 1;
372 	time_cmd.policy
373 	    = htole16(IWM_TE_V2_NOTIF_HOST_EVENT_START |
374 	        IWM_TE_V2_NOTIF_HOST_EVENT_END |
375 		IWM_T2_V2_START_IMMEDIATELY);
376 
377 	if (!wait_for_notif) {
378 		iwm_time_event_send_add(sc, ivp, /*te_data*/NULL, &time_cmd);
379 		DELAY(100);
380 		sc->sc_flags |= IWM_FLAG_TE_ACTIVE;
381 		return;
382 	}
383 
384 	/*
385 	 * Create notification_wait for the TIME_EVENT_NOTIFICATION to use
386 	 * right after we send the time event
387 	 */
388 	iwm_init_notification_wait(sc->sc_notif_wait, &wait_te_notif,
389 	    te_notif_response, nitems(te_notif_response),
390 	    iwm_te_notif, /*te_data*/NULL);
391 
392 	/* If TE was sent OK - wait for the notification that started */
393 	if (iwm_time_event_send_add(sc, ivp, /*te_data*/NULL, &time_cmd)) {
394 		IWM_DPRINTF(sc, IWM_DEBUG_TE,
395 		    "%s: Failed to add TE to protect session\n", __func__);
396 		iwm_remove_notification(sc->sc_notif_wait, &wait_te_notif);
397 	} else {
398 		sc->sc_flags |= IWM_FLAG_TE_ACTIVE;
399 		IWM_UNLOCK(sc);
400 		if (iwm_wait_notification(sc->sc_notif_wait, &wait_te_notif,
401 		    TU_TO_HZ(max_delay))) {
402 			IWM_DPRINTF(sc, IWM_DEBUG_TE,
403 			    "%s: Failed to protect session until TE\n",
404 			    __func__);
405 		}
406 		IWM_LOCK(sc);
407 	}
408 }
409 
410 void
411 iwm_stop_session_protection(struct iwm_softc *sc, struct iwm_vap *ivp)
412 {
413 	struct iwm_time_event_cmd time_cmd = {};
414 
415 	/* Do nothing if the time event has already ended. */
416 	if ((sc->sc_flags & IWM_FLAG_TE_ACTIVE) == 0)
417 		return;
418 
419 	time_cmd.id = htole32(sc->sc_time_event_uid);
420 	time_cmd.action = htole32(IWM_FW_CTXT_ACTION_REMOVE);
421 	time_cmd.id_and_color =
422 	    htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id, ivp->color));
423 
424 	IWM_DPRINTF(sc, IWM_DEBUG_TE,
425 	    "%s: Removing TE 0x%x\n", __func__, le32toh(time_cmd.id));
426 	if (iwm_send_cmd_pdu(sc, IWM_TIME_EVENT_CMD, 0, sizeof(time_cmd),
427 	    &time_cmd) == 0)
428 		iwm_te_clear_data(sc);
429 
430 	DELAY(100);
431 }
432