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