xref: /freebsd/sys/dev/rtwn/if_rtwn_task.c (revision 63a938566d524836885917d95bd491aa4400b181)
1 /*-
2  * Copyright (c) 2015-2016 Andriy Voskoboinyk <avos@FreeBSD.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
19 
20 #include "opt_wlan.h"
21 
22 #include <sys/param.h>
23 #include <sys/lock.h>
24 #include <sys/mutex.h>
25 #include <sys/mbuf.h>
26 #include <sys/kernel.h>
27 #include <sys/socket.h>
28 #include <sys/systm.h>
29 #include <sys/malloc.h>
30 #include <sys/queue.h>
31 #include <sys/taskqueue.h>
32 #include <sys/bus.h>
33 #include <sys/endian.h>
34 
35 #include <net/if.h>
36 #include <net/ethernet.h>
37 #include <net/if_media.h>
38 
39 #include <net80211/ieee80211_var.h>
40 #include <net80211/ieee80211_radiotap.h>
41 
42 #include <dev/rtwn/if_rtwnvar.h>
43 
44 #include <dev/rtwn/if_rtwn_task.h>
45 
46 
47 static void
48 rtwn_cmdq_cb(void *arg, int pending)
49 {
50 	struct rtwn_softc *sc = arg;
51 	struct rtwn_cmdq *item;
52 
53 	/*
54 	 * Device must be powered on (via rtwn_power_on())
55 	 * before any command may be sent.
56 	 */
57 	RTWN_LOCK(sc);
58 	if (!(sc->sc_flags & RTWN_RUNNING)) {
59 		RTWN_UNLOCK(sc);
60 		return;
61 	}
62 
63 	RTWN_CMDQ_LOCK(sc);
64 	while (sc->cmdq[sc->cmdq_first].func != NULL) {
65 		item = &sc->cmdq[sc->cmdq_first];
66 		sc->cmdq_first = (sc->cmdq_first + 1) % RTWN_CMDQ_SIZE;
67 		RTWN_CMDQ_UNLOCK(sc);
68 
69 		item->func(sc, &item->data);
70 
71 		RTWN_CMDQ_LOCK(sc);
72 		memset(item, 0, sizeof (*item));
73 	}
74 	RTWN_CMDQ_UNLOCK(sc);
75 	RTWN_UNLOCK(sc);
76 }
77 
78 void
79 rtwn_cmdq_init(struct rtwn_softc *sc)
80 {
81 	RTWN_CMDQ_LOCK_INIT(sc);
82 	TASK_INIT(&sc->cmdq_task, 0, rtwn_cmdq_cb, sc);
83 }
84 
85 void
86 rtwn_cmdq_destroy(struct rtwn_softc *sc)
87 {
88 	if (RTWN_CMDQ_LOCK_INITIALIZED(sc))
89 		RTWN_CMDQ_LOCK_DESTROY(sc);
90 }
91 
92 int
93 rtwn_cmd_sleepable(struct rtwn_softc *sc, const void *ptr, size_t len,
94     CMD_FUNC_PROTO)
95 {
96 	struct ieee80211com *ic = &sc->sc_ic;
97 
98 	KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
99 
100 	RTWN_CMDQ_LOCK(sc);
101 	if (sc->sc_detached) {
102 		RTWN_CMDQ_UNLOCK(sc);
103 		return (ESHUTDOWN);
104 	}
105 
106 	if (sc->cmdq[sc->cmdq_last].func != NULL) {
107 		device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
108 		RTWN_CMDQ_UNLOCK(sc);
109 
110 		return (EAGAIN);
111 	}
112 
113 	if (ptr != NULL)
114 		memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
115 	sc->cmdq[sc->cmdq_last].func = func;
116 	sc->cmdq_last = (sc->cmdq_last + 1) % RTWN_CMDQ_SIZE;
117 	RTWN_CMDQ_UNLOCK(sc);
118 
119 	ieee80211_runtask(ic, &sc->cmdq_task);
120 
121 	return (0);
122 }
123