xref: /freebsd/sys/dev/rtwn/if_rtwn_task.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 #include "opt_wlan.h"
19 
20 #include <sys/param.h>
21 #include <sys/lock.h>
22 #include <sys/mutex.h>
23 #include <sys/mbuf.h>
24 #include <sys/kernel.h>
25 #include <sys/socket.h>
26 #include <sys/systm.h>
27 #include <sys/malloc.h>
28 #include <sys/queue.h>
29 #include <sys/taskqueue.h>
30 #include <sys/bus.h>
31 #include <sys/endian.h>
32 
33 #include <net/if.h>
34 #include <net/ethernet.h>
35 #include <net/if_media.h>
36 
37 #include <net80211/ieee80211_var.h>
38 #include <net80211/ieee80211_radiotap.h>
39 
40 #include <dev/rtwn/if_rtwnvar.h>
41 
42 #include <dev/rtwn/if_rtwn_task.h>
43 
44 static void
45 rtwn_cmdq_cb(void *arg, int pending)
46 {
47 	struct rtwn_softc *sc = arg;
48 	struct rtwn_cmdq *item;
49 
50 	/*
51 	 * Device must be powered on (via rtwn_power_on())
52 	 * before any command may be sent.
53 	 */
54 	RTWN_LOCK(sc);
55 	if (!(sc->sc_flags & RTWN_RUNNING)) {
56 		RTWN_UNLOCK(sc);
57 		return;
58 	}
59 
60 	RTWN_CMDQ_LOCK(sc);
61 	while (sc->cmdq[sc->cmdq_first].func != NULL) {
62 		item = &sc->cmdq[sc->cmdq_first];
63 		sc->cmdq_first = (sc->cmdq_first + 1) % RTWN_CMDQ_SIZE;
64 		RTWN_CMDQ_UNLOCK(sc);
65 
66 		item->func(sc, &item->data);
67 
68 		RTWN_CMDQ_LOCK(sc);
69 		memset(item, 0, sizeof (*item));
70 	}
71 	RTWN_CMDQ_UNLOCK(sc);
72 	RTWN_UNLOCK(sc);
73 }
74 
75 void
76 rtwn_cmdq_init(struct rtwn_softc *sc)
77 {
78 	RTWN_CMDQ_LOCK_INIT(sc);
79 	TASK_INIT(&sc->cmdq_task, 0, rtwn_cmdq_cb, sc);
80 }
81 
82 void
83 rtwn_cmdq_destroy(struct rtwn_softc *sc)
84 {
85 	if (RTWN_CMDQ_LOCK_INITIALIZED(sc))
86 		RTWN_CMDQ_LOCK_DESTROY(sc);
87 }
88 
89 int
90 rtwn_cmd_sleepable(struct rtwn_softc *sc, const void *ptr, size_t len,
91     CMD_FUNC_PROTO)
92 {
93 	struct ieee80211com *ic = &sc->sc_ic;
94 
95 	KASSERT(len <= sizeof(union sec_param), ("buffer overflow"));
96 
97 	RTWN_CMDQ_LOCK(sc);
98 	if (sc->sc_detached) {
99 		RTWN_CMDQ_UNLOCK(sc);
100 		return (ESHUTDOWN);
101 	}
102 
103 	if (sc->cmdq[sc->cmdq_last].func != NULL) {
104 		device_printf(sc->sc_dev, "%s: cmdq overflow\n", __func__);
105 		RTWN_CMDQ_UNLOCK(sc);
106 
107 		return (EAGAIN);
108 	}
109 
110 	if (ptr != NULL)
111 		memcpy(&sc->cmdq[sc->cmdq_last].data, ptr, len);
112 	sc->cmdq[sc->cmdq_last].func = func;
113 	sc->cmdq_last = (sc->cmdq_last + 1) % RTWN_CMDQ_SIZE;
114 	RTWN_CMDQ_UNLOCK(sc);
115 
116 	ieee80211_runtask(ic, &sc->cmdq_task);
117 
118 	return (0);
119 }
120