xref: /wlan-dirver/qca-wifi-host-cmn/umac/scan/core/src/wlan_scan_11d.c (revision a175314c51a4ce5cec2835cc8a8c7dc0c1810915)
1 /*
2  * Copyright (c) 2011-2018 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * DOC: contains scan 11d api and functionality
21  */
22 #include <qdf_status.h>
23 #include <wlan_objmgr_psoc_obj.h>
24 #include <wlan_objmgr_pdev_obj.h>
25 #include <wlan_objmgr_vdev_obj.h>
26 #include <wlan_scan_public_structs.h>
27 #include <wlan_scan_utils_api.h>
28 #include "wlan_scan_main.h"
29 #include "wlan_scan_11d.h"
30 #include "wlan_reg_services_api.h"
31 #include "wlan_reg_ucfg_api.h"
32 
33 /**
34  * wlan_pdevid_get_cc_db() - private API to get cc db from pdev id
35  * @psoc: psoc object
36  * @pdev_id: pdev id
37  *
38  * Return: cc db for the pdev id
39  */
40 static struct scan_country_code_db *
41 wlan_pdevid_get_cc_db(struct wlan_objmgr_psoc *psoc, uint8_t pdev_id)
42 {
43 	struct wlan_scan_obj *scan_obj;
44 
45 	if (pdev_id > WLAN_UMAC_MAX_PDEVS) {
46 		scm_err("invalid pdev_id %d", pdev_id);
47 		return NULL;
48 	}
49 
50 	scan_obj = wlan_psoc_get_scan_obj(psoc);
51 	if (!scan_obj)
52 		return NULL;
53 
54 	return &scan_obj->cc_db[pdev_id];
55 }
56 
57 /**
58  * wlan_pdev_get_cc_db() - private API to get cc db from pdev
59  * @psoc: psoc object
60  * @pdev: Pdev object
61  *
62  * Return: cc db for the pdev
63  */
64 static struct scan_country_code_db *
65 wlan_pdev_get_cc_db(struct wlan_objmgr_psoc *psoc,
66 		    struct wlan_objmgr_pdev *pdev)
67 {
68 	uint8_t pdev_id;
69 
70 	if (!pdev) {
71 		scm_err("pdev is NULL");
72 		return NULL;
73 	}
74 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
75 
76 	return wlan_pdevid_get_cc_db(psoc, pdev_id);
77 }
78 
79 /**
80  * scm_11d_elected_country_algo_fcc - private api to get cc per fcc algo
81  * @cc_db: scan country code db
82  *
83  * Return: true or false
84  */
85 static bool
86 scm_11d_elected_country_algo_fcc(struct scan_country_code_db *cc_db)
87 {
88 	uint8_t i;
89 	uint8_t country_idx;
90 	uint16_t max_votes;
91 	bool found = false;
92 
93 	if (!cc_db->num_country_codes) {
94 		scm_err("No AP with 11d Country code is present in scan list");
95 		return false;
96 	}
97 
98 	max_votes = cc_db->votes[0].votes;
99 	if (wlan_reg_is_us(cc_db->votes[0].cc)) {
100 		found = true;
101 		country_idx = 0;
102 		goto algo_done;
103 	} else if (max_votes >= MIN_11D_AP_COUNT) {
104 		found = true;
105 		country_idx = 0;
106 	}
107 
108 	for (i = 1; i < cc_db->num_country_codes; i++) {
109 		if (wlan_reg_is_us(cc_db->votes[i].cc)) {
110 			found = true;
111 			country_idx = i;
112 			goto algo_done;
113 		}
114 
115 		if ((max_votes < cc_db->votes[i].votes) &&
116 		    (cc_db->votes[i].votes >= MIN_11D_AP_COUNT)) {
117 			scm_debug("Votes for Country %c%c : %d",
118 				  cc_db->votes[i].cc[0],
119 				  cc_db->votes[i].cc[1],
120 				  cc_db->votes[i].votes);
121 			max_votes = cc_db->votes[i].votes;
122 			country_idx = i;
123 			found = true;
124 		}
125 	}
126 
127 algo_done:
128 	if (found) {
129 		qdf_mem_copy(cc_db->elected_cc,
130 			     cc_db->votes[country_idx].cc,
131 			     REG_ALPHA2_LEN + 1);
132 
133 		scm_debug("Selected Country is %c%c With count %d",
134 			  cc_db->votes[country_idx].cc[0],
135 			  cc_db->votes[country_idx].cc[1],
136 			  cc_db->votes[country_idx].votes);
137 	}
138 
139 	return found;
140 }
141 
142 /**
143  * scm_11d_elected_country_info - private api to get cc
144  * @cc_db: scan country code db
145  *
146  * Return: true or false
147  */
148 static bool
149 scm_11d_elected_country_info(struct scan_country_code_db *cc_db)
150 {
151 	uint8_t i, j = 0;
152 	uint8_t max_votes;
153 
154 	if (!cc_db->num_country_codes) {
155 		scm_err("No AP with 11d Country code is present in scan list");
156 		return false;
157 	}
158 
159 	max_votes = cc_db->votes[0].votes;
160 
161 	for (i = 1; i < cc_db->num_country_codes; i++) {
162 		/*
163 		 * If we have a tie for max votes for 2 different country codes,
164 		 * pick random.
165 		 */
166 		if (max_votes < cc_db->votes[i].votes) {
167 			scm_debug("Votes for Country %c%c : %d",
168 				  cc_db->votes[i].cc[0],
169 				  cc_db->votes[i].cc[1],
170 				  cc_db->votes[i].votes);
171 
172 			max_votes = cc_db->votes[i].votes;
173 			j = i;
174 		}
175 	}
176 
177 	qdf_mem_copy(cc_db->elected_cc, cc_db->votes[j].cc,
178 		     REG_ALPHA2_LEN + 1);
179 
180 	scm_debug("Selected Country is %c%c With count %d",
181 		  cc_db->votes[j].cc[0],
182 		  cc_db->votes[j].cc[1],
183 		  cc_db->votes[j].votes);
184 
185 	return true;
186 }
187 
188 /**
189  * scm_11d_set_country_code - private api to set cc per 11d learning
190  * @pdev: pdev object
191  * @elected_cc: elected country code
192  * @current_cc: current country code
193  *
194  * Return: true or false
195  */
196 static bool
197 scm_11d_set_country_code(struct wlan_objmgr_pdev *pdev,
198 			 uint8_t *elected_cc, uint8_t *current_cc)
199 {
200 	scm_debug("elected country %c%c, current country %c%c",
201 		  elected_cc[0], elected_cc[1], current_cc[0], current_cc[1]);
202 
203 	if (!qdf_mem_cmp(elected_cc, current_cc, REG_ALPHA2_LEN + 1))
204 		return true;
205 
206 	wlan_reg_set_11d_country(pdev, elected_cc);
207 	return true;
208 }
209 
210 /**
211  * scm_11d_reset_cc_db - reset the country code db
212  * @cc_db: the pointer of country code db
213  *
214  * Return: void
215  */
216 static void scm_11d_reset_cc_db(struct scan_country_code_db *cc_db)
217 {
218 	qdf_mem_zero(cc_db->votes, sizeof(cc_db->votes));
219 	qdf_mem_zero(cc_db->elected_cc, sizeof(cc_db->elected_cc));
220 	cc_db->num_country_codes = 0;
221 }
222 
223 QDF_STATUS scm_11d_cc_db_init(struct wlan_objmgr_psoc *psoc)
224 {
225 	struct scan_country_code_db *cc_db;
226 	struct wlan_scan_obj *scan_obj;
227 
228 	if (!psoc) {
229 		scm_err("psoc is NULL");
230 		return QDF_STATUS_E_INVAL;
231 	}
232 
233 	scan_obj = wlan_psoc_get_scan_obj(psoc);
234 	if (!scan_obj) {
235 		scm_err("scan_obj is NULL");
236 		return QDF_STATUS_E_INVAL;
237 	}
238 
239 	cc_db = (struct scan_country_code_db *)qdf_mem_malloc_atomic(
240 		   sizeof(struct scan_country_code_db) * WLAN_UMAC_MAX_PDEVS);
241 	if (!cc_db) {
242 		scm_err("alloc country code db error");
243 		return QDF_STATUS_E_INVAL;
244 	}
245 
246 	qdf_mem_zero(cc_db,
247 		     sizeof(struct scan_country_code_db) *
248 			    WLAN_UMAC_MAX_PDEVS);
249 
250 	scan_obj->cc_db = cc_db;
251 	return QDF_STATUS_SUCCESS;
252 }
253 
254 QDF_STATUS scm_11d_cc_db_deinit(struct wlan_objmgr_psoc *psoc)
255 {
256 	struct wlan_scan_obj *scan_obj;
257 
258 	if (!psoc) {
259 		scm_err("psoc is NULL");
260 		return QDF_STATUS_E_INVAL;
261 	}
262 
263 	scan_obj = wlan_psoc_get_scan_obj(psoc);
264 	if (!scan_obj) {
265 		scm_err("scan_obj is NULL");
266 		return QDF_STATUS_E_INVAL;
267 	}
268 
269 	qdf_mem_free(scan_obj->cc_db);
270 	return QDF_STATUS_SUCCESS;
271 }
272 
273 void scm_11d_handle_country_info(struct wlan_objmgr_psoc *psoc,
274 				 struct wlan_objmgr_pdev *pdev,
275 				 struct scan_cache_entry *scan_entry)
276 {
277 	uint8_t i;
278 	bool match = false;
279 	uint8_t num_country_codes;
280 	struct scan_country_code_db *cc_db;
281 	struct wlan_country_ie *cc_ie;
282 
283 	cc_ie = util_scan_entry_country(scan_entry);
284 	if (!cc_ie)
285 		return;
286 
287 	cc_db = wlan_pdev_get_cc_db(psoc, pdev);
288 	if (!cc_db)
289 		return;
290 
291 	/* just to be sure, convert to UPPER case here */
292 	for (i = 0; i < 3; i++)
293 		cc_ie->cc[i] = qdf_toupper(cc_ie->cc[i]);
294 
295 	num_country_codes = cc_db->num_country_codes;
296 	for (i = 0; i < num_country_codes; i++) {
297 		match = !qdf_mem_cmp(cc_db->votes[i].cc, cc_ie->cc,
298 				     REG_ALPHA2_LEN);
299 		if (match)
300 			break;
301 	}
302 
303 	if (match) {
304 		cc_db->votes[i].votes++;
305 		return;
306 	}
307 
308 	if (num_country_codes >= SCAN_MAX_NUM_COUNTRY_CODE) {
309 		scm_debug("country code db already full: %d",
310 			  num_country_codes);
311 		return;
312 	}
313 
314 	/* add country code to end of the list */
315 	qdf_mem_copy(cc_db->votes[num_country_codes].cc, cc_ie->cc,
316 		     REG_ALPHA2_LEN + 1);
317 	cc_db->votes[num_country_codes].votes = 1;
318 	cc_db->num_country_codes++;
319 }
320 
321 void scm_11d_decide_country_code(struct wlan_objmgr_vdev *vdev)
322 {
323 	uint8_t current_cc[REG_ALPHA2_LEN + 1];
324 	bool found;
325 	struct scan_country_code_db *cc_db;
326 	struct wlan_objmgr_pdev *pdev = wlan_vdev_get_pdev(vdev);
327 	struct wlan_objmgr_psoc *psoc = wlan_pdev_get_psoc(pdev);
328 
329 	if (!wlan_reg_11d_enabled_on_host(psoc))
330 		return;
331 
332 	if (SOURCE_UNKNOWN == ucfg_reg_get_cc_and_src(psoc, current_cc)) {
333 		scm_err("fail to get current country code");
334 		return;
335 	}
336 
337 	cc_db = wlan_pdev_get_cc_db(psoc, pdev);
338 	if (!cc_db) {
339 		scm_err("scan_db is NULL");
340 		return;
341 	}
342 
343 	if (wlan_reg_is_us(current_cc) || wlan_reg_is_world(current_cc))
344 		found = scm_11d_elected_country_algo_fcc(cc_db);
345 	else
346 		found = scm_11d_elected_country_info(cc_db);
347 
348 	if (found)
349 		scm_11d_set_country_code(pdev, cc_db->elected_cc,
350 					 current_cc);
351 	scm_11d_reset_cc_db(cc_db);
352 }
353