1/*
2 * Copyright (c) 2012 Broadcom Corporation
3 *
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.
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 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.
15 */
16#include <linux/netdevice.h>
17
18#include "brcmu_wifi.h"
19#include "brcmu_utils.h"
20
21#include "core.h"
22#include "debug.h"
23#include "tracepoint.h"
24#include "fwsignal.h"
25#include "fweh.h"
26#include "fwil.h"
27
28/**
29 * struct brcm_ethhdr - broadcom specific ether header.
30 *
31 * @subtype: subtype for this packet.
32 * @length: TODO: length of appended data.
33 * @version: version indication.
34 * @oui: OUI of this packet.
35 * @usr_subtype: subtype for this OUI.
36 */
37struct brcm_ethhdr {
38	__be16 subtype;
39	__be16 length;
40	u8 version;
41	u8 oui[3];
42	__be16 usr_subtype;
43} __packed;
44
45struct brcmf_event_msg_be {
46	__be16 version;
47	__be16 flags;
48	__be32 event_type;
49	__be32 status;
50	__be32 reason;
51	__be32 auth_type;
52	__be32 datalen;
53	u8 addr[ETH_ALEN];
54	char ifname[IFNAMSIZ];
55	u8 ifidx;
56	u8 bsscfgidx;
57} __packed;
58
59/**
60 * struct brcmf_event - contents of broadcom event packet.
61 *
62 * @eth: standard ether header.
63 * @hdr: broadcom specific ether header.
64 * @msg: common part of the actual event message.
65 */
66struct brcmf_event {
67	struct ethhdr eth;
68	struct brcm_ethhdr hdr;
69	struct brcmf_event_msg_be msg;
70} __packed;
71
72/**
73 * struct brcmf_fweh_queue_item - event item on event queue.
74 *
75 * @q: list element for queuing.
76 * @code: event code.
77 * @ifidx: interface index related to this event.
78 * @ifaddr: ethernet address for interface.
79 * @emsg: common parameters of the firmware event message.
80 * @data: event specific data part of the firmware event.
81 */
82struct brcmf_fweh_queue_item {
83	struct list_head q;
84	enum brcmf_fweh_event_code code;
85	u8 ifidx;
86	u8 ifaddr[ETH_ALEN];
87	struct brcmf_event_msg_be emsg;
88	u8 data[0];
89};
90
91/**
92 * struct brcmf_fweh_event_name - code, name mapping entry.
93 */
94struct brcmf_fweh_event_name {
95	enum brcmf_fweh_event_code code;
96	const char *name;
97};
98
99#ifdef DEBUG
100#define BRCMF_ENUM_DEF(id, val) \
101	{ val, #id },
102
103/* array for mapping code to event name */
104static struct brcmf_fweh_event_name fweh_event_names[] = {
105	BRCMF_FWEH_EVENT_ENUM_DEFLIST
106};
107#undef BRCMF_ENUM_DEF
108
109/**
110 * brcmf_fweh_event_name() - returns name for given event code.
111 *
112 * @code: code to lookup.
113 */
114static const char *brcmf_fweh_event_name(enum brcmf_fweh_event_code code)
115{
116	int i;
117	for (i = 0; i < ARRAY_SIZE(fweh_event_names); i++) {
118		if (fweh_event_names[i].code == code)
119			return fweh_event_names[i].name;
120	}
121	return "unknown";
122}
123#else
124static const char *brcmf_fweh_event_name(enum brcmf_fweh_event_code code)
125{
126	return "nodebug";
127}
128#endif
129
130/**
131 * brcmf_fweh_queue_event() - create and queue event.
132 *
133 * @fweh: firmware event handling info.
134 * @event: event queue entry.
135 */
136static void brcmf_fweh_queue_event(struct brcmf_fweh_info *fweh,
137				   struct brcmf_fweh_queue_item *event)
138{
139	ulong flags;
140
141	spin_lock_irqsave(&fweh->evt_q_lock, flags);
142	list_add_tail(&event->q, &fweh->event_q);
143	spin_unlock_irqrestore(&fweh->evt_q_lock, flags);
144	schedule_work(&fweh->event_work);
145}
146
147static int brcmf_fweh_call_event_handler(struct brcmf_if *ifp,
148					 enum brcmf_fweh_event_code code,
149					 struct brcmf_event_msg *emsg,
150					 void *data)
151{
152	struct brcmf_fweh_info *fweh;
153	int err = -EINVAL;
154
155	if (ifp) {
156		fweh = &ifp->drvr->fweh;
157
158		/* handle the event if valid interface and handler */
159		if (fweh->evt_handler[code])
160			err = fweh->evt_handler[code](ifp, emsg, data);
161		else
162			brcmf_err("unhandled event %d ignored\n", code);
163	} else {
164		brcmf_err("no interface object\n");
165	}
166	return err;
167}
168
169/**
170 * brcmf_fweh_handle_if_event() - handle IF event.
171 *
172 * @drvr: driver information object.
173 * @item: queue entry.
174 * @ifpp: interface object (may change upon ADD action).
175 */
176static void brcmf_fweh_handle_if_event(struct brcmf_pub *drvr,
177				       struct brcmf_event_msg *emsg,
178				       void *data)
179{
180	struct brcmf_if_event *ifevent = data;
181	struct brcmf_if *ifp;
182	int err = 0;
183
184	brcmf_dbg(EVENT, "action: %u idx: %u bsscfg: %u flags: %u role: %u\n",
185		  ifevent->action, ifevent->ifidx, ifevent->bssidx,
186		  ifevent->flags, ifevent->role);
187
188	/* The P2P Device interface event must not be ignored
189	 * contrary to what firmware tells us. The only way to
190	 * distinguish the P2P Device is by looking at the ifidx
191	 * and bssidx received.
192	 */
193	if (!(ifevent->ifidx == 0 && ifevent->bssidx == 1) &&
194	    (ifevent->flags & BRCMF_E_IF_FLAG_NOIF)) {
195		brcmf_dbg(EVENT, "event can be ignored\n");
196		return;
197	}
198	if (ifevent->ifidx >= BRCMF_MAX_IFS) {
199		brcmf_err("invalid interface index: %u\n",
200			  ifevent->ifidx);
201		return;
202	}
203
204	ifp = drvr->iflist[ifevent->bssidx];
205
206	if (ifevent->action == BRCMF_E_IF_ADD) {
207		brcmf_dbg(EVENT, "adding %s (%pM)\n", emsg->ifname,
208			  emsg->addr);
209		ifp = brcmf_add_if(drvr, ifevent->bssidx, ifevent->ifidx,
210				   emsg->ifname, emsg->addr);
211		if (IS_ERR(ifp))
212			return;
213		brcmf_fws_add_interface(ifp);
214		if (!drvr->fweh.evt_handler[BRCMF_E_IF])
215			if (brcmf_net_attach(ifp, false) < 0)
216				return;
217	}
218
219	if (ifp && ifevent->action == BRCMF_E_IF_CHANGE)
220		brcmf_fws_reset_interface(ifp);
221
222	err = brcmf_fweh_call_event_handler(ifp, emsg->event_code, emsg, data);
223
224	if (ifp && ifevent->action == BRCMF_E_IF_DEL)
225		brcmf_remove_interface(drvr, ifevent->bssidx);
226}
227
228/**
229 * brcmf_fweh_dequeue_event() - get event from the queue.
230 *
231 * @fweh: firmware event handling info.
232 */
233static struct brcmf_fweh_queue_item *
234brcmf_fweh_dequeue_event(struct brcmf_fweh_info *fweh)
235{
236	struct brcmf_fweh_queue_item *event = NULL;
237	ulong flags;
238
239	spin_lock_irqsave(&fweh->evt_q_lock, flags);
240	if (!list_empty(&fweh->event_q)) {
241		event = list_first_entry(&fweh->event_q,
242					 struct brcmf_fweh_queue_item, q);
243		list_del(&event->q);
244	}
245	spin_unlock_irqrestore(&fweh->evt_q_lock, flags);
246
247	return event;
248}
249
250/**
251 * brcmf_fweh_event_worker() - firmware event worker.
252 *
253 * @work: worker object.
254 */
255static void brcmf_fweh_event_worker(struct work_struct *work)
256{
257	struct brcmf_pub *drvr;
258	struct brcmf_if *ifp;
259	struct brcmf_fweh_info *fweh;
260	struct brcmf_fweh_queue_item *event;
261	int err = 0;
262	struct brcmf_event_msg_be *emsg_be;
263	struct brcmf_event_msg emsg;
264
265	fweh = container_of(work, struct brcmf_fweh_info, event_work);
266	drvr = container_of(fweh, struct brcmf_pub, fweh);
267
268	while ((event = brcmf_fweh_dequeue_event(fweh))) {
269		brcmf_dbg(EVENT, "event %s (%u) ifidx %u bsscfg %u addr %pM\n",
270			  brcmf_fweh_event_name(event->code), event->code,
271			  event->emsg.ifidx, event->emsg.bsscfgidx,
272			  event->emsg.addr);
273
274		/* convert event message */
275		emsg_be = &event->emsg;
276		emsg.version = be16_to_cpu(emsg_be->version);
277		emsg.flags = be16_to_cpu(emsg_be->flags);
278		emsg.event_code = event->code;
279		emsg.status = be32_to_cpu(emsg_be->status);
280		emsg.reason = be32_to_cpu(emsg_be->reason);
281		emsg.auth_type = be32_to_cpu(emsg_be->auth_type);
282		emsg.datalen = be32_to_cpu(emsg_be->datalen);
283		memcpy(emsg.addr, emsg_be->addr, ETH_ALEN);
284		memcpy(emsg.ifname, emsg_be->ifname, sizeof(emsg.ifname));
285		emsg.ifidx = emsg_be->ifidx;
286		emsg.bsscfgidx = emsg_be->bsscfgidx;
287
288		brcmf_dbg(EVENT, "  version %u flags %u status %u reason %u\n",
289			  emsg.version, emsg.flags, emsg.status, emsg.reason);
290		brcmf_dbg_hex_dump(BRCMF_EVENT_ON(), event->data,
291				   min_t(u32, emsg.datalen, 64),
292				   "event payload, len=%d\n", emsg.datalen);
293
294		/* special handling of interface event */
295		if (event->code == BRCMF_E_IF) {
296			brcmf_fweh_handle_if_event(drvr, &emsg, event->data);
297			goto event_free;
298		}
299
300		if ((event->code == BRCMF_E_TDLS_PEER_EVENT) &&
301		    (emsg.bsscfgidx == 1))
302			ifp = drvr->iflist[0];
303		else
304			ifp = drvr->iflist[emsg.bsscfgidx];
305		err = brcmf_fweh_call_event_handler(ifp, event->code, &emsg,
306						    event->data);
307		if (err) {
308			brcmf_err("event handler failed (%d)\n",
309				  event->code);
310			err = 0;
311		}
312event_free:
313		kfree(event);
314	}
315}
316
317/**
318 * brcmf_fweh_attach() - initialize firmware event handling.
319 *
320 * @drvr: driver information object.
321 */
322void brcmf_fweh_attach(struct brcmf_pub *drvr)
323{
324	struct brcmf_fweh_info *fweh = &drvr->fweh;
325	INIT_WORK(&fweh->event_work, brcmf_fweh_event_worker);
326	spin_lock_init(&fweh->evt_q_lock);
327	INIT_LIST_HEAD(&fweh->event_q);
328}
329
330/**
331 * brcmf_fweh_detach() - cleanup firmware event handling.
332 *
333 * @drvr: driver information object.
334 */
335void brcmf_fweh_detach(struct brcmf_pub *drvr)
336{
337	struct brcmf_fweh_info *fweh = &drvr->fweh;
338	struct brcmf_if *ifp = drvr->iflist[0];
339	s8 eventmask[BRCMF_EVENTING_MASK_LEN];
340
341	if (ifp) {
342		/* clear all events */
343		memset(eventmask, 0, BRCMF_EVENTING_MASK_LEN);
344		(void)brcmf_fil_iovar_data_set(ifp, "event_msgs",
345					       eventmask,
346					       BRCMF_EVENTING_MASK_LEN);
347	}
348	/* cancel the worker */
349	cancel_work_sync(&fweh->event_work);
350	WARN_ON(!list_empty(&fweh->event_q));
351	memset(fweh->evt_handler, 0, sizeof(fweh->evt_handler));
352}
353
354/**
355 * brcmf_fweh_register() - register handler for given event code.
356 *
357 * @drvr: driver information object.
358 * @code: event code.
359 * @handler: handler for the given event code.
360 */
361int brcmf_fweh_register(struct brcmf_pub *drvr, enum brcmf_fweh_event_code code,
362			brcmf_fweh_handler_t handler)
363{
364	if (drvr->fweh.evt_handler[code]) {
365		brcmf_err("event code %d already registered\n", code);
366		return -ENOSPC;
367	}
368	drvr->fweh.evt_handler[code] = handler;
369	brcmf_dbg(TRACE, "event handler registered for %s\n",
370		  brcmf_fweh_event_name(code));
371	return 0;
372}
373
374/**
375 * brcmf_fweh_unregister() - remove handler for given code.
376 *
377 * @drvr: driver information object.
378 * @code: event code.
379 */
380void brcmf_fweh_unregister(struct brcmf_pub *drvr,
381			   enum brcmf_fweh_event_code code)
382{
383	brcmf_dbg(TRACE, "event handler cleared for %s\n",
384		  brcmf_fweh_event_name(code));
385	drvr->fweh.evt_handler[code] = NULL;
386}
387
388/**
389 * brcmf_fweh_activate_events() - enables firmware events registered.
390 *
391 * @ifp: primary interface object.
392 */
393int brcmf_fweh_activate_events(struct brcmf_if *ifp)
394{
395	int i, err;
396	s8 eventmask[BRCMF_EVENTING_MASK_LEN];
397
398	for (i = 0; i < BRCMF_E_LAST; i++) {
399		if (ifp->drvr->fweh.evt_handler[i]) {
400			brcmf_dbg(EVENT, "enable event %s\n",
401				  brcmf_fweh_event_name(i));
402			setbit(eventmask, i);
403		}
404	}
405
406	/* want to handle IF event as well */
407	brcmf_dbg(EVENT, "enable event IF\n");
408	setbit(eventmask, BRCMF_E_IF);
409
410	err = brcmf_fil_iovar_data_set(ifp, "event_msgs",
411				       eventmask, BRCMF_EVENTING_MASK_LEN);
412	if (err)
413		brcmf_err("Set event_msgs error (%d)\n", err);
414
415	return err;
416}
417
418/**
419 * brcmf_fweh_process_event() - process skb as firmware event.
420 *
421 * @drvr: driver information object.
422 * @event_packet: event packet to process.
423 *
424 * If the packet buffer contains a firmware event message it will
425 * dispatch the event to a registered handler (using worker).
426 */
427void brcmf_fweh_process_event(struct brcmf_pub *drvr,
428			      struct brcmf_event *event_packet)
429{
430	enum brcmf_fweh_event_code code;
431	struct brcmf_fweh_info *fweh = &drvr->fweh;
432	struct brcmf_fweh_queue_item *event;
433	gfp_t alloc_flag = GFP_KERNEL;
434	void *data;
435	u32 datalen;
436
437	/* get event info */
438	code = get_unaligned_be32(&event_packet->msg.event_type);
439	datalen = get_unaligned_be32(&event_packet->msg.datalen);
440	data = &event_packet[1];
441
442	if (code >= BRCMF_E_LAST)
443		return;
444
445	if (code != BRCMF_E_IF && !fweh->evt_handler[code])
446		return;
447
448	if (in_interrupt())
449		alloc_flag = GFP_ATOMIC;
450
451	event = kzalloc(sizeof(*event) + datalen, alloc_flag);
452	if (!event)
453		return;
454
455	event->code = code;
456	event->ifidx = event_packet->msg.ifidx;
457
458	/* use memcpy to get aligned event message */
459	memcpy(&event->emsg, &event_packet->msg, sizeof(event->emsg));
460	memcpy(event->data, data, datalen);
461	memcpy(event->ifaddr, event_packet->eth.h_dest, ETH_ALEN);
462
463	brcmf_fweh_queue_event(fweh, event);
464}
465