1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2012 Broadcom Corporation
5 #include <linux/netdevice.h>
7 #include "brcmu_wifi.h"
8 #include "brcmu_utils.h"
13 #include "tracepoint.h"
19 * struct brcmf_fweh_queue_item - event item on event queue.
21 * @q: list element for queuing.
23 * @ifidx: interface index related to this event.
24 * @ifaddr: ethernet address for interface.
25 * @emsg: common parameters of the firmware event message.
26 * @datalen: length of the data array
27 * @data: event specific data part of the firmware event.
29 struct brcmf_fweh_queue_item
{
31 enum brcmf_fweh_event_code code
;
34 struct brcmf_event_msg_be emsg
;
40 * struct brcmf_fweh_event_name - code, name mapping entry.
42 struct brcmf_fweh_event_name
{
43 enum brcmf_fweh_event_code code
;
48 #define BRCMF_ENUM_DEF(id, val) \
51 /* array for mapping code to event name */
52 static struct brcmf_fweh_event_name fweh_event_names
[] = {
53 BRCMF_FWEH_EVENT_ENUM_DEFLIST
58 * brcmf_fweh_event_name() - returns name for given event code.
60 * @code: code to lookup.
62 const char *brcmf_fweh_event_name(enum brcmf_fweh_event_code code
)
65 for (i
= 0; i
< ARRAY_SIZE(fweh_event_names
); i
++) {
66 if (fweh_event_names
[i
].code
== code
)
67 return fweh_event_names
[i
].name
;
72 const char *brcmf_fweh_event_name(enum brcmf_fweh_event_code code
)
79 * brcmf_fweh_queue_event() - create and queue event.
81 * @fweh: firmware event handling info.
82 * @event: event queue entry.
84 static void brcmf_fweh_queue_event(struct brcmf_fweh_info
*fweh
,
85 struct brcmf_fweh_queue_item
*event
)
89 spin_lock_irqsave(&fweh
->evt_q_lock
, flags
);
90 list_add_tail(&event
->q
, &fweh
->event_q
);
91 spin_unlock_irqrestore(&fweh
->evt_q_lock
, flags
);
92 schedule_work(&fweh
->event_work
);
95 static int brcmf_fweh_call_event_handler(struct brcmf_pub
*drvr
,
97 enum brcmf_fweh_event_code code
,
98 struct brcmf_event_msg
*emsg
,
101 struct brcmf_fweh_info
*fweh
;
105 fweh
= &ifp
->drvr
->fweh
;
107 /* handle the event if valid interface and handler */
108 if (fweh
->evt_handler
[code
])
109 err
= fweh
->evt_handler
[code
](ifp
, emsg
, data
);
111 bphy_err(drvr
, "unhandled event %d ignored\n", code
);
113 bphy_err(drvr
, "no interface object\n");
119 * brcmf_fweh_handle_if_event() - handle IF event.
121 * @drvr: driver information object.
122 * @emsg: event message object.
123 * @data: event object.
125 static void brcmf_fweh_handle_if_event(struct brcmf_pub
*drvr
,
126 struct brcmf_event_msg
*emsg
,
129 struct brcmf_if_event
*ifevent
= data
;
130 struct brcmf_if
*ifp
;
133 brcmf_dbg(EVENT
, "action: %u ifidx: %u bsscfgidx: %u flags: %u role: %u\n",
134 ifevent
->action
, ifevent
->ifidx
, ifevent
->bsscfgidx
,
135 ifevent
->flags
, ifevent
->role
);
137 /* The P2P Device interface event must not be ignored contrary to what
138 * firmware tells us. Older firmware uses p2p noif, with sta role.
139 * This should be accepted when p2pdev_setup is ongoing. TDLS setup will
140 * use the same ifevent and should be ignored.
142 is_p2pdev
= ((ifevent
->flags
& BRCMF_E_IF_FLAG_NOIF
) &&
143 (ifevent
->role
== BRCMF_E_IF_ROLE_P2P_CLIENT
||
144 ((ifevent
->role
== BRCMF_E_IF_ROLE_STA
) &&
145 (drvr
->fweh
.p2pdev_setup_ongoing
))));
146 if (!is_p2pdev
&& (ifevent
->flags
& BRCMF_E_IF_FLAG_NOIF
)) {
147 brcmf_dbg(EVENT
, "event can be ignored\n");
150 if (ifevent
->ifidx
>= BRCMF_MAX_IFS
) {
151 bphy_err(drvr
, "invalid interface index: %u\n", ifevent
->ifidx
);
155 ifp
= drvr
->iflist
[ifevent
->bsscfgidx
];
157 if (ifevent
->action
== BRCMF_E_IF_ADD
) {
158 brcmf_dbg(EVENT
, "adding %s (%pM)\n", emsg
->ifname
,
160 ifp
= brcmf_add_if(drvr
, ifevent
->bsscfgidx
, ifevent
->ifidx
,
161 is_p2pdev
, emsg
->ifname
, emsg
->addr
);
165 brcmf_proto_add_if(drvr
, ifp
);
166 if (!drvr
->fweh
.evt_handler
[BRCMF_E_IF
])
167 if (brcmf_net_attach(ifp
, false) < 0)
171 if (ifp
&& ifevent
->action
== BRCMF_E_IF_CHANGE
)
172 brcmf_proto_reset_if(drvr
, ifp
);
174 brcmf_fweh_call_event_handler(drvr
, ifp
, emsg
->event_code
, emsg
,
177 if (ifp
&& ifevent
->action
== BRCMF_E_IF_DEL
) {
178 bool armed
= brcmf_cfg80211_vif_event_armed(drvr
->config
);
180 /* Default handling in case no-one waits for this event */
182 brcmf_remove_interface(ifp
, false);
187 * brcmf_fweh_dequeue_event() - get event from the queue.
189 * @fweh: firmware event handling info.
191 static struct brcmf_fweh_queue_item
*
192 brcmf_fweh_dequeue_event(struct brcmf_fweh_info
*fweh
)
194 struct brcmf_fweh_queue_item
*event
= NULL
;
197 spin_lock_irqsave(&fweh
->evt_q_lock
, flags
);
198 if (!list_empty(&fweh
->event_q
)) {
199 event
= list_first_entry(&fweh
->event_q
,
200 struct brcmf_fweh_queue_item
, q
);
203 spin_unlock_irqrestore(&fweh
->evt_q_lock
, flags
);
209 * brcmf_fweh_event_worker() - firmware event worker.
211 * @work: worker object.
213 static void brcmf_fweh_event_worker(struct work_struct
*work
)
215 struct brcmf_pub
*drvr
;
216 struct brcmf_if
*ifp
;
217 struct brcmf_fweh_info
*fweh
;
218 struct brcmf_fweh_queue_item
*event
;
220 struct brcmf_event_msg_be
*emsg_be
;
221 struct brcmf_event_msg emsg
;
223 fweh
= container_of(work
, struct brcmf_fweh_info
, event_work
);
224 drvr
= container_of(fweh
, struct brcmf_pub
, fweh
);
226 while ((event
= brcmf_fweh_dequeue_event(fweh
))) {
227 brcmf_dbg(EVENT
, "event %s (%u) ifidx %u bsscfg %u addr %pM\n",
228 brcmf_fweh_event_name(event
->code
), event
->code
,
229 event
->emsg
.ifidx
, event
->emsg
.bsscfgidx
,
232 /* convert event message */
233 emsg_be
= &event
->emsg
;
234 emsg
.version
= be16_to_cpu(emsg_be
->version
);
235 emsg
.flags
= be16_to_cpu(emsg_be
->flags
);
236 emsg
.event_code
= event
->code
;
237 emsg
.status
= be32_to_cpu(emsg_be
->status
);
238 emsg
.reason
= be32_to_cpu(emsg_be
->reason
);
239 emsg
.auth_type
= be32_to_cpu(emsg_be
->auth_type
);
240 emsg
.datalen
= be32_to_cpu(emsg_be
->datalen
);
241 memcpy(emsg
.addr
, emsg_be
->addr
, ETH_ALEN
);
242 memcpy(emsg
.ifname
, emsg_be
->ifname
, sizeof(emsg
.ifname
));
243 emsg
.ifidx
= emsg_be
->ifidx
;
244 emsg
.bsscfgidx
= emsg_be
->bsscfgidx
;
246 brcmf_dbg(EVENT
, " version %u flags %u status %u reason %u\n",
247 emsg
.version
, emsg
.flags
, emsg
.status
, emsg
.reason
);
248 brcmf_dbg_hex_dump(BRCMF_EVENT_ON(), event
->data
,
249 min_t(u32
, emsg
.datalen
, 64),
250 "event payload, len=%d\n", emsg
.datalen
);
252 /* special handling of interface event */
253 if (event
->code
== BRCMF_E_IF
) {
254 brcmf_fweh_handle_if_event(drvr
, &emsg
, event
->data
);
258 if (event
->code
== BRCMF_E_TDLS_PEER_EVENT
)
259 ifp
= drvr
->iflist
[0];
261 ifp
= drvr
->iflist
[emsg
.bsscfgidx
];
262 err
= brcmf_fweh_call_event_handler(drvr
, ifp
, event
->code
,
265 bphy_err(drvr
, "event handler failed (%d)\n",
275 * brcmf_fweh_p2pdev_setup() - P2P device setup ongoing (or not).
277 * @ifp: ifp on which setup is taking place or finished.
278 * @ongoing: p2p device setup in progress (or not).
280 void brcmf_fweh_p2pdev_setup(struct brcmf_if
*ifp
, bool ongoing
)
282 ifp
->drvr
->fweh
.p2pdev_setup_ongoing
= ongoing
;
286 * brcmf_fweh_attach() - initialize firmware event handling.
288 * @drvr: driver information object.
290 void brcmf_fweh_attach(struct brcmf_pub
*drvr
)
292 struct brcmf_fweh_info
*fweh
= &drvr
->fweh
;
293 INIT_WORK(&fweh
->event_work
, brcmf_fweh_event_worker
);
294 spin_lock_init(&fweh
->evt_q_lock
);
295 INIT_LIST_HEAD(&fweh
->event_q
);
299 * brcmf_fweh_detach() - cleanup firmware event handling.
301 * @drvr: driver information object.
303 void brcmf_fweh_detach(struct brcmf_pub
*drvr
)
305 struct brcmf_fweh_info
*fweh
= &drvr
->fweh
;
307 /* cancel the worker if initialized */
308 if (fweh
->event_work
.func
) {
309 cancel_work_sync(&fweh
->event_work
);
310 WARN_ON(!list_empty(&fweh
->event_q
));
311 memset(fweh
->evt_handler
, 0, sizeof(fweh
->evt_handler
));
316 * brcmf_fweh_register() - register handler for given event code.
318 * @drvr: driver information object.
320 * @handler: handler for the given event code.
322 int brcmf_fweh_register(struct brcmf_pub
*drvr
, enum brcmf_fweh_event_code code
,
323 brcmf_fweh_handler_t handler
)
325 if (drvr
->fweh
.evt_handler
[code
]) {
326 bphy_err(drvr
, "event code %d already registered\n", code
);
329 drvr
->fweh
.evt_handler
[code
] = handler
;
330 brcmf_dbg(TRACE
, "event handler registered for %s\n",
331 brcmf_fweh_event_name(code
));
336 * brcmf_fweh_unregister() - remove handler for given code.
338 * @drvr: driver information object.
341 void brcmf_fweh_unregister(struct brcmf_pub
*drvr
,
342 enum brcmf_fweh_event_code code
)
344 brcmf_dbg(TRACE
, "event handler cleared for %s\n",
345 brcmf_fweh_event_name(code
));
346 drvr
->fweh
.evt_handler
[code
] = NULL
;
350 * brcmf_fweh_activate_events() - enables firmware events registered.
352 * @ifp: primary interface object.
354 int brcmf_fweh_activate_events(struct brcmf_if
*ifp
)
356 struct brcmf_pub
*drvr
= ifp
->drvr
;
358 s8 eventmask
[BRCMF_EVENTING_MASK_LEN
];
360 memset(eventmask
, 0, sizeof(eventmask
));
361 for (i
= 0; i
< BRCMF_E_LAST
; i
++) {
362 if (ifp
->drvr
->fweh
.evt_handler
[i
]) {
363 brcmf_dbg(EVENT
, "enable event %s\n",
364 brcmf_fweh_event_name(i
));
365 setbit(eventmask
, i
);
369 /* want to handle IF event as well */
370 brcmf_dbg(EVENT
, "enable event IF\n");
371 setbit(eventmask
, BRCMF_E_IF
);
373 err
= brcmf_fil_iovar_data_set(ifp
, "event_msgs",
374 eventmask
, BRCMF_EVENTING_MASK_LEN
);
376 bphy_err(drvr
, "Set event_msgs error (%d)\n", err
);
382 * brcmf_fweh_process_event() - process skb as firmware event.
384 * @drvr: driver information object.
385 * @event_packet: event packet to process.
386 * @packet_len: length of the packet
387 * @gfp: memory allocation flags.
389 * If the packet buffer contains a firmware event message it will
390 * dispatch the event to a registered handler (using worker).
392 void brcmf_fweh_process_event(struct brcmf_pub
*drvr
,
393 struct brcmf_event
*event_packet
,
394 u32 packet_len
, gfp_t gfp
)
396 enum brcmf_fweh_event_code code
;
397 struct brcmf_fweh_info
*fweh
= &drvr
->fweh
;
398 struct brcmf_fweh_queue_item
*event
;
403 code
= get_unaligned_be32(&event_packet
->msg
.event_type
);
404 datalen
= get_unaligned_be32(&event_packet
->msg
.datalen
);
405 data
= &event_packet
[1];
407 if (code
>= BRCMF_E_LAST
)
410 if (code
!= BRCMF_E_IF
&& !fweh
->evt_handler
[code
])
413 if (datalen
> BRCMF_DCMD_MAXLEN
||
414 datalen
+ sizeof(*event_packet
) > packet_len
)
417 event
= kzalloc(sizeof(*event
) + datalen
, gfp
);
422 event
->ifidx
= event_packet
->msg
.ifidx
;
424 /* use memcpy to get aligned event message */
425 memcpy(&event
->emsg
, &event_packet
->msg
, sizeof(event
->emsg
));
426 memcpy(event
->data
, data
, datalen
);
427 event
->datalen
= datalen
;
428 memcpy(event
->ifaddr
, event_packet
->eth
.h_dest
, ETH_ALEN
);
430 brcmf_fweh_queue_event(fweh
, event
);