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