xref: /linux/drivers/net/wireless/broadcom/brcm80211/brcmfmac/p2p.c (revision d003d772e64df08af04ee63609d47169ee82ae0e)
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/slab.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/rtnetlink.h>
20 #include <net/cfg80211.h>
21 
22 #include <brcmu_wifi.h>
23 #include <brcmu_utils.h>
24 #include <defs.h>
25 #include "core.h"
26 #include "debug.h"
27 #include "fwil.h"
28 #include "fwil_types.h"
29 #include "p2p.h"
30 #include "cfg80211.h"
31 
32 /* parameters used for p2p escan */
33 #define P2PAPI_SCAN_NPROBES 1
34 #define P2PAPI_SCAN_DWELL_TIME_MS 80
35 #define P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS 40
36 #define P2PAPI_SCAN_HOME_TIME_MS 60
37 #define P2PAPI_SCAN_NPROBS_TIME_MS 30
38 #define P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS 100
39 #define WL_SCAN_CONNECT_DWELL_TIME_MS 200
40 #define WL_SCAN_JOIN_PROBE_INTERVAL_MS 20
41 
42 #define BRCMF_P2P_WILDCARD_SSID		"DIRECT-"
43 #define BRCMF_P2P_WILDCARD_SSID_LEN	(sizeof(BRCMF_P2P_WILDCARD_SSID) - 1)
44 
45 #define SOCIAL_CHAN_1		1
46 #define SOCIAL_CHAN_2		6
47 #define SOCIAL_CHAN_3		11
48 #define IS_P2P_SOCIAL_CHANNEL(channel) ((channel == SOCIAL_CHAN_1) || \
49 					 (channel == SOCIAL_CHAN_2) || \
50 					 (channel == SOCIAL_CHAN_3))
51 #define BRCMF_P2P_TEMP_CHAN	SOCIAL_CHAN_3
52 #define SOCIAL_CHAN_CNT		3
53 #define AF_PEER_SEARCH_CNT	2
54 
55 #define BRCMF_SCB_TIMEOUT_VALUE	20
56 
57 #define P2P_VER			9	/* P2P version: 9=WiFi P2P v1.0 */
58 #define P2P_PUB_AF_CATEGORY	0x04
59 #define P2P_PUB_AF_ACTION	0x09
60 #define P2P_AF_CATEGORY		0x7f
61 #define P2P_OUI			"\x50\x6F\x9A"	/* P2P OUI */
62 #define P2P_OUI_LEN		3		/* P2P OUI length */
63 
64 /* Action Frame Constants */
65 #define DOT11_ACTION_HDR_LEN	2	/* action frame category + action */
66 #define DOT11_ACTION_CAT_OFF	0	/* category offset */
67 #define DOT11_ACTION_ACT_OFF	1	/* action offset */
68 
69 #define P2P_AF_DWELL_TIME		200
70 #define P2P_AF_MIN_DWELL_TIME		100
71 #define P2P_AF_MED_DWELL_TIME		400
72 #define P2P_AF_LONG_DWELL_TIME		1000
73 #define P2P_AF_TX_MAX_RETRY		1
74 #define P2P_AF_MAX_WAIT_TIME		msecs_to_jiffies(2000)
75 #define P2P_INVALID_CHANNEL		-1
76 #define P2P_CHANNEL_SYNC_RETRY		5
77 #define P2P_AF_FRM_SCAN_MAX_WAIT	msecs_to_jiffies(450)
78 #define P2P_DEFAULT_SLEEP_TIME_VSDB	200
79 
80 /* WiFi P2P Public Action Frame OUI Subtypes */
81 #define P2P_PAF_GON_REQ		0	/* Group Owner Negotiation Req */
82 #define P2P_PAF_GON_RSP		1	/* Group Owner Negotiation Rsp */
83 #define P2P_PAF_GON_CONF	2	/* Group Owner Negotiation Confirm */
84 #define P2P_PAF_INVITE_REQ	3	/* P2P Invitation Request */
85 #define P2P_PAF_INVITE_RSP	4	/* P2P Invitation Response */
86 #define P2P_PAF_DEVDIS_REQ	5	/* Device Discoverability Request */
87 #define P2P_PAF_DEVDIS_RSP	6	/* Device Discoverability Response */
88 #define P2P_PAF_PROVDIS_REQ	7	/* Provision Discovery Request */
89 #define P2P_PAF_PROVDIS_RSP	8	/* Provision Discovery Response */
90 #define P2P_PAF_SUBTYPE_INVALID	255	/* Invalid Subtype */
91 
92 /* WiFi P2P Action Frame OUI Subtypes */
93 #define P2P_AF_NOTICE_OF_ABSENCE	0	/* Notice of Absence */
94 #define P2P_AF_PRESENCE_REQ		1	/* P2P Presence Request */
95 #define P2P_AF_PRESENCE_RSP		2	/* P2P Presence Response */
96 #define P2P_AF_GO_DISC_REQ		3	/* GO Discoverability Request */
97 
98 /* P2P Service Discovery related */
99 #define P2PSD_ACTION_CATEGORY		0x04	/* Public action frame */
100 #define P2PSD_ACTION_ID_GAS_IREQ	0x0a	/* GAS Initial Request AF */
101 #define P2PSD_ACTION_ID_GAS_IRESP	0x0b	/* GAS Initial Response AF */
102 #define P2PSD_ACTION_ID_GAS_CREQ	0x0c	/* GAS Comback Request AF */
103 #define P2PSD_ACTION_ID_GAS_CRESP	0x0d	/* GAS Comback Response AF */
104 
105 #define BRCMF_P2P_DISABLE_TIMEOUT	msecs_to_jiffies(500)
106 /**
107  * struct brcmf_p2p_disc_st_le - set discovery state in firmware.
108  *
109  * @state: requested discovery state (see enum brcmf_p2p_disc_state).
110  * @chspec: channel parameter for %WL_P2P_DISC_ST_LISTEN state.
111  * @dwell: dwell time in ms for %WL_P2P_DISC_ST_LISTEN state.
112  */
113 struct brcmf_p2p_disc_st_le {
114 	u8 state;
115 	__le16 chspec;
116 	__le16 dwell;
117 };
118 
119 /**
120  * enum brcmf_p2p_disc_state - P2P discovery state values
121  *
122  * @WL_P2P_DISC_ST_SCAN: P2P discovery with wildcard SSID and P2P IE.
123  * @WL_P2P_DISC_ST_LISTEN: P2P discovery off-channel for specified time.
124  * @WL_P2P_DISC_ST_SEARCH: P2P discovery with P2P wildcard SSID and P2P IE.
125  */
126 enum brcmf_p2p_disc_state {
127 	WL_P2P_DISC_ST_SCAN,
128 	WL_P2P_DISC_ST_LISTEN,
129 	WL_P2P_DISC_ST_SEARCH
130 };
131 
132 /**
133  * struct brcmf_p2p_scan_le - P2P specific scan request.
134  *
135  * @type: type of scan method requested (values: 'E' or 'S').
136  * @reserved: reserved (ignored).
137  * @eparams: parameters used for type 'E'.
138  * @sparams: parameters used for type 'S'.
139  */
140 struct brcmf_p2p_scan_le {
141 	u8 type;
142 	u8 reserved[3];
143 	union {
144 		struct brcmf_escan_params_le eparams;
145 		struct brcmf_scan_params_le sparams;
146 	};
147 };
148 
149 /**
150  * struct brcmf_p2p_pub_act_frame - WiFi P2P Public Action Frame
151  *
152  * @category: P2P_PUB_AF_CATEGORY
153  * @action: P2P_PUB_AF_ACTION
154  * @oui[3]: P2P_OUI
155  * @oui_type: OUI type - P2P_VER
156  * @subtype: OUI subtype - P2P_TYPE_*
157  * @dialog_token: nonzero, identifies req/rsp transaction
158  * @elts[1]: Variable length information elements.
159  */
160 struct brcmf_p2p_pub_act_frame {
161 	u8	category;
162 	u8	action;
163 	u8	oui[3];
164 	u8	oui_type;
165 	u8	subtype;
166 	u8	dialog_token;
167 	u8	elts[1];
168 };
169 
170 /**
171  * struct brcmf_p2p_action_frame - WiFi P2P Action Frame
172  *
173  * @category: P2P_AF_CATEGORY
174  * @OUI[3]: OUI - P2P_OUI
175  * @type: OUI Type - P2P_VER
176  * @subtype: OUI Subtype - P2P_AF_*
177  * @dialog_token: nonzero, identifies req/resp tranaction
178  * @elts[1]: Variable length information elements.
179  */
180 struct brcmf_p2p_action_frame {
181 	u8	category;
182 	u8	oui[3];
183 	u8	type;
184 	u8	subtype;
185 	u8	dialog_token;
186 	u8	elts[1];
187 };
188 
189 /**
190  * struct brcmf_p2psd_gas_pub_act_frame - Wi-Fi GAS Public Action Frame
191  *
192  * @category: 0x04 Public Action Frame
193  * @action: 0x6c Advertisement Protocol
194  * @dialog_token: nonzero, identifies req/rsp transaction
195  * @query_data[1]: Query Data. SD gas ireq SD gas iresp
196  */
197 struct brcmf_p2psd_gas_pub_act_frame {
198 	u8	category;
199 	u8	action;
200 	u8	dialog_token;
201 	u8	query_data[1];
202 };
203 
204 /**
205  * struct brcmf_config_af_params - Action Frame Parameters for tx.
206  *
207  * @mpc_onoff: To make sure to send successfully action frame, we have to
208  *             turn off mpc  0: off, 1: on,  (-1): do nothing
209  * @search_channel: 1: search peer's channel to send af
210  * extra_listen: keep the dwell time to get af response frame.
211  */
212 struct brcmf_config_af_params {
213 	s32 mpc_onoff;
214 	bool search_channel;
215 	bool extra_listen;
216 };
217 
218 /**
219  * brcmf_p2p_is_pub_action() - true if p2p public type frame.
220  *
221  * @frame: action frame data.
222  * @frame_len: length of action frame data.
223  *
224  * Determine if action frame is p2p public action type
225  */
226 static bool brcmf_p2p_is_pub_action(void *frame, u32 frame_len)
227 {
228 	struct brcmf_p2p_pub_act_frame *pact_frm;
229 
230 	if (frame == NULL)
231 		return false;
232 
233 	pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
234 	if (frame_len < sizeof(struct brcmf_p2p_pub_act_frame) - 1)
235 		return false;
236 
237 	if (pact_frm->category == P2P_PUB_AF_CATEGORY &&
238 	    pact_frm->action == P2P_PUB_AF_ACTION &&
239 	    pact_frm->oui_type == P2P_VER &&
240 	    memcmp(pact_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
241 		return true;
242 
243 	return false;
244 }
245 
246 /**
247  * brcmf_p2p_is_p2p_action() - true if p2p action type frame.
248  *
249  * @frame: action frame data.
250  * @frame_len: length of action frame data.
251  *
252  * Determine if action frame is p2p action type
253  */
254 static bool brcmf_p2p_is_p2p_action(void *frame, u32 frame_len)
255 {
256 	struct brcmf_p2p_action_frame *act_frm;
257 
258 	if (frame == NULL)
259 		return false;
260 
261 	act_frm = (struct brcmf_p2p_action_frame *)frame;
262 	if (frame_len < sizeof(struct brcmf_p2p_action_frame) - 1)
263 		return false;
264 
265 	if (act_frm->category == P2P_AF_CATEGORY &&
266 	    act_frm->type  == P2P_VER &&
267 	    memcmp(act_frm->oui, P2P_OUI, P2P_OUI_LEN) == 0)
268 		return true;
269 
270 	return false;
271 }
272 
273 /**
274  * brcmf_p2p_is_gas_action() - true if p2p gas action type frame.
275  *
276  * @frame: action frame data.
277  * @frame_len: length of action frame data.
278  *
279  * Determine if action frame is p2p gas action type
280  */
281 static bool brcmf_p2p_is_gas_action(void *frame, u32 frame_len)
282 {
283 	struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
284 
285 	if (frame == NULL)
286 		return false;
287 
288 	sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
289 	if (frame_len < sizeof(struct brcmf_p2psd_gas_pub_act_frame) - 1)
290 		return false;
291 
292 	if (sd_act_frm->category != P2PSD_ACTION_CATEGORY)
293 		return false;
294 
295 	if (sd_act_frm->action == P2PSD_ACTION_ID_GAS_IREQ ||
296 	    sd_act_frm->action == P2PSD_ACTION_ID_GAS_IRESP ||
297 	    sd_act_frm->action == P2PSD_ACTION_ID_GAS_CREQ ||
298 	    sd_act_frm->action == P2PSD_ACTION_ID_GAS_CRESP)
299 		return true;
300 
301 	return false;
302 }
303 
304 /**
305  * brcmf_p2p_print_actframe() - debug print routine.
306  *
307  * @tx: Received or to be transmitted
308  * @frame: action frame data.
309  * @frame_len: length of action frame data.
310  *
311  * Print information about the p2p action frame
312  */
313 
314 #ifdef DEBUG
315 
316 static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
317 {
318 	struct brcmf_p2p_pub_act_frame *pact_frm;
319 	struct brcmf_p2p_action_frame *act_frm;
320 	struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
321 
322 	if (!frame || frame_len <= 2)
323 		return;
324 
325 	if (brcmf_p2p_is_pub_action(frame, frame_len)) {
326 		pact_frm = (struct brcmf_p2p_pub_act_frame *)frame;
327 		switch (pact_frm->subtype) {
328 		case P2P_PAF_GON_REQ:
329 			brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Req Frame\n",
330 				  (tx) ? "TX" : "RX");
331 			break;
332 		case P2P_PAF_GON_RSP:
333 			brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Rsp Frame\n",
334 				  (tx) ? "TX" : "RX");
335 			break;
336 		case P2P_PAF_GON_CONF:
337 			brcmf_dbg(TRACE, "%s P2P Group Owner Negotiation Confirm Frame\n",
338 				  (tx) ? "TX" : "RX");
339 			break;
340 		case P2P_PAF_INVITE_REQ:
341 			brcmf_dbg(TRACE, "%s P2P Invitation Request  Frame\n",
342 				  (tx) ? "TX" : "RX");
343 			break;
344 		case P2P_PAF_INVITE_RSP:
345 			brcmf_dbg(TRACE, "%s P2P Invitation Response Frame\n",
346 				  (tx) ? "TX" : "RX");
347 			break;
348 		case P2P_PAF_DEVDIS_REQ:
349 			brcmf_dbg(TRACE, "%s P2P Device Discoverability Request Frame\n",
350 				  (tx) ? "TX" : "RX");
351 			break;
352 		case P2P_PAF_DEVDIS_RSP:
353 			brcmf_dbg(TRACE, "%s P2P Device Discoverability Response Frame\n",
354 				  (tx) ? "TX" : "RX");
355 			break;
356 		case P2P_PAF_PROVDIS_REQ:
357 			brcmf_dbg(TRACE, "%s P2P Provision Discovery Request Frame\n",
358 				  (tx) ? "TX" : "RX");
359 			break;
360 		case P2P_PAF_PROVDIS_RSP:
361 			brcmf_dbg(TRACE, "%s P2P Provision Discovery Response Frame\n",
362 				  (tx) ? "TX" : "RX");
363 			break;
364 		default:
365 			brcmf_dbg(TRACE, "%s Unknown P2P Public Action Frame\n",
366 				  (tx) ? "TX" : "RX");
367 			break;
368 		}
369 	} else if (brcmf_p2p_is_p2p_action(frame, frame_len)) {
370 		act_frm = (struct brcmf_p2p_action_frame *)frame;
371 		switch (act_frm->subtype) {
372 		case P2P_AF_NOTICE_OF_ABSENCE:
373 			brcmf_dbg(TRACE, "%s P2P Notice of Absence Frame\n",
374 				  (tx) ? "TX" : "RX");
375 			break;
376 		case P2P_AF_PRESENCE_REQ:
377 			brcmf_dbg(TRACE, "%s P2P Presence Request Frame\n",
378 				  (tx) ? "TX" : "RX");
379 			break;
380 		case P2P_AF_PRESENCE_RSP:
381 			brcmf_dbg(TRACE, "%s P2P Presence Response Frame\n",
382 				  (tx) ? "TX" : "RX");
383 			break;
384 		case P2P_AF_GO_DISC_REQ:
385 			brcmf_dbg(TRACE, "%s P2P Discoverability Request Frame\n",
386 				  (tx) ? "TX" : "RX");
387 			break;
388 		default:
389 			brcmf_dbg(TRACE, "%s Unknown P2P Action Frame\n",
390 				  (tx) ? "TX" : "RX");
391 		}
392 
393 	} else if (brcmf_p2p_is_gas_action(frame, frame_len)) {
394 		sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
395 		switch (sd_act_frm->action) {
396 		case P2PSD_ACTION_ID_GAS_IREQ:
397 			brcmf_dbg(TRACE, "%s P2P GAS Initial Request\n",
398 				  (tx) ? "TX" : "RX");
399 			break;
400 		case P2PSD_ACTION_ID_GAS_IRESP:
401 			brcmf_dbg(TRACE, "%s P2P GAS Initial Response\n",
402 				  (tx) ? "TX" : "RX");
403 			break;
404 		case P2PSD_ACTION_ID_GAS_CREQ:
405 			brcmf_dbg(TRACE, "%s P2P GAS Comback Request\n",
406 				  (tx) ? "TX" : "RX");
407 			break;
408 		case P2PSD_ACTION_ID_GAS_CRESP:
409 			brcmf_dbg(TRACE, "%s P2P GAS Comback Response\n",
410 				  (tx) ? "TX" : "RX");
411 			break;
412 		default:
413 			brcmf_dbg(TRACE, "%s Unknown P2P GAS Frame\n",
414 				  (tx) ? "TX" : "RX");
415 			break;
416 		}
417 	}
418 }
419 
420 #else
421 
422 static void brcmf_p2p_print_actframe(bool tx, void *frame, u32 frame_len)
423 {
424 }
425 
426 #endif
427 
428 
429 /**
430  * brcmf_p2p_set_firmware() - prepare firmware for peer-to-peer operation.
431  *
432  * @ifp: ifp to use for iovars (primary).
433  * @p2p_mac: mac address to configure for p2p_da_override
434  */
435 static int brcmf_p2p_set_firmware(struct brcmf_if *ifp, u8 *p2p_mac)
436 {
437 	struct brcmf_pub *drvr = ifp->drvr;
438 	s32 ret = 0;
439 
440 	brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
441 	brcmf_fil_iovar_int_set(ifp, "apsta", 1);
442 	brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
443 
444 	/* In case of COB type, firmware has default mac address
445 	 * After Initializing firmware, we have to set current mac address to
446 	 * firmware for P2P device address. This must be done with discovery
447 	 * disabled.
448 	 */
449 	brcmf_fil_iovar_int_set(ifp, "p2p_disc", 0);
450 
451 	ret = brcmf_fil_iovar_data_set(ifp, "p2p_da_override", p2p_mac,
452 				       ETH_ALEN);
453 	if (ret)
454 		bphy_err(drvr, "failed to update device address ret %d\n", ret);
455 
456 	return ret;
457 }
458 
459 /**
460  * brcmf_p2p_generate_bss_mac() - derive mac addresses for P2P.
461  *
462  * @p2p: P2P specific data.
463  * @dev_addr: optional device address.
464  *
465  * P2P needs mac addresses for P2P device and interface. If no device
466  * address it specified, these are derived from a random ethernet
467  * address.
468  */
469 static void brcmf_p2p_generate_bss_mac(struct brcmf_p2p_info *p2p, u8 *dev_addr)
470 {
471 	bool random_addr = false;
472 
473 	if (!dev_addr || is_zero_ether_addr(dev_addr))
474 		random_addr = true;
475 
476 	/* Generate the P2P Device Address obtaining a random ethernet
477 	 * address with the locally administered bit set.
478 	 */
479 	if (random_addr)
480 		eth_random_addr(p2p->dev_addr);
481 	else
482 		memcpy(p2p->dev_addr, dev_addr, ETH_ALEN);
483 
484 	/* Generate the P2P Interface Address.  If the discovery and connection
485 	 * BSSCFGs need to simultaneously co-exist, then this address must be
486 	 * different from the P2P Device Address, but also locally administered.
487 	 */
488 	memcpy(p2p->int_addr, p2p->dev_addr, ETH_ALEN);
489 	p2p->int_addr[0] |= 0x02;
490 	p2p->int_addr[4] ^= 0x80;
491 }
492 
493 /**
494  * brcmf_p2p_scan_is_p2p_request() - is cfg80211 scan request a P2P scan.
495  *
496  * @request: the scan request as received from cfg80211.
497  *
498  * returns true if one of the ssids in the request matches the
499  * P2P wildcard ssid; otherwise returns false.
500  */
501 static bool brcmf_p2p_scan_is_p2p_request(struct cfg80211_scan_request *request)
502 {
503 	struct cfg80211_ssid *ssids = request->ssids;
504 	int i;
505 
506 	for (i = 0; i < request->n_ssids; i++) {
507 		if (ssids[i].ssid_len != BRCMF_P2P_WILDCARD_SSID_LEN)
508 			continue;
509 
510 		brcmf_dbg(INFO, "comparing ssid \"%s\"", ssids[i].ssid);
511 		if (!memcmp(BRCMF_P2P_WILDCARD_SSID, ssids[i].ssid,
512 			    BRCMF_P2P_WILDCARD_SSID_LEN))
513 			return true;
514 	}
515 	return false;
516 }
517 
518 /**
519  * brcmf_p2p_set_discover_state - set discover state in firmware.
520  *
521  * @ifp: low-level interface object.
522  * @state: discover state to set.
523  * @chanspec: channel parameters (for state @WL_P2P_DISC_ST_LISTEN only).
524  * @listen_ms: duration to listen (for state @WL_P2P_DISC_ST_LISTEN only).
525  */
526 static s32 brcmf_p2p_set_discover_state(struct brcmf_if *ifp, u8 state,
527 					u16 chanspec, u16 listen_ms)
528 {
529 	struct brcmf_p2p_disc_st_le discover_state;
530 	s32 ret = 0;
531 	brcmf_dbg(TRACE, "enter\n");
532 
533 	discover_state.state = state;
534 	discover_state.chspec = cpu_to_le16(chanspec);
535 	discover_state.dwell = cpu_to_le16(listen_ms);
536 	ret = brcmf_fil_bsscfg_data_set(ifp, "p2p_state", &discover_state,
537 					sizeof(discover_state));
538 	return ret;
539 }
540 
541 /**
542  * brcmf_p2p_deinit_discovery() - disable P2P device discovery.
543  *
544  * @p2p: P2P specific data.
545  *
546  * Resets the discovery state and disables it in firmware.
547  */
548 static s32 brcmf_p2p_deinit_discovery(struct brcmf_p2p_info *p2p)
549 {
550 	struct brcmf_cfg80211_vif *vif;
551 
552 	brcmf_dbg(TRACE, "enter\n");
553 
554 	/* Set the discovery state to SCAN */
555 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
556 	(void)brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
557 
558 	/* Disable P2P discovery in the firmware */
559 	vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
560 	(void)brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 0);
561 
562 	return 0;
563 }
564 
565 /**
566  * brcmf_p2p_enable_discovery() - initialize and configure discovery.
567  *
568  * @p2p: P2P specific data.
569  *
570  * Initializes the discovery device and configure the virtual interface.
571  */
572 static int brcmf_p2p_enable_discovery(struct brcmf_p2p_info *p2p)
573 {
574 	struct brcmf_pub *drvr = p2p->cfg->pub;
575 	struct brcmf_cfg80211_vif *vif;
576 	s32 ret = 0;
577 
578 	brcmf_dbg(TRACE, "enter\n");
579 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
580 	if (!vif) {
581 		bphy_err(drvr, "P2P config device not available\n");
582 		ret = -EPERM;
583 		goto exit;
584 	}
585 
586 	if (test_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status)) {
587 		brcmf_dbg(INFO, "P2P config device already configured\n");
588 		goto exit;
589 	}
590 
591 	/* Re-initialize P2P Discovery in the firmware */
592 	vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
593 	ret = brcmf_fil_iovar_int_set(vif->ifp, "p2p_disc", 1);
594 	if (ret < 0) {
595 		bphy_err(drvr, "set p2p_disc error\n");
596 		goto exit;
597 	}
598 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
599 	ret = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
600 	if (ret < 0) {
601 		bphy_err(drvr, "unable to set WL_P2P_DISC_ST_SCAN\n");
602 		goto exit;
603 	}
604 
605 	/*
606 	 * Set wsec to any non-zero value in the discovery bsscfg
607 	 * to ensure our P2P probe responses have the privacy bit
608 	 * set in the 802.11 WPA IE. Some peer devices may not
609 	 * initiate WPS with us if this bit is not set.
610 	 */
611 	ret = brcmf_fil_bsscfg_int_set(vif->ifp, "wsec", AES_ENABLED);
612 	if (ret < 0) {
613 		bphy_err(drvr, "wsec error %d\n", ret);
614 		goto exit;
615 	}
616 
617 	set_bit(BRCMF_P2P_STATUS_ENABLED, &p2p->status);
618 exit:
619 	return ret;
620 }
621 
622 /**
623  * brcmf_p2p_escan() - initiate a P2P scan.
624  *
625  * @p2p: P2P specific data.
626  * @num_chans: number of channels to scan.
627  * @chanspecs: channel parameters for @num_chans channels.
628  * @search_state: P2P discover state to use.
629  * @bss_type: type of P2P bss.
630  */
631 static s32 brcmf_p2p_escan(struct brcmf_p2p_info *p2p, u32 num_chans,
632 			   u16 chanspecs[], s32 search_state,
633 			   enum p2p_bss_type bss_type)
634 {
635 	struct brcmf_pub *drvr = p2p->cfg->pub;
636 	s32 ret = 0;
637 	s32 memsize = offsetof(struct brcmf_p2p_scan_le,
638 			       eparams.params_le.channel_list);
639 	s32 nprobes;
640 	s32 active;
641 	u32 i;
642 	u8 *memblk;
643 	struct brcmf_cfg80211_vif *vif;
644 	struct brcmf_p2p_scan_le *p2p_params;
645 	struct brcmf_scan_params_le *sparams;
646 
647 	memsize += num_chans * sizeof(__le16);
648 	memblk = kzalloc(memsize, GFP_KERNEL);
649 	if (!memblk)
650 		return -ENOMEM;
651 
652 	vif = p2p->bss_idx[bss_type].vif;
653 	if (vif == NULL) {
654 		bphy_err(drvr, "no vif for bss type %d\n", bss_type);
655 		ret = -EINVAL;
656 		goto exit;
657 	}
658 	p2p_params = (struct brcmf_p2p_scan_le *)memblk;
659 	sparams = &p2p_params->eparams.params_le;
660 
661 	switch (search_state) {
662 	case WL_P2P_DISC_ST_SEARCH:
663 		/*
664 		 * If we in SEARCH STATE, we don't need to set SSID explictly
665 		 * because dongle use P2P WILDCARD internally by default, use
666 		 * null ssid, which it is already due to kzalloc.
667 		 */
668 		break;
669 	case WL_P2P_DISC_ST_SCAN:
670 		/*
671 		 * wpa_supplicant has p2p_find command with type social or
672 		 * progressive. For progressive, we need to set the ssid to
673 		 * P2P WILDCARD because we just do broadcast scan unless
674 		 * setting SSID.
675 		 */
676 		sparams->ssid_le.SSID_len =
677 				cpu_to_le32(BRCMF_P2P_WILDCARD_SSID_LEN);
678 		memcpy(sparams->ssid_le.SSID, BRCMF_P2P_WILDCARD_SSID,
679 		       BRCMF_P2P_WILDCARD_SSID_LEN);
680 		break;
681 	default:
682 		bphy_err(drvr, " invalid search state %d\n", search_state);
683 		ret = -EINVAL;
684 		goto exit;
685 	}
686 
687 	brcmf_p2p_set_discover_state(vif->ifp, search_state, 0, 0);
688 
689 	/*
690 	 * set p2p scan parameters.
691 	 */
692 	p2p_params->type = 'E';
693 
694 	/* determine the scan engine parameters */
695 	sparams->bss_type = DOT11_BSSTYPE_ANY;
696 	sparams->scan_type = BRCMF_SCANTYPE_ACTIVE;
697 
698 	eth_broadcast_addr(sparams->bssid);
699 	sparams->home_time = cpu_to_le32(P2PAPI_SCAN_HOME_TIME_MS);
700 
701 	/*
702 	 * SOCIAL_CHAN_CNT + 1 takes care of the Progressive scan
703 	 * supported by the supplicant.
704 	 */
705 	if (num_chans == SOCIAL_CHAN_CNT || num_chans == (SOCIAL_CHAN_CNT + 1))
706 		active = P2PAPI_SCAN_SOCIAL_DWELL_TIME_MS;
707 	else if (num_chans == AF_PEER_SEARCH_CNT)
708 		active = P2PAPI_SCAN_AF_SEARCH_DWELL_TIME_MS;
709 	else if (brcmf_get_vif_state_any(p2p->cfg, BRCMF_VIF_STATUS_CONNECTED))
710 		active = -1;
711 	else
712 		active = P2PAPI_SCAN_DWELL_TIME_MS;
713 
714 	/* Override scan params to find a peer for a connection */
715 	if (num_chans == 1) {
716 		active = WL_SCAN_CONNECT_DWELL_TIME_MS;
717 		/* WAR to sync with presence period of VSDB GO.
718 		 * send probe request more frequently
719 		 */
720 		nprobes = active / WL_SCAN_JOIN_PROBE_INTERVAL_MS;
721 	} else {
722 		nprobes = active / P2PAPI_SCAN_NPROBS_TIME_MS;
723 	}
724 
725 	if (nprobes <= 0)
726 		nprobes = 1;
727 
728 	brcmf_dbg(INFO, "nprobes # %d, active_time %d\n", nprobes, active);
729 	sparams->active_time = cpu_to_le32(active);
730 	sparams->nprobes = cpu_to_le32(nprobes);
731 	sparams->passive_time = cpu_to_le32(-1);
732 	sparams->channel_num = cpu_to_le32(num_chans &
733 					   BRCMF_SCAN_PARAMS_COUNT_MASK);
734 	for (i = 0; i < num_chans; i++)
735 		sparams->channel_list[i] = cpu_to_le16(chanspecs[i]);
736 
737 	/* set the escan specific parameters */
738 	p2p_params->eparams.version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
739 	p2p_params->eparams.action =  cpu_to_le16(WL_ESCAN_ACTION_START);
740 	p2p_params->eparams.sync_id = cpu_to_le16(0x1234);
741 	/* perform p2p scan on primary device */
742 	ret = brcmf_fil_bsscfg_data_set(vif->ifp, "p2p_scan", memblk, memsize);
743 	if (!ret)
744 		set_bit(BRCMF_SCAN_STATUS_BUSY, &p2p->cfg->scan_status);
745 exit:
746 	kfree(memblk);
747 	return ret;
748 }
749 
750 /**
751  * brcmf_p2p_run_escan() - escan callback for peer-to-peer.
752  *
753  * @cfg: driver private data for cfg80211 interface.
754  * @ndev: net device for which scan is requested.
755  * @request: scan request from cfg80211.
756  * @action: scan action.
757  *
758  * Determines the P2P discovery state based to scan request parameters and
759  * validates the channels in the request.
760  */
761 static s32 brcmf_p2p_run_escan(struct brcmf_cfg80211_info *cfg,
762 			       struct brcmf_if *ifp,
763 			       struct cfg80211_scan_request *request)
764 {
765 	struct brcmf_p2p_info *p2p = &cfg->p2p;
766 	struct brcmf_pub *drvr = cfg->pub;
767 	s32 err = 0;
768 	s32 search_state = WL_P2P_DISC_ST_SCAN;
769 	struct brcmf_cfg80211_vif *vif;
770 	struct net_device *dev = NULL;
771 	int i, num_nodfs = 0;
772 	u16 *chanspecs;
773 
774 	brcmf_dbg(TRACE, "enter\n");
775 
776 	if (!request) {
777 		err = -EINVAL;
778 		goto exit;
779 	}
780 
781 	if (request->n_channels) {
782 		chanspecs = kcalloc(request->n_channels, sizeof(*chanspecs),
783 				    GFP_KERNEL);
784 		if (!chanspecs) {
785 			err = -ENOMEM;
786 			goto exit;
787 		}
788 		vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
789 		if (vif)
790 			dev = vif->wdev.netdev;
791 		if (request->n_channels == 3 &&
792 		    request->channels[0]->hw_value == SOCIAL_CHAN_1 &&
793 		    request->channels[1]->hw_value == SOCIAL_CHAN_2 &&
794 		    request->channels[2]->hw_value == SOCIAL_CHAN_3) {
795 			/* SOCIAL CHANNELS 1, 6, 11 */
796 			search_state = WL_P2P_DISC_ST_SEARCH;
797 			brcmf_dbg(INFO, "P2P SEARCH PHASE START\n");
798 		} else if (dev != NULL &&
799 			   vif->wdev.iftype == NL80211_IFTYPE_P2P_GO) {
800 			/* If you are already a GO, then do SEARCH only */
801 			brcmf_dbg(INFO, "Already a GO. Do SEARCH Only\n");
802 			search_state = WL_P2P_DISC_ST_SEARCH;
803 		} else {
804 			brcmf_dbg(INFO, "P2P SCAN STATE START\n");
805 		}
806 
807 		/*
808 		 * no P2P scanning on passive or DFS channels.
809 		 */
810 		for (i = 0; i < request->n_channels; i++) {
811 			struct ieee80211_channel *chan = request->channels[i];
812 
813 			if (chan->flags & (IEEE80211_CHAN_RADAR |
814 					   IEEE80211_CHAN_NO_IR))
815 				continue;
816 
817 			chanspecs[i] = channel_to_chanspec(&p2p->cfg->d11inf,
818 							   chan);
819 			brcmf_dbg(INFO, "%d: chan=%d, channel spec=%x\n",
820 				  num_nodfs, chan->hw_value, chanspecs[i]);
821 			num_nodfs++;
822 		}
823 		err = brcmf_p2p_escan(p2p, num_nodfs, chanspecs, search_state,
824 				      P2PAPI_BSSCFG_DEVICE);
825 		kfree(chanspecs);
826 	}
827 exit:
828 	if (err)
829 		bphy_err(drvr, "error (%d)\n", err);
830 	return err;
831 }
832 
833 
834 /**
835  * brcmf_p2p_find_listen_channel() - find listen channel in ie string.
836  *
837  * @ie: string of information elements.
838  * @ie_len: length of string.
839  *
840  * Scan ie for p2p ie and look for attribute 6 channel. If available determine
841  * channel and return it.
842  */
843 static s32 brcmf_p2p_find_listen_channel(const u8 *ie, u32 ie_len)
844 {
845 	u8 channel_ie[5];
846 	s32 listen_channel;
847 	s32 err;
848 
849 	err = cfg80211_get_p2p_attr(ie, ie_len,
850 				    IEEE80211_P2P_ATTR_LISTEN_CHANNEL,
851 				    channel_ie, sizeof(channel_ie));
852 	if (err < 0)
853 		return err;
854 
855 	/* listen channel subel length format:     */
856 	/* 3(country) + 1(op. class) + 1(chan num) */
857 	listen_channel = (s32)channel_ie[3 + 1];
858 
859 	if (listen_channel == SOCIAL_CHAN_1 ||
860 	    listen_channel == SOCIAL_CHAN_2 ||
861 	    listen_channel == SOCIAL_CHAN_3) {
862 		brcmf_dbg(INFO, "Found my Listen Channel %d\n", listen_channel);
863 		return listen_channel;
864 	}
865 
866 	return -EPERM;
867 }
868 
869 
870 /**
871  * brcmf_p2p_scan_prep() - prepare scan based on request.
872  *
873  * @wiphy: wiphy device.
874  * @request: scan request from cfg80211.
875  * @vif: vif on which scan request is to be executed.
876  *
877  * Prepare the scan appropriately for type of scan requested. Overrides the
878  * escan .run() callback for peer-to-peer scanning.
879  */
880 int brcmf_p2p_scan_prep(struct wiphy *wiphy,
881 			struct cfg80211_scan_request *request,
882 			struct brcmf_cfg80211_vif *vif)
883 {
884 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
885 	struct brcmf_p2p_info *p2p = &cfg->p2p;
886 	int err;
887 
888 	if (brcmf_p2p_scan_is_p2p_request(request)) {
889 		/* find my listen channel */
890 		err = brcmf_p2p_find_listen_channel(request->ie,
891 						    request->ie_len);
892 		if (err < 0)
893 			return err;
894 
895 		p2p->afx_hdl.my_listen_chan = err;
896 
897 		clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
898 		brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
899 
900 		err = brcmf_p2p_enable_discovery(p2p);
901 		if (err)
902 			return err;
903 
904 		vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
905 
906 		/* override .run_escan() callback. */
907 		cfg->escan_info.run = brcmf_p2p_run_escan;
908 	}
909 	return 0;
910 }
911 
912 
913 /**
914  * brcmf_p2p_discover_listen() - set firmware to discover listen state.
915  *
916  * @p2p: p2p device.
917  * @channel: channel nr for discover listen.
918  * @duration: time in ms to stay on channel.
919  *
920  */
921 static s32
922 brcmf_p2p_discover_listen(struct brcmf_p2p_info *p2p, u16 channel, u32 duration)
923 {
924 	struct brcmf_pub *drvr = p2p->cfg->pub;
925 	struct brcmf_cfg80211_vif *vif;
926 	struct brcmu_chan ch;
927 	s32 err = 0;
928 
929 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
930 	if (!vif) {
931 		bphy_err(drvr, "Discovery is not set, so we have nothing to do\n");
932 		err = -EPERM;
933 		goto exit;
934 	}
935 
936 	if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status)) {
937 		bphy_err(drvr, "Previous LISTEN is not completed yet\n");
938 		/* WAR: prevent cookie mismatch in wpa_supplicant return OK */
939 		goto exit;
940 	}
941 
942 	ch.chnum = channel;
943 	ch.bw = BRCMU_CHAN_BW_20;
944 	p2p->cfg->d11inf.encchspec(&ch);
945 	err = brcmf_p2p_set_discover_state(vif->ifp, WL_P2P_DISC_ST_LISTEN,
946 					   ch.chspec, (u16)duration);
947 	if (!err) {
948 		set_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status);
949 		p2p->remain_on_channel_cookie++;
950 	}
951 exit:
952 	return err;
953 }
954 
955 
956 /**
957  * brcmf_p2p_remain_on_channel() - put device on channel and stay there.
958  *
959  * @wiphy: wiphy device.
960  * @channel: channel to stay on.
961  * @duration: time in ms to remain on channel.
962  *
963  */
964 int brcmf_p2p_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
965 				struct ieee80211_channel *channel,
966 				unsigned int duration, u64 *cookie)
967 {
968 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
969 	struct brcmf_p2p_info *p2p = &cfg->p2p;
970 	s32 err;
971 	u16 channel_nr;
972 
973 	channel_nr = ieee80211_frequency_to_channel(channel->center_freq);
974 	brcmf_dbg(TRACE, "Enter, channel: %d, duration ms (%d)\n", channel_nr,
975 		  duration);
976 
977 	err = brcmf_p2p_enable_discovery(p2p);
978 	if (err)
979 		goto exit;
980 	err = brcmf_p2p_discover_listen(p2p, channel_nr, duration);
981 	if (err)
982 		goto exit;
983 
984 	memcpy(&p2p->remain_on_channel, channel, sizeof(*channel));
985 	*cookie = p2p->remain_on_channel_cookie;
986 	cfg80211_ready_on_channel(wdev, *cookie, channel, duration, GFP_KERNEL);
987 
988 exit:
989 	return err;
990 }
991 
992 
993 /**
994  * brcmf_p2p_notify_listen_complete() - p2p listen has completed.
995  *
996  * @ifp: interfac control.
997  * @e: event message. Not used, to make it usable for fweh event dispatcher.
998  * @data: payload of message. Not used.
999  *
1000  */
1001 int brcmf_p2p_notify_listen_complete(struct brcmf_if *ifp,
1002 				     const struct brcmf_event_msg *e,
1003 				     void *data)
1004 {
1005 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1006 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1007 
1008 	brcmf_dbg(TRACE, "Enter\n");
1009 	if (test_and_clear_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN,
1010 			       &p2p->status)) {
1011 		if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1012 				       &p2p->status)) {
1013 			clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1014 				  &p2p->status);
1015 			brcmf_dbg(INFO, "Listen DONE, wake up wait_next_af\n");
1016 			complete(&p2p->wait_next_af);
1017 		}
1018 
1019 		cfg80211_remain_on_channel_expired(&ifp->vif->wdev,
1020 						   p2p->remain_on_channel_cookie,
1021 						   &p2p->remain_on_channel,
1022 						   GFP_KERNEL);
1023 	}
1024 	return 0;
1025 }
1026 
1027 
1028 /**
1029  * brcmf_p2p_cancel_remain_on_channel() - cancel p2p listen state.
1030  *
1031  * @ifp: interfac control.
1032  *
1033  */
1034 void brcmf_p2p_cancel_remain_on_channel(struct brcmf_if *ifp)
1035 {
1036 	if (!ifp)
1037 		return;
1038 	brcmf_p2p_set_discover_state(ifp, WL_P2P_DISC_ST_SCAN, 0, 0);
1039 	brcmf_p2p_notify_listen_complete(ifp, NULL, NULL);
1040 }
1041 
1042 
1043 /**
1044  * brcmf_p2p_act_frm_search() - search function for action frame.
1045  *
1046  * @p2p: p2p device.
1047  * channel: channel on which action frame is to be trasmitted.
1048  *
1049  * search function to reach at common channel to send action frame. When
1050  * channel is 0 then all social channels will be used to send af
1051  */
1052 static s32 brcmf_p2p_act_frm_search(struct brcmf_p2p_info *p2p, u16 channel)
1053 {
1054 	struct brcmf_pub *drvr = p2p->cfg->pub;
1055 	s32 err;
1056 	u32 channel_cnt;
1057 	u16 *default_chan_list;
1058 	u32 i;
1059 	struct brcmu_chan ch;
1060 
1061 	brcmf_dbg(TRACE, "Enter\n");
1062 
1063 	if (channel)
1064 		channel_cnt = AF_PEER_SEARCH_CNT;
1065 	else
1066 		channel_cnt = SOCIAL_CHAN_CNT;
1067 	default_chan_list = kcalloc(channel_cnt, sizeof(*default_chan_list),
1068 				    GFP_KERNEL);
1069 	if (default_chan_list == NULL) {
1070 		bphy_err(drvr, "channel list allocation failed\n");
1071 		err = -ENOMEM;
1072 		goto exit;
1073 	}
1074 	ch.bw = BRCMU_CHAN_BW_20;
1075 	if (channel) {
1076 		ch.chnum = channel;
1077 		p2p->cfg->d11inf.encchspec(&ch);
1078 		/* insert same channel to the chan_list */
1079 		for (i = 0; i < channel_cnt; i++)
1080 			default_chan_list[i] = ch.chspec;
1081 	} else {
1082 		ch.chnum = SOCIAL_CHAN_1;
1083 		p2p->cfg->d11inf.encchspec(&ch);
1084 		default_chan_list[0] = ch.chspec;
1085 		ch.chnum = SOCIAL_CHAN_2;
1086 		p2p->cfg->d11inf.encchspec(&ch);
1087 		default_chan_list[1] = ch.chspec;
1088 		ch.chnum = SOCIAL_CHAN_3;
1089 		p2p->cfg->d11inf.encchspec(&ch);
1090 		default_chan_list[2] = ch.chspec;
1091 	}
1092 	err = brcmf_p2p_escan(p2p, channel_cnt, default_chan_list,
1093 			      WL_P2P_DISC_ST_SEARCH, P2PAPI_BSSCFG_DEVICE);
1094 	kfree(default_chan_list);
1095 exit:
1096 	return err;
1097 }
1098 
1099 
1100 /**
1101  * brcmf_p2p_afx_handler() - afx worker thread.
1102  *
1103  * @work:
1104  *
1105  */
1106 static void brcmf_p2p_afx_handler(struct work_struct *work)
1107 {
1108 	struct afx_hdl *afx_hdl = container_of(work, struct afx_hdl, afx_work);
1109 	struct brcmf_p2p_info *p2p = container_of(afx_hdl,
1110 						  struct brcmf_p2p_info,
1111 						  afx_hdl);
1112 	struct brcmf_pub *drvr = p2p->cfg->pub;
1113 	s32 err;
1114 
1115 	if (!afx_hdl->is_active)
1116 		return;
1117 
1118 	if (afx_hdl->is_listen && afx_hdl->my_listen_chan)
1119 		/* 100ms ~ 300ms */
1120 		err = brcmf_p2p_discover_listen(p2p, afx_hdl->my_listen_chan,
1121 						100 * (1 + prandom_u32() % 3));
1122 	else
1123 		err = brcmf_p2p_act_frm_search(p2p, afx_hdl->peer_listen_chan);
1124 
1125 	if (err) {
1126 		bphy_err(drvr, "ERROR occurred! value is (%d)\n", err);
1127 		if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1128 			     &p2p->status))
1129 			complete(&afx_hdl->act_frm_scan);
1130 	}
1131 }
1132 
1133 
1134 /**
1135  * brcmf_p2p_af_searching_channel() - search channel.
1136  *
1137  * @p2p: p2p device info struct.
1138  *
1139  */
1140 static s32 brcmf_p2p_af_searching_channel(struct brcmf_p2p_info *p2p)
1141 {
1142 	struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1143 	struct brcmf_cfg80211_vif *pri_vif;
1144 	s32 retry;
1145 
1146 	brcmf_dbg(TRACE, "Enter\n");
1147 
1148 	pri_vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1149 
1150 	reinit_completion(&afx_hdl->act_frm_scan);
1151 	set_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1152 	afx_hdl->is_active = true;
1153 	afx_hdl->peer_chan = P2P_INVALID_CHANNEL;
1154 
1155 	/* Loop to wait until we find a peer's channel or the
1156 	 * pending action frame tx is cancelled.
1157 	 */
1158 	retry = 0;
1159 	while ((retry < P2P_CHANNEL_SYNC_RETRY) &&
1160 	       (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)) {
1161 		afx_hdl->is_listen = false;
1162 		brcmf_dbg(TRACE, "Scheduling action frame for sending.. (%d)\n",
1163 			  retry);
1164 		/* search peer on peer's listen channel */
1165 		schedule_work(&afx_hdl->afx_work);
1166 		wait_for_completion_timeout(&afx_hdl->act_frm_scan,
1167 					    P2P_AF_FRM_SCAN_MAX_WAIT);
1168 		if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1169 		    (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1170 			       &p2p->status)))
1171 			break;
1172 
1173 		if (afx_hdl->my_listen_chan) {
1174 			brcmf_dbg(TRACE, "Scheduling listen peer, channel=%d\n",
1175 				  afx_hdl->my_listen_chan);
1176 			/* listen on my listen channel */
1177 			afx_hdl->is_listen = true;
1178 			schedule_work(&afx_hdl->afx_work);
1179 			wait_for_completion_timeout(&afx_hdl->act_frm_scan,
1180 						    P2P_AF_FRM_SCAN_MAX_WAIT);
1181 		}
1182 		if ((afx_hdl->peer_chan != P2P_INVALID_CHANNEL) ||
1183 		    (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1184 			       &p2p->status)))
1185 			break;
1186 		retry++;
1187 
1188 		/* if sta is connected or connecting, sleep for a while before
1189 		 * retry af tx or finding a peer
1190 		 */
1191 		if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &pri_vif->sme_state) ||
1192 		    test_bit(BRCMF_VIF_STATUS_CONNECTING, &pri_vif->sme_state))
1193 			msleep(P2P_DEFAULT_SLEEP_TIME_VSDB);
1194 	}
1195 
1196 	brcmf_dbg(TRACE, "Completed search/listen peer_chan=%d\n",
1197 		  afx_hdl->peer_chan);
1198 	afx_hdl->is_active = false;
1199 
1200 	clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status);
1201 
1202 	return afx_hdl->peer_chan;
1203 }
1204 
1205 
1206 /**
1207  * brcmf_p2p_scan_finding_common_channel() - was escan used for finding channel
1208  *
1209  * @cfg: common configuration struct.
1210  * @bi: bss info struct, result from scan.
1211  *
1212  */
1213 bool brcmf_p2p_scan_finding_common_channel(struct brcmf_cfg80211_info *cfg,
1214 					   struct brcmf_bss_info_le *bi)
1215 
1216 {
1217 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1218 	struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1219 	struct brcmu_chan ch;
1220 	u8 *ie;
1221 	s32 err;
1222 	u8 p2p_dev_addr[ETH_ALEN];
1223 
1224 	if (!test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status))
1225 		return false;
1226 
1227 	if (bi == NULL) {
1228 		brcmf_dbg(TRACE, "ACTION FRAME SCAN Done\n");
1229 		if (afx_hdl->peer_chan == P2P_INVALID_CHANNEL)
1230 			complete(&afx_hdl->act_frm_scan);
1231 		return true;
1232 	}
1233 
1234 	ie = ((u8 *)bi) + le16_to_cpu(bi->ie_offset);
1235 	memset(p2p_dev_addr, 0, sizeof(p2p_dev_addr));
1236 	err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1237 				    IEEE80211_P2P_ATTR_DEVICE_INFO,
1238 				    p2p_dev_addr, sizeof(p2p_dev_addr));
1239 	if (err < 0)
1240 		err = cfg80211_get_p2p_attr(ie, le32_to_cpu(bi->ie_length),
1241 					    IEEE80211_P2P_ATTR_DEVICE_ID,
1242 					    p2p_dev_addr, sizeof(p2p_dev_addr));
1243 	if ((err >= 0) &&
1244 	    (ether_addr_equal(p2p_dev_addr, afx_hdl->tx_dst_addr))) {
1245 		if (!bi->ctl_ch) {
1246 			ch.chspec = le16_to_cpu(bi->chanspec);
1247 			cfg->d11inf.decchspec(&ch);
1248 			bi->ctl_ch = ch.control_ch_num;
1249 		}
1250 		afx_hdl->peer_chan = bi->ctl_ch;
1251 		brcmf_dbg(TRACE, "ACTION FRAME SCAN : Peer %pM found, channel : %d\n",
1252 			  afx_hdl->tx_dst_addr, afx_hdl->peer_chan);
1253 		complete(&afx_hdl->act_frm_scan);
1254 	}
1255 	return true;
1256 }
1257 
1258 /**
1259  * brcmf_p2p_stop_wait_next_action_frame() - finish scan if af tx complete.
1260  *
1261  * @cfg: common configuration struct.
1262  *
1263  */
1264 static void
1265 brcmf_p2p_stop_wait_next_action_frame(struct brcmf_cfg80211_info *cfg)
1266 {
1267 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1268 	struct brcmf_if *ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
1269 
1270 	if (test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status) &&
1271 	    (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status) ||
1272 	     test_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status))) {
1273 		brcmf_dbg(TRACE, "*** Wake UP ** abort actframe iovar\n");
1274 		/* if channel is not zero, "actfame" uses off channel scan.
1275 		 * So abort scan for off channel completion.
1276 		 */
1277 		if (p2p->af_sent_channel)
1278 			brcmf_notify_escan_complete(cfg, ifp, true, true);
1279 	} else if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1280 			    &p2p->status)) {
1281 		brcmf_dbg(TRACE, "*** Wake UP ** abort listen for next af frame\n");
1282 		/* So abort scan to cancel listen */
1283 		brcmf_notify_escan_complete(cfg, ifp, true, true);
1284 	}
1285 }
1286 
1287 
1288 /**
1289  * brcmf_p2p_gon_req_collision() - Check if go negotiaton collission
1290  *
1291  * @p2p: p2p device info struct.
1292  *
1293  * return true if recevied action frame is to be dropped.
1294  */
1295 static bool
1296 brcmf_p2p_gon_req_collision(struct brcmf_p2p_info *p2p, u8 *mac)
1297 {
1298 	struct brcmf_cfg80211_info *cfg = p2p->cfg;
1299 	struct brcmf_if *ifp;
1300 
1301 	brcmf_dbg(TRACE, "Enter\n");
1302 
1303 	if (!test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) ||
1304 	    !p2p->gon_req_action)
1305 		return false;
1306 
1307 	brcmf_dbg(TRACE, "GO Negotiation Request COLLISION !!!\n");
1308 	/* if sa(peer) addr is less than da(my) addr, then this device
1309 	 * process peer's gon request and block to send gon req.
1310 	 * if not (sa addr > da addr),
1311 	 * this device will process gon request and drop gon req of peer.
1312 	 */
1313 	ifp = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->ifp;
1314 	if (memcmp(mac, ifp->mac_addr, ETH_ALEN) < 0) {
1315 		brcmf_dbg(INFO, "Block transmit gon req !!!\n");
1316 		p2p->block_gon_req_tx = true;
1317 		/* if we are finding a common channel for sending af,
1318 		 * do not scan more to block to send current gon req
1319 		 */
1320 		if (test_and_clear_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1321 				       &p2p->status))
1322 			complete(&p2p->afx_hdl.act_frm_scan);
1323 		if (test_and_clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1324 				       &p2p->status))
1325 			brcmf_p2p_stop_wait_next_action_frame(cfg);
1326 		return false;
1327 	}
1328 
1329 	/* drop gon request of peer to process gon request by this device. */
1330 	brcmf_dbg(INFO, "Drop received gon req !!!\n");
1331 
1332 	return true;
1333 }
1334 
1335 
1336 /**
1337  * brcmf_p2p_notify_action_frame_rx() - received action frame.
1338  *
1339  * @ifp: interfac control.
1340  * @e: event message. Not used, to make it usable for fweh event dispatcher.
1341  * @data: payload of message, containing action frame data.
1342  *
1343  */
1344 int brcmf_p2p_notify_action_frame_rx(struct brcmf_if *ifp,
1345 				     const struct brcmf_event_msg *e,
1346 				     void *data)
1347 {
1348 	struct brcmf_pub *drvr = ifp->drvr;
1349 	struct brcmf_cfg80211_info *cfg = drvr->config;
1350 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1351 	struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1352 	struct wireless_dev *wdev;
1353 	u32 mgmt_frame_len = e->datalen - sizeof(struct brcmf_rx_mgmt_data);
1354 	struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
1355 	u8 *frame = (u8 *)(rxframe + 1);
1356 	struct brcmf_p2p_pub_act_frame *act_frm;
1357 	struct brcmf_p2psd_gas_pub_act_frame *sd_act_frm;
1358 	struct brcmu_chan ch;
1359 	struct ieee80211_mgmt *mgmt_frame;
1360 	s32 freq;
1361 	u16 mgmt_type;
1362 	u8 action;
1363 
1364 	if (e->datalen < sizeof(*rxframe)) {
1365 		brcmf_dbg(SCAN, "Event data to small. Ignore\n");
1366 		return 0;
1367 	}
1368 
1369 	ch.chspec = be16_to_cpu(rxframe->chanspec);
1370 	cfg->d11inf.decchspec(&ch);
1371 	/* Check if wpa_supplicant has registered for this frame */
1372 	brcmf_dbg(INFO, "ifp->vif->mgmt_rx_reg %04x\n", ifp->vif->mgmt_rx_reg);
1373 	mgmt_type = (IEEE80211_STYPE_ACTION & IEEE80211_FCTL_STYPE) >> 4;
1374 	if ((ifp->vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1375 		return 0;
1376 
1377 	brcmf_p2p_print_actframe(false, frame, mgmt_frame_len);
1378 
1379 	action = P2P_PAF_SUBTYPE_INVALID;
1380 	if (brcmf_p2p_is_pub_action(frame, mgmt_frame_len)) {
1381 		act_frm = (struct brcmf_p2p_pub_act_frame *)frame;
1382 		action = act_frm->subtype;
1383 		if ((action == P2P_PAF_GON_REQ) &&
1384 		    (brcmf_p2p_gon_req_collision(p2p, (u8 *)e->addr))) {
1385 			if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL,
1386 				     &p2p->status) &&
1387 			    (ether_addr_equal(afx_hdl->tx_dst_addr, e->addr))) {
1388 				afx_hdl->peer_chan = ch.control_ch_num;
1389 				brcmf_dbg(INFO, "GON request: Peer found, channel=%d\n",
1390 					  afx_hdl->peer_chan);
1391 				complete(&afx_hdl->act_frm_scan);
1392 			}
1393 			return 0;
1394 		}
1395 		/* After complete GO Negotiation, roll back to mpc mode */
1396 		if ((action == P2P_PAF_GON_CONF) ||
1397 		    (action == P2P_PAF_PROVDIS_RSP))
1398 			brcmf_set_mpc(ifp, 1);
1399 		if (action == P2P_PAF_GON_CONF) {
1400 			brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1401 			clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1402 		}
1403 	} else if (brcmf_p2p_is_gas_action(frame, mgmt_frame_len)) {
1404 		sd_act_frm = (struct brcmf_p2psd_gas_pub_act_frame *)frame;
1405 		action = sd_act_frm->action;
1406 	}
1407 
1408 	if (test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1409 	    (p2p->next_af_subtype == action)) {
1410 		brcmf_dbg(TRACE, "We got a right next frame! (%d)\n", action);
1411 		clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME,
1412 			  &p2p->status);
1413 		/* Stop waiting for next AF. */
1414 		brcmf_p2p_stop_wait_next_action_frame(cfg);
1415 	}
1416 
1417 	mgmt_frame = kzalloc(offsetof(struct ieee80211_mgmt, u) +
1418 			     mgmt_frame_len, GFP_KERNEL);
1419 	if (!mgmt_frame) {
1420 		bphy_err(drvr, "No memory available for action frame\n");
1421 		return -ENOMEM;
1422 	}
1423 	memcpy(mgmt_frame->da, ifp->mac_addr, ETH_ALEN);
1424 	brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mgmt_frame->bssid,
1425 			       ETH_ALEN);
1426 	memcpy(mgmt_frame->sa, e->addr, ETH_ALEN);
1427 	mgmt_frame->frame_control = cpu_to_le16(IEEE80211_STYPE_ACTION);
1428 	memcpy(&mgmt_frame->u, frame, mgmt_frame_len);
1429 	mgmt_frame_len += offsetof(struct ieee80211_mgmt, u);
1430 
1431 	freq = ieee80211_channel_to_frequency(ch.control_ch_num,
1432 					      ch.band == BRCMU_CHAN_BAND_2G ?
1433 					      NL80211_BAND_2GHZ :
1434 					      NL80211_BAND_5GHZ);
1435 
1436 	wdev = &ifp->vif->wdev;
1437 	cfg80211_rx_mgmt(wdev, freq, 0, (u8 *)mgmt_frame, mgmt_frame_len, 0);
1438 
1439 	kfree(mgmt_frame);
1440 	return 0;
1441 }
1442 
1443 
1444 /**
1445  * brcmf_p2p_notify_action_tx_complete() - transmit action frame complete
1446  *
1447  * @ifp: interfac control.
1448  * @e: event message. Not used, to make it usable for fweh event dispatcher.
1449  * @data: not used.
1450  *
1451  */
1452 int brcmf_p2p_notify_action_tx_complete(struct brcmf_if *ifp,
1453 					const struct brcmf_event_msg *e,
1454 					void *data)
1455 {
1456 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1457 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1458 
1459 	brcmf_dbg(INFO, "Enter: event %s, status=%d\n",
1460 		  e->event_code == BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE ?
1461 		  "ACTION_FRAME_OFF_CHAN_COMPLETE" : "ACTION_FRAME_COMPLETE",
1462 		  e->status);
1463 
1464 	if (!test_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status))
1465 		return 0;
1466 
1467 	if (e->event_code == BRCMF_E_ACTION_FRAME_COMPLETE) {
1468 		if (e->status == BRCMF_E_STATUS_SUCCESS) {
1469 			set_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED,
1470 				&p2p->status);
1471 			if (!p2p->wait_for_offchan_complete)
1472 				complete(&p2p->send_af_done);
1473 		} else {
1474 			set_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1475 			/* If there is no ack, we don't need to wait for
1476 			 * WLC_E_ACTION_FRAME_OFFCHAN_COMPLETE event
1477 			 */
1478 			brcmf_p2p_stop_wait_next_action_frame(cfg);
1479 		}
1480 
1481 	} else {
1482 		complete(&p2p->send_af_done);
1483 	}
1484 	return 0;
1485 }
1486 
1487 
1488 /**
1489  * brcmf_p2p_tx_action_frame() - send action frame over fil.
1490  *
1491  * @p2p: p2p info struct for vif.
1492  * @af_params: action frame data/info.
1493  *
1494  * Send an action frame immediately without doing channel synchronization.
1495  *
1496  * This function waits for a completion event before returning.
1497  * The WLC_E_ACTION_FRAME_COMPLETE event will be received when the action
1498  * frame is transmitted.
1499  */
1500 static s32 brcmf_p2p_tx_action_frame(struct brcmf_p2p_info *p2p,
1501 				     struct brcmf_fil_af_params_le *af_params)
1502 {
1503 	struct brcmf_pub *drvr = p2p->cfg->pub;
1504 	struct brcmf_cfg80211_vif *vif;
1505 	s32 err = 0;
1506 	s32 timeout = 0;
1507 
1508 	brcmf_dbg(TRACE, "Enter\n");
1509 
1510 	reinit_completion(&p2p->send_af_done);
1511 	clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1512 	clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1513 
1514 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
1515 	err = brcmf_fil_bsscfg_data_set(vif->ifp, "actframe", af_params,
1516 					sizeof(*af_params));
1517 	if (err) {
1518 		bphy_err(drvr, " sending action frame has failed\n");
1519 		goto exit;
1520 	}
1521 
1522 	p2p->af_sent_channel = le32_to_cpu(af_params->channel);
1523 	p2p->af_tx_sent_jiffies = jiffies;
1524 
1525 	if (test_bit(BRCMF_P2P_STATUS_DISCOVER_LISTEN, &p2p->status) &&
1526 	    p2p->af_sent_channel ==
1527 	    ieee80211_frequency_to_channel(p2p->remain_on_channel.center_freq))
1528 		p2p->wait_for_offchan_complete = false;
1529 	else
1530 		p2p->wait_for_offchan_complete = true;
1531 
1532 	brcmf_dbg(TRACE, "Waiting for %s tx completion event\n",
1533 		  (p2p->wait_for_offchan_complete) ?
1534 		   "off-channel" : "on-channel");
1535 
1536 	timeout = wait_for_completion_timeout(&p2p->send_af_done,
1537 					      P2P_AF_MAX_WAIT_TIME);
1538 
1539 	if (test_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status)) {
1540 		brcmf_dbg(TRACE, "TX action frame operation is success\n");
1541 	} else {
1542 		err = -EIO;
1543 		brcmf_dbg(TRACE, "TX action frame operation has failed\n");
1544 	}
1545 	/* clear status bit for action tx */
1546 	clear_bit(BRCMF_P2P_STATUS_ACTION_TX_COMPLETED, &p2p->status);
1547 	clear_bit(BRCMF_P2P_STATUS_ACTION_TX_NOACK, &p2p->status);
1548 
1549 exit:
1550 	return err;
1551 }
1552 
1553 
1554 /**
1555  * brcmf_p2p_pub_af_tx() - public action frame tx routine.
1556  *
1557  * @cfg: driver private data for cfg80211 interface.
1558  * @af_params: action frame data/info.
1559  * @config_af_params: configuration data for action frame.
1560  *
1561  * routine which transmits ation frame public type.
1562  */
1563 static s32 brcmf_p2p_pub_af_tx(struct brcmf_cfg80211_info *cfg,
1564 			       struct brcmf_fil_af_params_le *af_params,
1565 			       struct brcmf_config_af_params *config_af_params)
1566 {
1567 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1568 	struct brcmf_pub *drvr = cfg->pub;
1569 	struct brcmf_fil_action_frame_le *action_frame;
1570 	struct brcmf_p2p_pub_act_frame *act_frm;
1571 	s32 err = 0;
1572 	u16 ie_len;
1573 
1574 	action_frame = &af_params->action_frame;
1575 	act_frm = (struct brcmf_p2p_pub_act_frame *)(action_frame->data);
1576 
1577 	config_af_params->extra_listen = true;
1578 
1579 	switch (act_frm->subtype) {
1580 	case P2P_PAF_GON_REQ:
1581 		brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status set\n");
1582 		set_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1583 		config_af_params->mpc_onoff = 0;
1584 		config_af_params->search_channel = true;
1585 		p2p->next_af_subtype = act_frm->subtype + 1;
1586 		p2p->gon_req_action = true;
1587 		/* increase dwell time to wait for RESP frame */
1588 		af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1589 		break;
1590 	case P2P_PAF_GON_RSP:
1591 		p2p->next_af_subtype = act_frm->subtype + 1;
1592 		/* increase dwell time to wait for CONF frame */
1593 		af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1594 		break;
1595 	case P2P_PAF_GON_CONF:
1596 		/* If we reached till GO Neg confirmation reset the filter */
1597 		brcmf_dbg(TRACE, "P2P: GO_NEG_PHASE status cleared\n");
1598 		clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1599 		/* turn on mpc again if go nego is done */
1600 		config_af_params->mpc_onoff = 1;
1601 		/* minimize dwell time */
1602 		af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1603 		config_af_params->extra_listen = false;
1604 		break;
1605 	case P2P_PAF_INVITE_REQ:
1606 		config_af_params->search_channel = true;
1607 		p2p->next_af_subtype = act_frm->subtype + 1;
1608 		/* increase dwell time */
1609 		af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1610 		break;
1611 	case P2P_PAF_INVITE_RSP:
1612 		/* minimize dwell time */
1613 		af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1614 		config_af_params->extra_listen = false;
1615 		break;
1616 	case P2P_PAF_DEVDIS_REQ:
1617 		config_af_params->search_channel = true;
1618 		p2p->next_af_subtype = act_frm->subtype + 1;
1619 		/* maximize dwell time to wait for RESP frame */
1620 		af_params->dwell_time = cpu_to_le32(P2P_AF_LONG_DWELL_TIME);
1621 		break;
1622 	case P2P_PAF_DEVDIS_RSP:
1623 		/* minimize dwell time */
1624 		af_params->dwell_time = cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1625 		config_af_params->extra_listen = false;
1626 		break;
1627 	case P2P_PAF_PROVDIS_REQ:
1628 		ie_len = le16_to_cpu(action_frame->len) -
1629 			 offsetof(struct brcmf_p2p_pub_act_frame, elts);
1630 		if (cfg80211_get_p2p_attr(&act_frm->elts[0], ie_len,
1631 					  IEEE80211_P2P_ATTR_GROUP_ID,
1632 					  NULL, 0) < 0)
1633 			config_af_params->search_channel = true;
1634 		config_af_params->mpc_onoff = 0;
1635 		p2p->next_af_subtype = act_frm->subtype + 1;
1636 		/* increase dwell time to wait for RESP frame */
1637 		af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1638 		break;
1639 	case P2P_PAF_PROVDIS_RSP:
1640 		/* wpa_supplicant send go nego req right after prov disc */
1641 		p2p->next_af_subtype = P2P_PAF_GON_REQ;
1642 		/* increase dwell time to MED level */
1643 		af_params->dwell_time = cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1644 		config_af_params->extra_listen = false;
1645 		break;
1646 	default:
1647 		bphy_err(drvr, "Unknown p2p pub act frame subtype: %d\n",
1648 			 act_frm->subtype);
1649 		err = -EINVAL;
1650 	}
1651 	return err;
1652 }
1653 
1654 /**
1655  * brcmf_p2p_send_action_frame() - send action frame .
1656  *
1657  * @cfg: driver private data for cfg80211 interface.
1658  * @ndev: net device to transmit on.
1659  * @af_params: configuration data for action frame.
1660  */
1661 bool brcmf_p2p_send_action_frame(struct brcmf_cfg80211_info *cfg,
1662 				 struct net_device *ndev,
1663 				 struct brcmf_fil_af_params_le *af_params)
1664 {
1665 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1666 	struct brcmf_if *ifp = netdev_priv(ndev);
1667 	struct brcmf_fil_action_frame_le *action_frame;
1668 	struct brcmf_config_af_params config_af_params;
1669 	struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1670 	struct brcmf_pub *drvr = cfg->pub;
1671 	u16 action_frame_len;
1672 	bool ack = false;
1673 	u8 category;
1674 	u8 action;
1675 	s32 tx_retry;
1676 	s32 extra_listen_time;
1677 	uint delta_ms;
1678 
1679 	action_frame = &af_params->action_frame;
1680 	action_frame_len = le16_to_cpu(action_frame->len);
1681 
1682 	brcmf_p2p_print_actframe(true, action_frame->data, action_frame_len);
1683 
1684 	/* Add the default dwell time. Dwell time to stay off-channel */
1685 	/* to wait for a response action frame after transmitting an  */
1686 	/* GO Negotiation action frame                                */
1687 	af_params->dwell_time = cpu_to_le32(P2P_AF_DWELL_TIME);
1688 
1689 	category = action_frame->data[DOT11_ACTION_CAT_OFF];
1690 	action = action_frame->data[DOT11_ACTION_ACT_OFF];
1691 
1692 	/* initialize variables */
1693 	p2p->next_af_subtype = P2P_PAF_SUBTYPE_INVALID;
1694 	p2p->gon_req_action = false;
1695 
1696 	/* config parameters */
1697 	config_af_params.mpc_onoff = -1;
1698 	config_af_params.search_channel = false;
1699 	config_af_params.extra_listen = false;
1700 
1701 	if (brcmf_p2p_is_pub_action(action_frame->data, action_frame_len)) {
1702 		/* p2p public action frame process */
1703 		if (brcmf_p2p_pub_af_tx(cfg, af_params, &config_af_params)) {
1704 			/* Just send unknown subtype frame with */
1705 			/* default parameters.                  */
1706 			bphy_err(drvr, "P2P Public action frame, unknown subtype.\n");
1707 		}
1708 	} else if (brcmf_p2p_is_gas_action(action_frame->data,
1709 					   action_frame_len)) {
1710 		/* service discovery process */
1711 		if (action == P2PSD_ACTION_ID_GAS_IREQ ||
1712 		    action == P2PSD_ACTION_ID_GAS_CREQ) {
1713 			/* configure service discovery query frame */
1714 			config_af_params.search_channel = true;
1715 
1716 			/* save next af suptype to cancel */
1717 			/* remaining dwell time           */
1718 			p2p->next_af_subtype = action + 1;
1719 
1720 			af_params->dwell_time =
1721 				cpu_to_le32(P2P_AF_MED_DWELL_TIME);
1722 		} else if (action == P2PSD_ACTION_ID_GAS_IRESP ||
1723 			   action == P2PSD_ACTION_ID_GAS_CRESP) {
1724 			/* configure service discovery response frame */
1725 			af_params->dwell_time =
1726 				cpu_to_le32(P2P_AF_MIN_DWELL_TIME);
1727 		} else {
1728 			bphy_err(drvr, "Unknown action type: %d\n", action);
1729 			goto exit;
1730 		}
1731 	} else if (brcmf_p2p_is_p2p_action(action_frame->data,
1732 					   action_frame_len)) {
1733 		/* do not configure anything. it will be */
1734 		/* sent with a default configuration     */
1735 	} else {
1736 		bphy_err(drvr, "Unknown Frame: category 0x%x, action 0x%x\n",
1737 			 category, action);
1738 		return false;
1739 	}
1740 
1741 	/* if connecting on primary iface, sleep for a while before sending
1742 	 * af tx for VSDB
1743 	 */
1744 	if (test_bit(BRCMF_VIF_STATUS_CONNECTING,
1745 		     &p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->sme_state))
1746 		msleep(50);
1747 
1748 	/* if scan is ongoing, abort current scan. */
1749 	if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
1750 		brcmf_abort_scanning(cfg);
1751 
1752 	memcpy(afx_hdl->tx_dst_addr, action_frame->da, ETH_ALEN);
1753 
1754 	/* To make sure to send successfully action frame, turn off mpc */
1755 	if (config_af_params.mpc_onoff == 0)
1756 		brcmf_set_mpc(ifp, 0);
1757 
1758 	/* set status and destination address before sending af */
1759 	if (p2p->next_af_subtype != P2P_PAF_SUBTYPE_INVALID) {
1760 		/* set status to cancel the remained dwell time in rx process */
1761 		set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1762 	}
1763 
1764 	p2p->af_sent_channel = 0;
1765 	set_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1766 	/* validate channel and p2p ies */
1767 	if (config_af_params.search_channel &&
1768 	    IS_P2P_SOCIAL_CHANNEL(le32_to_cpu(af_params->channel)) &&
1769 	    p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif->saved_ie.probe_req_ie_len) {
1770 		afx_hdl = &p2p->afx_hdl;
1771 		afx_hdl->peer_listen_chan = le32_to_cpu(af_params->channel);
1772 
1773 		if (brcmf_p2p_af_searching_channel(p2p) ==
1774 							P2P_INVALID_CHANNEL) {
1775 			bphy_err(drvr, "Couldn't find peer's channel.\n");
1776 			goto exit;
1777 		}
1778 
1779 		/* Abort scan even for VSDB scenarios. Scan gets aborted in
1780 		 * firmware but after the check of piggyback algorithm. To take
1781 		 * care of current piggback algo, lets abort the scan here
1782 		 * itself.
1783 		 */
1784 		brcmf_notify_escan_complete(cfg, ifp, true, true);
1785 
1786 		/* update channel */
1787 		af_params->channel = cpu_to_le32(afx_hdl->peer_chan);
1788 	}
1789 
1790 	tx_retry = 0;
1791 	while (!p2p->block_gon_req_tx &&
1792 	       (ack == false) && (tx_retry < P2P_AF_TX_MAX_RETRY)) {
1793 		ack = !brcmf_p2p_tx_action_frame(p2p, af_params);
1794 		tx_retry++;
1795 	}
1796 	if (ack == false) {
1797 		bphy_err(drvr, "Failed to send Action Frame(retry %d)\n",
1798 			 tx_retry);
1799 		clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
1800 	}
1801 
1802 exit:
1803 	clear_bit(BRCMF_P2P_STATUS_SENDING_ACT_FRAME, &p2p->status);
1804 
1805 	/* WAR: sometimes dongle does not keep the dwell time of 'actframe'.
1806 	 * if we coundn't get the next action response frame and dongle does
1807 	 * not keep the dwell time, go to listen state again to get next action
1808 	 * response frame.
1809 	 */
1810 	if (ack && config_af_params.extra_listen && !p2p->block_gon_req_tx &&
1811 	    test_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status) &&
1812 	    p2p->af_sent_channel == afx_hdl->my_listen_chan) {
1813 		delta_ms = jiffies_to_msecs(jiffies - p2p->af_tx_sent_jiffies);
1814 		if (le32_to_cpu(af_params->dwell_time) > delta_ms)
1815 			extra_listen_time = le32_to_cpu(af_params->dwell_time) -
1816 					    delta_ms;
1817 		else
1818 			extra_listen_time = 0;
1819 		if (extra_listen_time > 50) {
1820 			set_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1821 				&p2p->status);
1822 			brcmf_dbg(INFO, "Wait more time! actual af time:%d, calculated extra listen:%d\n",
1823 				  le32_to_cpu(af_params->dwell_time),
1824 				  extra_listen_time);
1825 			extra_listen_time += 100;
1826 			if (!brcmf_p2p_discover_listen(p2p,
1827 						       p2p->af_sent_channel,
1828 						       extra_listen_time)) {
1829 				unsigned long duration;
1830 
1831 				extra_listen_time += 100;
1832 				duration = msecs_to_jiffies(extra_listen_time);
1833 				wait_for_completion_timeout(&p2p->wait_next_af,
1834 							    duration);
1835 			}
1836 			clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_AF_LISTEN,
1837 				  &p2p->status);
1838 		}
1839 	}
1840 
1841 	if (p2p->block_gon_req_tx) {
1842 		/* if ack is true, supplicant will wait more time(100ms).
1843 		 * so we will return it as a success to get more time .
1844 		 */
1845 		p2p->block_gon_req_tx = false;
1846 		ack = true;
1847 	}
1848 
1849 	clear_bit(BRCMF_P2P_STATUS_WAITING_NEXT_ACT_FRAME, &p2p->status);
1850 	/* if all done, turn mpc on again */
1851 	if (config_af_params.mpc_onoff == 1)
1852 		brcmf_set_mpc(ifp, 1);
1853 
1854 	return ack;
1855 }
1856 
1857 /**
1858  * brcmf_p2p_notify_rx_mgmt_p2p_probereq() - Event handler for p2p probe req.
1859  *
1860  * @ifp: interface pointer for which event was received.
1861  * @e: even message.
1862  * @data: payload of event message (probe request).
1863  */
1864 s32 brcmf_p2p_notify_rx_mgmt_p2p_probereq(struct brcmf_if *ifp,
1865 					  const struct brcmf_event_msg *e,
1866 					  void *data)
1867 {
1868 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1869 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1870 	struct afx_hdl *afx_hdl = &p2p->afx_hdl;
1871 	struct brcmf_cfg80211_vif *vif = ifp->vif;
1872 	struct brcmf_rx_mgmt_data *rxframe = (struct brcmf_rx_mgmt_data *)data;
1873 	struct brcmu_chan ch;
1874 	u8 *mgmt_frame;
1875 	u32 mgmt_frame_len;
1876 	s32 freq;
1877 	u16 mgmt_type;
1878 
1879 	brcmf_dbg(INFO, "Enter: event %d reason %d\n", e->event_code,
1880 		  e->reason);
1881 
1882 	if (e->datalen < sizeof(*rxframe)) {
1883 		brcmf_dbg(SCAN, "Event data to small. Ignore\n");
1884 		return 0;
1885 	}
1886 
1887 	ch.chspec = be16_to_cpu(rxframe->chanspec);
1888 	cfg->d11inf.decchspec(&ch);
1889 
1890 	if (test_bit(BRCMF_P2P_STATUS_FINDING_COMMON_CHANNEL, &p2p->status) &&
1891 	    (ether_addr_equal(afx_hdl->tx_dst_addr, e->addr))) {
1892 		afx_hdl->peer_chan = ch.control_ch_num;
1893 		brcmf_dbg(INFO, "PROBE REQUEST: Peer found, channel=%d\n",
1894 			  afx_hdl->peer_chan);
1895 		complete(&afx_hdl->act_frm_scan);
1896 	}
1897 
1898 	/* Firmware sends us two proberesponses for each idx one. At the */
1899 	/* moment anything but bsscfgidx 0 is passed up to supplicant    */
1900 	if (e->bsscfgidx == 0)
1901 		return 0;
1902 
1903 	/* Filter any P2P probe reqs arriving during the GO-NEG Phase */
1904 	if (test_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status)) {
1905 		brcmf_dbg(INFO, "Filtering P2P probe_req in GO-NEG phase\n");
1906 		return 0;
1907 	}
1908 
1909 	/* Check if wpa_supplicant has registered for this frame */
1910 	brcmf_dbg(INFO, "vif->mgmt_rx_reg %04x\n", vif->mgmt_rx_reg);
1911 	mgmt_type = (IEEE80211_STYPE_PROBE_REQ & IEEE80211_FCTL_STYPE) >> 4;
1912 	if ((vif->mgmt_rx_reg & BIT(mgmt_type)) == 0)
1913 		return 0;
1914 
1915 	mgmt_frame = (u8 *)(rxframe + 1);
1916 	mgmt_frame_len = e->datalen - sizeof(*rxframe);
1917 	freq = ieee80211_channel_to_frequency(ch.control_ch_num,
1918 					      ch.band == BRCMU_CHAN_BAND_2G ?
1919 					      NL80211_BAND_2GHZ :
1920 					      NL80211_BAND_5GHZ);
1921 
1922 	cfg80211_rx_mgmt(&vif->wdev, freq, 0, mgmt_frame, mgmt_frame_len, 0);
1923 
1924 	brcmf_dbg(INFO, "mgmt_frame_len (%d) , e->datalen (%d), chanspec (%04x), freq (%d)\n",
1925 		  mgmt_frame_len, e->datalen, ch.chspec, freq);
1926 
1927 	return 0;
1928 }
1929 
1930 
1931 /**
1932  * brcmf_p2p_get_current_chanspec() - Get current operation channel.
1933  *
1934  * @p2p: P2P specific data.
1935  * @chanspec: chanspec to be returned.
1936  */
1937 static void brcmf_p2p_get_current_chanspec(struct brcmf_p2p_info *p2p,
1938 					   u16 *chanspec)
1939 {
1940 	struct brcmf_if *ifp;
1941 	u8 mac_addr[ETH_ALEN];
1942 	struct brcmu_chan ch;
1943 	struct brcmf_bss_info_le *bi;
1944 	u8 *buf;
1945 
1946 	ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
1947 
1948 	if (brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSSID, mac_addr,
1949 				   ETH_ALEN) == 0) {
1950 		buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
1951 		if (buf != NULL) {
1952 			*(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
1953 			if (brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
1954 						   buf, WL_BSS_INFO_MAX) == 0) {
1955 				bi = (struct brcmf_bss_info_le *)(buf + 4);
1956 				*chanspec = le16_to_cpu(bi->chanspec);
1957 				kfree(buf);
1958 				return;
1959 			}
1960 			kfree(buf);
1961 		}
1962 	}
1963 	/* Use default channel for P2P */
1964 	ch.chnum = BRCMF_P2P_TEMP_CHAN;
1965 	ch.bw = BRCMU_CHAN_BW_20;
1966 	p2p->cfg->d11inf.encchspec(&ch);
1967 	*chanspec = ch.chspec;
1968 }
1969 
1970 /**
1971  * Change a P2P Role.
1972  * Parameters:
1973  * @mac: MAC address of the BSS to change a role
1974  * Returns 0 if success.
1975  */
1976 int brcmf_p2p_ifchange(struct brcmf_cfg80211_info *cfg,
1977 		       enum brcmf_fil_p2p_if_types if_type)
1978 {
1979 	struct brcmf_p2p_info *p2p = &cfg->p2p;
1980 	struct brcmf_pub *drvr = cfg->pub;
1981 	struct brcmf_cfg80211_vif *vif;
1982 	struct brcmf_fil_p2p_if_le if_request;
1983 	s32 err;
1984 	u16 chanspec;
1985 
1986 	brcmf_dbg(TRACE, "Enter\n");
1987 
1988 	vif = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1989 	if (!vif) {
1990 		bphy_err(drvr, "vif for P2PAPI_BSSCFG_PRIMARY does not exist\n");
1991 		return -EPERM;
1992 	}
1993 	brcmf_notify_escan_complete(cfg, vif->ifp, true, true);
1994 	vif = p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif;
1995 	if (!vif) {
1996 		bphy_err(drvr, "vif for P2PAPI_BSSCFG_CONNECTION does not exist\n");
1997 		return -EPERM;
1998 	}
1999 	brcmf_set_mpc(vif->ifp, 0);
2000 
2001 	/* In concurrency case, STA may be already associated in a particular */
2002 	/* channel. so retrieve the current channel of primary interface and  */
2003 	/* then start the virtual interface on that.                          */
2004 	brcmf_p2p_get_current_chanspec(p2p, &chanspec);
2005 
2006 	if_request.type = cpu_to_le16((u16)if_type);
2007 	if_request.chspec = cpu_to_le16(chanspec);
2008 	memcpy(if_request.addr, p2p->int_addr, sizeof(if_request.addr));
2009 
2010 	brcmf_cfg80211_arm_vif_event(cfg, vif);
2011 	err = brcmf_fil_iovar_data_set(vif->ifp, "p2p_ifupd", &if_request,
2012 				       sizeof(if_request));
2013 	if (err) {
2014 		bphy_err(drvr, "p2p_ifupd FAILED, err=%d\n", err);
2015 		brcmf_cfg80211_arm_vif_event(cfg, NULL);
2016 		return err;
2017 	}
2018 	err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_CHANGE,
2019 					    BRCMF_VIF_EVENT_TIMEOUT);
2020 	brcmf_cfg80211_arm_vif_event(cfg, NULL);
2021 	if (!err)  {
2022 		bphy_err(drvr, "No BRCMF_E_IF_CHANGE event received\n");
2023 		return -EIO;
2024 	}
2025 
2026 	err = brcmf_fil_cmd_int_set(vif->ifp, BRCMF_C_SET_SCB_TIMEOUT,
2027 				    BRCMF_SCB_TIMEOUT_VALUE);
2028 
2029 	return err;
2030 }
2031 
2032 static int brcmf_p2p_request_p2p_if(struct brcmf_p2p_info *p2p,
2033 				    struct brcmf_if *ifp, u8 ea[ETH_ALEN],
2034 				    enum brcmf_fil_p2p_if_types iftype)
2035 {
2036 	struct brcmf_fil_p2p_if_le if_request;
2037 	int err;
2038 	u16 chanspec;
2039 
2040 	/* we need a default channel */
2041 	brcmf_p2p_get_current_chanspec(p2p, &chanspec);
2042 
2043 	/* fill the firmware request */
2044 	memcpy(if_request.addr, ea, ETH_ALEN);
2045 	if_request.type = cpu_to_le16((u16)iftype);
2046 	if_request.chspec = cpu_to_le16(chanspec);
2047 
2048 	err = brcmf_fil_iovar_data_set(ifp, "p2p_ifadd", &if_request,
2049 				       sizeof(if_request));
2050 
2051 	return err;
2052 }
2053 
2054 static int brcmf_p2p_disable_p2p_if(struct brcmf_cfg80211_vif *vif)
2055 {
2056 	struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2057 	struct net_device *pri_ndev = cfg_to_ndev(cfg);
2058 	struct brcmf_if *ifp = netdev_priv(pri_ndev);
2059 	u8 *addr = vif->wdev.netdev->dev_addr;
2060 
2061 	return brcmf_fil_iovar_data_set(ifp, "p2p_ifdis", addr, ETH_ALEN);
2062 }
2063 
2064 static int brcmf_p2p_release_p2p_if(struct brcmf_cfg80211_vif *vif)
2065 {
2066 	struct brcmf_cfg80211_info *cfg = wdev_to_cfg(&vif->wdev);
2067 	struct net_device *pri_ndev = cfg_to_ndev(cfg);
2068 	struct brcmf_if *ifp = netdev_priv(pri_ndev);
2069 	u8 *addr = vif->wdev.netdev->dev_addr;
2070 
2071 	return brcmf_fil_iovar_data_set(ifp, "p2p_ifdel", addr, ETH_ALEN);
2072 }
2073 
2074 /**
2075  * brcmf_p2p_create_p2pdev() - create a P2P_DEVICE virtual interface.
2076  *
2077  * @p2p: P2P specific data.
2078  * @wiphy: wiphy device of new interface.
2079  * @addr: mac address for this new interface.
2080  */
2081 static struct wireless_dev *brcmf_p2p_create_p2pdev(struct brcmf_p2p_info *p2p,
2082 						    struct wiphy *wiphy,
2083 						    u8 *addr)
2084 {
2085 	struct brcmf_pub *drvr = p2p->cfg->pub;
2086 	struct brcmf_cfg80211_vif *p2p_vif;
2087 	struct brcmf_if *p2p_ifp;
2088 	struct brcmf_if *pri_ifp;
2089 	int err;
2090 	u32 bsscfgidx;
2091 
2092 	if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2093 		return ERR_PTR(-ENOSPC);
2094 
2095 	p2p_vif = brcmf_alloc_vif(p2p->cfg, NL80211_IFTYPE_P2P_DEVICE);
2096 	if (IS_ERR(p2p_vif)) {
2097 		bphy_err(drvr, "could not create discovery vif\n");
2098 		return (struct wireless_dev *)p2p_vif;
2099 	}
2100 
2101 	pri_ifp = p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
2102 
2103 	/* firmware requires unique mac address for p2pdev interface */
2104 	if (addr && ether_addr_equal(addr, pri_ifp->mac_addr)) {
2105 		bphy_err(drvr, "discovery vif must be different from primary interface\n");
2106 		return ERR_PTR(-EINVAL);
2107 	}
2108 
2109 	brcmf_p2p_generate_bss_mac(p2p, addr);
2110 	brcmf_p2p_set_firmware(pri_ifp, p2p->dev_addr);
2111 
2112 	brcmf_cfg80211_arm_vif_event(p2p->cfg, p2p_vif);
2113 	brcmf_fweh_p2pdev_setup(pri_ifp, true);
2114 
2115 	/* Initialize P2P Discovery in the firmware */
2116 	err = brcmf_fil_iovar_int_set(pri_ifp, "p2p_disc", 1);
2117 	if (err < 0) {
2118 		bphy_err(drvr, "set p2p_disc error\n");
2119 		brcmf_fweh_p2pdev_setup(pri_ifp, false);
2120 		brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2121 		goto fail;
2122 	}
2123 
2124 	/* wait for firmware event */
2125 	err = brcmf_cfg80211_wait_vif_event(p2p->cfg, BRCMF_E_IF_ADD,
2126 					    BRCMF_VIF_EVENT_TIMEOUT);
2127 	brcmf_cfg80211_arm_vif_event(p2p->cfg, NULL);
2128 	brcmf_fweh_p2pdev_setup(pri_ifp, false);
2129 	if (!err) {
2130 		bphy_err(drvr, "timeout occurred\n");
2131 		err = -EIO;
2132 		goto fail;
2133 	}
2134 
2135 	/* discovery interface created */
2136 	p2p_ifp = p2p_vif->ifp;
2137 	p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif = p2p_vif;
2138 	memcpy(p2p_ifp->mac_addr, p2p->dev_addr, ETH_ALEN);
2139 	memcpy(&p2p_vif->wdev.address, p2p->dev_addr, sizeof(p2p->dev_addr));
2140 
2141 	/* verify bsscfg index for P2P discovery */
2142 	err = brcmf_fil_iovar_int_get(pri_ifp, "p2p_dev", &bsscfgidx);
2143 	if (err < 0) {
2144 		bphy_err(drvr, "retrieving discover bsscfg index failed\n");
2145 		goto fail;
2146 	}
2147 
2148 	WARN_ON(p2p_ifp->bsscfgidx != bsscfgidx);
2149 
2150 	init_completion(&p2p->send_af_done);
2151 	INIT_WORK(&p2p->afx_hdl.afx_work, brcmf_p2p_afx_handler);
2152 	init_completion(&p2p->afx_hdl.act_frm_scan);
2153 	init_completion(&p2p->wait_next_af);
2154 
2155 	return &p2p_vif->wdev;
2156 
2157 fail:
2158 	brcmf_free_vif(p2p_vif);
2159 	return ERR_PTR(err);
2160 }
2161 
2162 /**
2163  * brcmf_p2p_add_vif() - create a new P2P virtual interface.
2164  *
2165  * @wiphy: wiphy device of new interface.
2166  * @name: name of the new interface.
2167  * @name_assign_type: origin of the interface name
2168  * @type: nl80211 interface type.
2169  * @params: contains mac address for P2P device.
2170  */
2171 struct wireless_dev *brcmf_p2p_add_vif(struct wiphy *wiphy, const char *name,
2172 				       unsigned char name_assign_type,
2173 				       enum nl80211_iftype type,
2174 				       struct vif_params *params)
2175 {
2176 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2177 	struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
2178 	struct brcmf_pub *drvr = cfg->pub;
2179 	struct brcmf_cfg80211_vif *vif;
2180 	enum brcmf_fil_p2p_if_types iftype;
2181 	int err;
2182 
2183 	if (brcmf_cfg80211_vif_event_armed(cfg))
2184 		return ERR_PTR(-EBUSY);
2185 
2186 	brcmf_dbg(INFO, "adding vif \"%s\" (type=%d)\n", name, type);
2187 
2188 	switch (type) {
2189 	case NL80211_IFTYPE_P2P_CLIENT:
2190 		iftype = BRCMF_FIL_P2P_IF_CLIENT;
2191 		break;
2192 	case NL80211_IFTYPE_P2P_GO:
2193 		iftype = BRCMF_FIL_P2P_IF_GO;
2194 		break;
2195 	case NL80211_IFTYPE_P2P_DEVICE:
2196 		return brcmf_p2p_create_p2pdev(&cfg->p2p, wiphy,
2197 					       params->macaddr);
2198 	default:
2199 		return ERR_PTR(-EOPNOTSUPP);
2200 	}
2201 
2202 	vif = brcmf_alloc_vif(cfg, type);
2203 	if (IS_ERR(vif))
2204 		return (struct wireless_dev *)vif;
2205 	brcmf_cfg80211_arm_vif_event(cfg, vif);
2206 
2207 	err = brcmf_p2p_request_p2p_if(&cfg->p2p, ifp, cfg->p2p.int_addr,
2208 				       iftype);
2209 	if (err) {
2210 		brcmf_cfg80211_arm_vif_event(cfg, NULL);
2211 		goto fail;
2212 	}
2213 
2214 	/* wait for firmware event */
2215 	err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_ADD,
2216 					    BRCMF_VIF_EVENT_TIMEOUT);
2217 	brcmf_cfg80211_arm_vif_event(cfg, NULL);
2218 	if (!err) {
2219 		bphy_err(drvr, "timeout occurred\n");
2220 		err = -EIO;
2221 		goto fail;
2222 	}
2223 
2224 	/* interface created in firmware */
2225 	ifp = vif->ifp;
2226 	if (!ifp) {
2227 		bphy_err(drvr, "no if pointer provided\n");
2228 		err = -ENOENT;
2229 		goto fail;
2230 	}
2231 
2232 	strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
2233 	ifp->ndev->name_assign_type = name_assign_type;
2234 	err = brcmf_net_attach(ifp, true);
2235 	if (err) {
2236 		bphy_err(drvr, "Registering netdevice failed\n");
2237 		free_netdev(ifp->ndev);
2238 		goto fail;
2239 	}
2240 
2241 	cfg->p2p.bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = vif;
2242 	/* Disable firmware roaming for P2P interface  */
2243 	brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
2244 	if (iftype == BRCMF_FIL_P2P_IF_GO) {
2245 		/* set station timeout for p2p */
2246 		brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCB_TIMEOUT,
2247 				      BRCMF_SCB_TIMEOUT_VALUE);
2248 	}
2249 	return &ifp->vif->wdev;
2250 
2251 fail:
2252 	brcmf_free_vif(vif);
2253 	return ERR_PTR(err);
2254 }
2255 
2256 /**
2257  * brcmf_p2p_del_vif() - delete a P2P virtual interface.
2258  *
2259  * @wiphy: wiphy device of interface.
2260  * @wdev: wireless device of interface.
2261  */
2262 int brcmf_p2p_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
2263 {
2264 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2265 	struct brcmf_p2p_info *p2p = &cfg->p2p;
2266 	struct brcmf_cfg80211_vif *vif;
2267 	enum nl80211_iftype iftype;
2268 	bool wait_for_disable = false;
2269 	int err;
2270 
2271 	brcmf_dbg(TRACE, "delete P2P vif\n");
2272 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2273 
2274 	iftype = vif->wdev.iftype;
2275 	brcmf_cfg80211_arm_vif_event(cfg, vif);
2276 	switch (iftype) {
2277 	case NL80211_IFTYPE_P2P_CLIENT:
2278 		if (test_bit(BRCMF_VIF_STATUS_DISCONNECTING, &vif->sme_state))
2279 			wait_for_disable = true;
2280 		break;
2281 
2282 	case NL80211_IFTYPE_P2P_GO:
2283 		if (!brcmf_p2p_disable_p2p_if(vif))
2284 			wait_for_disable = true;
2285 		break;
2286 
2287 	case NL80211_IFTYPE_P2P_DEVICE:
2288 		if (!p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
2289 			return 0;
2290 		brcmf_p2p_cancel_remain_on_channel(vif->ifp);
2291 		brcmf_p2p_deinit_discovery(p2p);
2292 		break;
2293 
2294 	default:
2295 		return -ENOTSUPP;
2296 	}
2297 
2298 	clear_bit(BRCMF_P2P_STATUS_GO_NEG_PHASE, &p2p->status);
2299 	brcmf_dbg(INFO, "P2P: GO_NEG_PHASE status cleared\n");
2300 
2301 	if (wait_for_disable)
2302 		wait_for_completion_timeout(&cfg->vif_disabled,
2303 					    BRCMF_P2P_DISABLE_TIMEOUT);
2304 
2305 	err = 0;
2306 	if (iftype != NL80211_IFTYPE_P2P_DEVICE) {
2307 		brcmf_vif_clear_mgmt_ies(vif);
2308 		err = brcmf_p2p_release_p2p_if(vif);
2309 	}
2310 	if (!err) {
2311 		/* wait for firmware event */
2312 		err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_DEL,
2313 						    BRCMF_VIF_EVENT_TIMEOUT);
2314 		if (!err)
2315 			err = -EIO;
2316 		else
2317 			err = 0;
2318 	}
2319 	brcmf_remove_interface(vif->ifp, true);
2320 
2321 	brcmf_cfg80211_arm_vif_event(cfg, NULL);
2322 	if (iftype != NL80211_IFTYPE_P2P_DEVICE)
2323 		p2p->bss_idx[P2PAPI_BSSCFG_CONNECTION].vif = NULL;
2324 
2325 	return err;
2326 }
2327 
2328 void brcmf_p2p_ifp_removed(struct brcmf_if *ifp, bool rtnl_locked)
2329 {
2330 	struct brcmf_cfg80211_info *cfg;
2331 	struct brcmf_cfg80211_vif *vif;
2332 
2333 	brcmf_dbg(INFO, "P2P: device interface removed\n");
2334 	vif = ifp->vif;
2335 	cfg = wdev_to_cfg(&vif->wdev);
2336 	cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif = NULL;
2337 	if (!rtnl_locked)
2338 		rtnl_lock();
2339 	cfg80211_unregister_wdev(&vif->wdev);
2340 	if (!rtnl_locked)
2341 		rtnl_unlock();
2342 	brcmf_free_vif(vif);
2343 }
2344 
2345 int brcmf_p2p_start_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2346 {
2347 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2348 	struct brcmf_p2p_info *p2p = &cfg->p2p;
2349 	struct brcmf_cfg80211_vif *vif;
2350 	int err;
2351 
2352 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2353 	mutex_lock(&cfg->usr_sync);
2354 	err = brcmf_p2p_enable_discovery(p2p);
2355 	if (!err)
2356 		set_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2357 	mutex_unlock(&cfg->usr_sync);
2358 	return err;
2359 }
2360 
2361 void brcmf_p2p_stop_device(struct wiphy *wiphy, struct wireless_dev *wdev)
2362 {
2363 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2364 	struct brcmf_p2p_info *p2p = &cfg->p2p;
2365 	struct brcmf_cfg80211_vif *vif;
2366 
2367 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
2368 	/* This call can be result of the unregister_wdev call. In that case
2369 	 * we dont want to do anything anymore. Just return. The config vif
2370 	 * will have been cleared at this point.
2371 	 */
2372 	if (p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif == vif) {
2373 		mutex_lock(&cfg->usr_sync);
2374 		/* Set the discovery state to SCAN */
2375 		(void)brcmf_p2p_set_discover_state(vif->ifp,
2376 						   WL_P2P_DISC_ST_SCAN, 0, 0);
2377 		brcmf_abort_scanning(cfg);
2378 		clear_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state);
2379 		mutex_unlock(&cfg->usr_sync);
2380 	}
2381 }
2382 
2383 /**
2384  * brcmf_p2p_attach() - attach for P2P.
2385  *
2386  * @cfg: driver private data for cfg80211 interface.
2387  * @p2pdev_forced: create p2p device interface at attach.
2388  */
2389 s32 brcmf_p2p_attach(struct brcmf_cfg80211_info *cfg, bool p2pdev_forced)
2390 {
2391 	struct brcmf_pub *drvr = cfg->pub;
2392 	struct brcmf_p2p_info *p2p;
2393 	struct brcmf_if *pri_ifp;
2394 	s32 err = 0;
2395 	void *err_ptr;
2396 
2397 	p2p = &cfg->p2p;
2398 	p2p->cfg = cfg;
2399 
2400 	pri_ifp = brcmf_get_ifp(cfg->pub, 0);
2401 	p2p->bss_idx[P2PAPI_BSSCFG_PRIMARY].vif = pri_ifp->vif;
2402 
2403 	if (p2pdev_forced) {
2404 		err_ptr = brcmf_p2p_create_p2pdev(p2p, NULL, NULL);
2405 		if (IS_ERR(err_ptr)) {
2406 			bphy_err(drvr, "P2P device creation failed.\n");
2407 			err = PTR_ERR(err_ptr);
2408 		}
2409 	} else {
2410 		p2p->p2pdev_dynamically = true;
2411 	}
2412 	return err;
2413 }
2414 
2415 /**
2416  * brcmf_p2p_detach() - detach P2P.
2417  *
2418  * @p2p: P2P specific data.
2419  */
2420 void brcmf_p2p_detach(struct brcmf_p2p_info *p2p)
2421 {
2422 	struct brcmf_cfg80211_vif *vif;
2423 
2424 	vif = p2p->bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
2425 	if (vif != NULL) {
2426 		brcmf_p2p_cancel_remain_on_channel(vif->ifp);
2427 		brcmf_p2p_deinit_discovery(p2p);
2428 		brcmf_remove_interface(vif->ifp, false);
2429 	}
2430 	/* just set it all to zero */
2431 	memset(p2p, 0, sizeof(*p2p));
2432 }
2433 
2434