Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / fweh.c
blobe7eaa57d11d98f61280bd99013e649aacec7d0d6
1 /*
2 * Copyright (c) 2012 Broadcom Corporation
4 * Permission to use, copy, modify, and/or 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.
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 ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <linux/netdevice.h>
18 #include "brcmu_wifi.h"
19 #include "brcmu_utils.h"
21 #include "cfg80211.h"
22 #include "core.h"
23 #include "debug.h"
24 #include "tracepoint.h"
25 #include "fweh.h"
26 #include "fwil.h"
27 #include "proto.h"
29 /**
30 * struct brcmf_fweh_queue_item - event item on event queue.
32 * @q: list element for queuing.
33 * @code: event code.
34 * @ifidx: interface index related to this event.
35 * @ifaddr: ethernet address for interface.
36 * @emsg: common parameters of the firmware event message.
37 * @data: event specific data part of the firmware event.
39 struct brcmf_fweh_queue_item {
40 struct list_head q;
41 enum brcmf_fweh_event_code code;
42 u8 ifidx;
43 u8 ifaddr[ETH_ALEN];
44 struct brcmf_event_msg_be emsg;
45 u32 datalen;
46 u8 data[0];
49 /**
50 * struct brcmf_fweh_event_name - code, name mapping entry.
52 struct brcmf_fweh_event_name {
53 enum brcmf_fweh_event_code code;
54 const char *name;
57 #ifdef DEBUG
58 #define BRCMF_ENUM_DEF(id, val) \
59 { val, #id },
61 /* array for mapping code to event name */
62 static struct brcmf_fweh_event_name fweh_event_names[] = {
63 BRCMF_FWEH_EVENT_ENUM_DEFLIST
65 #undef BRCMF_ENUM_DEF
67 /**
68 * brcmf_fweh_event_name() - returns name for given event code.
70 * @code: code to lookup.
72 const char *brcmf_fweh_event_name(enum brcmf_fweh_event_code code)
74 int i;
75 for (i = 0; i < ARRAY_SIZE(fweh_event_names); i++) {
76 if (fweh_event_names[i].code == code)
77 return fweh_event_names[i].name;
79 return "unknown";
81 #else
82 const char *brcmf_fweh_event_name(enum brcmf_fweh_event_code code)
84 return "nodebug";
86 #endif
88 /**
89 * brcmf_fweh_queue_event() - create and queue event.
91 * @fweh: firmware event handling info.
92 * @event: event queue entry.
94 static void brcmf_fweh_queue_event(struct brcmf_fweh_info *fweh,
95 struct brcmf_fweh_queue_item *event)
97 ulong flags;
99 spin_lock_irqsave(&fweh->evt_q_lock, flags);
100 list_add_tail(&event->q, &fweh->event_q);
101 spin_unlock_irqrestore(&fweh->evt_q_lock, flags);
102 schedule_work(&fweh->event_work);
105 static int brcmf_fweh_call_event_handler(struct brcmf_if *ifp,
106 enum brcmf_fweh_event_code code,
107 struct brcmf_event_msg *emsg,
108 void *data)
110 struct brcmf_fweh_info *fweh;
111 int err = -EINVAL;
113 if (ifp) {
114 fweh = &ifp->drvr->fweh;
116 /* handle the event if valid interface and handler */
117 if (fweh->evt_handler[code])
118 err = fweh->evt_handler[code](ifp, emsg, data);
119 else
120 brcmf_err("unhandled event %d ignored\n", code);
121 } else {
122 brcmf_err("no interface object\n");
124 return err;
128 * brcmf_fweh_handle_if_event() - handle IF event.
130 * @drvr: driver information object.
131 * @item: queue entry.
132 * @ifpp: interface object (may change upon ADD action).
134 static void brcmf_fweh_handle_if_event(struct brcmf_pub *drvr,
135 struct brcmf_event_msg *emsg,
136 void *data)
138 struct brcmf_if_event *ifevent = data;
139 struct brcmf_if *ifp;
140 bool is_p2pdev;
141 int err = 0;
143 brcmf_dbg(EVENT, "action: %u ifidx: %u bsscfgidx: %u flags: %u role: %u\n",
144 ifevent->action, ifevent->ifidx, ifevent->bsscfgidx,
145 ifevent->flags, ifevent->role);
147 /* The P2P Device interface event must not be ignored contrary to what
148 * firmware tells us. Older firmware uses p2p noif, with sta role.
149 * This should be accepted when p2pdev_setup is ongoing. TDLS setup will
150 * use the same ifevent and should be ignored.
152 is_p2pdev = ((ifevent->flags & BRCMF_E_IF_FLAG_NOIF) &&
153 (ifevent->role == BRCMF_E_IF_ROLE_P2P_CLIENT ||
154 ((ifevent->role == BRCMF_E_IF_ROLE_STA) &&
155 (drvr->fweh.p2pdev_setup_ongoing))));
156 if (!is_p2pdev && (ifevent->flags & BRCMF_E_IF_FLAG_NOIF)) {
157 brcmf_dbg(EVENT, "event can be ignored\n");
158 return;
160 if (ifevent->ifidx >= BRCMF_MAX_IFS) {
161 brcmf_err("invalid interface index: %u\n", ifevent->ifidx);
162 return;
165 ifp = drvr->iflist[ifevent->bsscfgidx];
167 if (ifevent->action == BRCMF_E_IF_ADD) {
168 brcmf_dbg(EVENT, "adding %s (%pM)\n", emsg->ifname,
169 emsg->addr);
170 ifp = brcmf_add_if(drvr, ifevent->bsscfgidx, ifevent->ifidx,
171 is_p2pdev, emsg->ifname, emsg->addr);
172 if (IS_ERR(ifp))
173 return;
174 if (!is_p2pdev)
175 brcmf_proto_add_if(drvr, ifp);
176 if (!drvr->fweh.evt_handler[BRCMF_E_IF])
177 if (brcmf_net_attach(ifp, false) < 0)
178 return;
181 if (ifp && ifevent->action == BRCMF_E_IF_CHANGE)
182 brcmf_proto_reset_if(drvr, ifp);
184 err = brcmf_fweh_call_event_handler(ifp, emsg->event_code, emsg, data);
186 if (ifp && ifevent->action == BRCMF_E_IF_DEL) {
187 bool armed = brcmf_cfg80211_vif_event_armed(drvr->config);
189 /* Default handling in case no-one waits for this event */
190 if (!armed)
191 brcmf_remove_interface(ifp, false);
196 * brcmf_fweh_dequeue_event() - get event from the queue.
198 * @fweh: firmware event handling info.
200 static struct brcmf_fweh_queue_item *
201 brcmf_fweh_dequeue_event(struct brcmf_fweh_info *fweh)
203 struct brcmf_fweh_queue_item *event = NULL;
204 ulong flags;
206 spin_lock_irqsave(&fweh->evt_q_lock, flags);
207 if (!list_empty(&fweh->event_q)) {
208 event = list_first_entry(&fweh->event_q,
209 struct brcmf_fweh_queue_item, q);
210 list_del(&event->q);
212 spin_unlock_irqrestore(&fweh->evt_q_lock, flags);
214 return event;
218 * brcmf_fweh_event_worker() - firmware event worker.
220 * @work: worker object.
222 static void brcmf_fweh_event_worker(struct work_struct *work)
224 struct brcmf_pub *drvr;
225 struct brcmf_if *ifp;
226 struct brcmf_fweh_info *fweh;
227 struct brcmf_fweh_queue_item *event;
228 int err = 0;
229 struct brcmf_event_msg_be *emsg_be;
230 struct brcmf_event_msg emsg;
232 fweh = container_of(work, struct brcmf_fweh_info, event_work);
233 drvr = container_of(fweh, struct brcmf_pub, fweh);
235 while ((event = brcmf_fweh_dequeue_event(fweh))) {
236 brcmf_dbg(EVENT, "event %s (%u) ifidx %u bsscfg %u addr %pM\n",
237 brcmf_fweh_event_name(event->code), event->code,
238 event->emsg.ifidx, event->emsg.bsscfgidx,
239 event->emsg.addr);
241 /* convert event message */
242 emsg_be = &event->emsg;
243 emsg.version = be16_to_cpu(emsg_be->version);
244 emsg.flags = be16_to_cpu(emsg_be->flags);
245 emsg.event_code = event->code;
246 emsg.status = be32_to_cpu(emsg_be->status);
247 emsg.reason = be32_to_cpu(emsg_be->reason);
248 emsg.auth_type = be32_to_cpu(emsg_be->auth_type);
249 emsg.datalen = be32_to_cpu(emsg_be->datalen);
250 memcpy(emsg.addr, emsg_be->addr, ETH_ALEN);
251 memcpy(emsg.ifname, emsg_be->ifname, sizeof(emsg.ifname));
252 emsg.ifidx = emsg_be->ifidx;
253 emsg.bsscfgidx = emsg_be->bsscfgidx;
255 brcmf_dbg(EVENT, " version %u flags %u status %u reason %u\n",
256 emsg.version, emsg.flags, emsg.status, emsg.reason);
257 brcmf_dbg_hex_dump(BRCMF_EVENT_ON(), event->data,
258 min_t(u32, emsg.datalen, 64),
259 "event payload, len=%d\n", emsg.datalen);
261 /* special handling of interface event */
262 if (event->code == BRCMF_E_IF) {
263 brcmf_fweh_handle_if_event(drvr, &emsg, event->data);
264 goto event_free;
267 if (event->code == BRCMF_E_TDLS_PEER_EVENT)
268 ifp = drvr->iflist[0];
269 else
270 ifp = drvr->iflist[emsg.bsscfgidx];
271 err = brcmf_fweh_call_event_handler(ifp, event->code, &emsg,
272 event->data);
273 if (err) {
274 brcmf_err("event handler failed (%d)\n",
275 event->code);
276 err = 0;
278 event_free:
279 kfree(event);
284 * brcmf_fweh_p2pdev_setup() - P2P device setup ongoing (or not).
286 * @ifp: ifp on which setup is taking place or finished.
287 * @ongoing: p2p device setup in progress (or not).
289 void brcmf_fweh_p2pdev_setup(struct brcmf_if *ifp, bool ongoing)
291 ifp->drvr->fweh.p2pdev_setup_ongoing = ongoing;
295 * brcmf_fweh_attach() - initialize firmware event handling.
297 * @drvr: driver information object.
299 void brcmf_fweh_attach(struct brcmf_pub *drvr)
301 struct brcmf_fweh_info *fweh = &drvr->fweh;
302 INIT_WORK(&fweh->event_work, brcmf_fweh_event_worker);
303 spin_lock_init(&fweh->evt_q_lock);
304 INIT_LIST_HEAD(&fweh->event_q);
308 * brcmf_fweh_detach() - cleanup firmware event handling.
310 * @drvr: driver information object.
312 void brcmf_fweh_detach(struct brcmf_pub *drvr)
314 struct brcmf_fweh_info *fweh = &drvr->fweh;
315 struct brcmf_if *ifp = brcmf_get_ifp(drvr, 0);
316 s8 eventmask[BRCMF_EVENTING_MASK_LEN];
318 if (ifp) {
319 /* clear all events */
320 memset(eventmask, 0, BRCMF_EVENTING_MASK_LEN);
321 (void)brcmf_fil_iovar_data_set(ifp, "event_msgs",
322 eventmask,
323 BRCMF_EVENTING_MASK_LEN);
325 /* cancel the worker */
326 cancel_work_sync(&fweh->event_work);
327 WARN_ON(!list_empty(&fweh->event_q));
328 memset(fweh->evt_handler, 0, sizeof(fweh->evt_handler));
332 * brcmf_fweh_register() - register handler for given event code.
334 * @drvr: driver information object.
335 * @code: event code.
336 * @handler: handler for the given event code.
338 int brcmf_fweh_register(struct brcmf_pub *drvr, enum brcmf_fweh_event_code code,
339 brcmf_fweh_handler_t handler)
341 if (drvr->fweh.evt_handler[code]) {
342 brcmf_err("event code %d already registered\n", code);
343 return -ENOSPC;
345 drvr->fweh.evt_handler[code] = handler;
346 brcmf_dbg(TRACE, "event handler registered for %s\n",
347 brcmf_fweh_event_name(code));
348 return 0;
352 * brcmf_fweh_unregister() - remove handler for given code.
354 * @drvr: driver information object.
355 * @code: event code.
357 void brcmf_fweh_unregister(struct brcmf_pub *drvr,
358 enum brcmf_fweh_event_code code)
360 brcmf_dbg(TRACE, "event handler cleared for %s\n",
361 brcmf_fweh_event_name(code));
362 drvr->fweh.evt_handler[code] = NULL;
366 * brcmf_fweh_activate_events() - enables firmware events registered.
368 * @ifp: primary interface object.
370 int brcmf_fweh_activate_events(struct brcmf_if *ifp)
372 int i, err;
373 s8 eventmask[BRCMF_EVENTING_MASK_LEN];
375 memset(eventmask, 0, sizeof(eventmask));
376 for (i = 0; i < BRCMF_E_LAST; i++) {
377 if (ifp->drvr->fweh.evt_handler[i]) {
378 brcmf_dbg(EVENT, "enable event %s\n",
379 brcmf_fweh_event_name(i));
380 setbit(eventmask, i);
384 /* want to handle IF event as well */
385 brcmf_dbg(EVENT, "enable event IF\n");
386 setbit(eventmask, BRCMF_E_IF);
388 err = brcmf_fil_iovar_data_set(ifp, "event_msgs",
389 eventmask, BRCMF_EVENTING_MASK_LEN);
390 if (err)
391 brcmf_err("Set event_msgs error (%d)\n", err);
393 return err;
397 * brcmf_fweh_process_event() - process skb as firmware event.
399 * @drvr: driver information object.
400 * @event_packet: event packet to process.
402 * If the packet buffer contains a firmware event message it will
403 * dispatch the event to a registered handler (using worker).
405 void brcmf_fweh_process_event(struct brcmf_pub *drvr,
406 struct brcmf_event *event_packet,
407 u32 packet_len)
409 enum brcmf_fweh_event_code code;
410 struct brcmf_fweh_info *fweh = &drvr->fweh;
411 struct brcmf_fweh_queue_item *event;
412 gfp_t alloc_flag = GFP_KERNEL;
413 void *data;
414 u32 datalen;
416 /* get event info */
417 code = get_unaligned_be32(&event_packet->msg.event_type);
418 datalen = get_unaligned_be32(&event_packet->msg.datalen);
419 data = &event_packet[1];
421 if (code >= BRCMF_E_LAST)
422 return;
424 if (code != BRCMF_E_IF && !fweh->evt_handler[code])
425 return;
427 if (datalen > BRCMF_DCMD_MAXLEN ||
428 datalen + sizeof(*event_packet) > packet_len)
429 return;
431 if (in_interrupt())
432 alloc_flag = GFP_ATOMIC;
434 event = kzalloc(sizeof(*event) + datalen, alloc_flag);
435 if (!event)
436 return;
438 event->code = code;
439 event->ifidx = event_packet->msg.ifidx;
441 /* use memcpy to get aligned event message */
442 memcpy(&event->emsg, &event_packet->msg, sizeof(event->emsg));
443 memcpy(event->data, data, datalen);
444 event->datalen = datalen;
445 memcpy(event->ifaddr, event_packet->eth.h_dest, ETH_ALEN);
447 brcmf_fweh_queue_event(fweh, event);