1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2010 Broadcom Corporation
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/netdevice.h>
9 #include <linux/module.h>
10 #include <linux/firmware.h>
11 #include <brcmu_wifi.h>
12 #include <brcmu_utils.h>
13 #include "core.h"
14 #include "bus.h"
15 #include "debug.h"
16 #include "fwil.h"
17 #include "fwil_types.h"
18 #include "tracepoint.h"
19 #include "common.h"
20 #include "of.h"
21 #include "firmware.h"
22 #include "chip.h"
23 
24 MODULE_AUTHOR("Broadcom Corporation");
25 MODULE_DESCRIPTION("Broadcom 802.11 wireless LAN fullmac driver.");
26 MODULE_LICENSE("Dual BSD/GPL");
27 
28 #define BRCMF_DEFAULT_SCAN_CHANNEL_TIME	40
29 #define BRCMF_DEFAULT_SCAN_UNASSOC_TIME	40
30 
31 /* default boost value for RSSI_DELTA in preferred join selection */
32 #define BRCMF_JOIN_PREF_RSSI_BOOST	8
33 
34 #define BRCMF_DEFAULT_TXGLOM_SIZE	32  /* max tx frames in glom chain */
35 
36 static int brcmf_sdiod_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
37 module_param_named(txglomsz, brcmf_sdiod_txglomsz, int, 0);
38 MODULE_PARM_DESC(txglomsz, "Maximum tx packet chain size [SDIO]");
39 
40 /* Debug level configuration. See debug.h for bits, sysfs modifiable */
41 int brcmf_msg_level;
42 module_param_named(debug, brcmf_msg_level, int, 0600);
43 MODULE_PARM_DESC(debug, "Level of debug output");
44 
45 static int brcmf_p2p_enable;
46 module_param_named(p2pon, brcmf_p2p_enable, int, 0);
47 MODULE_PARM_DESC(p2pon, "Enable legacy p2p management functionality");
48 
49 static int brcmf_feature_disable;
50 module_param_named(feature_disable, brcmf_feature_disable, int, 0);
51 MODULE_PARM_DESC(feature_disable, "Disable features");
52 
53 static char brcmf_firmware_path[BRCMF_FW_ALTPATH_LEN];
54 module_param_string(alternative_fw_path, brcmf_firmware_path,
55 		    BRCMF_FW_ALTPATH_LEN, 0400);
56 MODULE_PARM_DESC(alternative_fw_path, "Alternative firmware path");
57 
58 static int brcmf_fcmode;
59 module_param_named(fcmode, brcmf_fcmode, int, 0);
60 MODULE_PARM_DESC(fcmode, "Mode of firmware signalled flow control");
61 
62 static int brcmf_roamoff;
63 module_param_named(roamoff, brcmf_roamoff, int, 0400);
64 MODULE_PARM_DESC(roamoff, "Do not use internal roaming engine");
65 
66 static int brcmf_iapp_enable;
67 module_param_named(iapp, brcmf_iapp_enable, int, 0);
68 MODULE_PARM_DESC(iapp, "Enable partial support for the obsoleted Inter-Access Point Protocol");
69 
70 #ifdef DEBUG
71 /* always succeed brcmf_bus_started() */
72 static int brcmf_ignore_probe_fail;
73 module_param_named(ignore_probe_fail, brcmf_ignore_probe_fail, int, 0);
74 MODULE_PARM_DESC(ignore_probe_fail, "always succeed probe for debugging");
75 #endif
76 
77 static struct brcmfmac_platform_data *brcmfmac_pdata;
78 struct brcmf_mp_global_t brcmf_mp_global;
79 
80 void brcmf_c_set_joinpref_default(struct brcmf_if *ifp)
81 {
82 	struct brcmf_pub *drvr = ifp->drvr;
83 	struct brcmf_join_pref_params join_pref_params[2];
84 	int err;
85 
86 	/* Setup join_pref to select target by RSSI (boost on 5GHz) */
87 	join_pref_params[0].type = BRCMF_JOIN_PREF_RSSI_DELTA;
88 	join_pref_params[0].len = 2;
89 	join_pref_params[0].rssi_gain = BRCMF_JOIN_PREF_RSSI_BOOST;
90 	join_pref_params[0].band = WLC_BAND_5G;
91 
92 	join_pref_params[1].type = BRCMF_JOIN_PREF_RSSI;
93 	join_pref_params[1].len = 2;
94 	join_pref_params[1].rssi_gain = 0;
95 	join_pref_params[1].band = 0;
96 	err = brcmf_fil_iovar_data_set(ifp, "join_pref", join_pref_params,
97 				       sizeof(join_pref_params));
98 	if (err)
99 		bphy_err(drvr, "Set join_pref error (%d)\n", err);
100 }
101 
102 static int brcmf_c_download(struct brcmf_if *ifp, u16 flag,
103 			    struct brcmf_dload_data_le *dload_buf,
104 			    u32 len)
105 {
106 	s32 err;
107 
108 	flag |= (DLOAD_HANDLER_VER << DLOAD_FLAG_VER_SHIFT);
109 	dload_buf->flag = cpu_to_le16(flag);
110 	dload_buf->dload_type = cpu_to_le16(DL_TYPE_CLM);
111 	dload_buf->len = cpu_to_le32(len);
112 	dload_buf->crc = cpu_to_le32(0);
113 	len = sizeof(*dload_buf) + len - 1;
114 
115 	err = brcmf_fil_iovar_data_set(ifp, "clmload", dload_buf, len);
116 
117 	return err;
118 }
119 
120 static int brcmf_c_process_clm_blob(struct brcmf_if *ifp)
121 {
122 	struct brcmf_pub *drvr = ifp->drvr;
123 	struct brcmf_bus *bus = drvr->bus_if;
124 	struct brcmf_dload_data_le *chunk_buf;
125 	const struct firmware *clm = NULL;
126 	u8 clm_name[BRCMF_FW_NAME_LEN];
127 	u32 chunk_len;
128 	u32 datalen;
129 	u32 cumulative_len;
130 	u16 dl_flag = DL_BEGIN;
131 	u32 status;
132 	s32 err;
133 
134 	brcmf_dbg(TRACE, "Enter\n");
135 
136 	memset(clm_name, 0, sizeof(clm_name));
137 	err = brcmf_bus_get_fwname(bus, ".clm_blob", clm_name);
138 	if (err) {
139 		bphy_err(drvr, "get CLM blob file name failed (%d)\n", err);
140 		return err;
141 	}
142 
143 	err = firmware_request_nowarn(&clm, clm_name, bus->dev);
144 	if (err) {
145 		brcmf_info("no clm_blob available (err=%d), device may have limited channels available\n",
146 			   err);
147 		return 0;
148 	}
149 
150 	chunk_buf = kzalloc(sizeof(*chunk_buf) + MAX_CHUNK_LEN - 1, GFP_KERNEL);
151 	if (!chunk_buf) {
152 		err = -ENOMEM;
153 		goto done;
154 	}
155 
156 	datalen = clm->size;
157 	cumulative_len = 0;
158 	do {
159 		if (datalen > MAX_CHUNK_LEN) {
160 			chunk_len = MAX_CHUNK_LEN;
161 		} else {
162 			chunk_len = datalen;
163 			dl_flag |= DL_END;
164 		}
165 		memcpy(chunk_buf->data, clm->data + cumulative_len, chunk_len);
166 
167 		err = brcmf_c_download(ifp, dl_flag, chunk_buf, chunk_len);
168 
169 		dl_flag &= ~DL_BEGIN;
170 
171 		cumulative_len += chunk_len;
172 		datalen -= chunk_len;
173 	} while ((datalen > 0) && (err == 0));
174 
175 	if (err) {
176 		bphy_err(drvr, "clmload (%zu byte file) failed (%d)\n",
177 			 clm->size, err);
178 		/* Retrieve clmload_status and print */
179 		err = brcmf_fil_iovar_int_get(ifp, "clmload_status", &status);
180 		if (err)
181 			bphy_err(drvr, "get clmload_status failed (%d)\n", err);
182 		else
183 			brcmf_dbg(INFO, "clmload_status=%d\n", status);
184 		err = -EIO;
185 	}
186 
187 	kfree(chunk_buf);
188 done:
189 	release_firmware(clm);
190 	return err;
191 }
192 
193 int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
194 {
195 	struct brcmf_pub *drvr = ifp->drvr;
196 	s8 eventmask[BRCMF_EVENTING_MASK_LEN];
197 	u8 buf[BRCMF_DCMD_SMLEN];
198 	struct brcmf_bus *bus;
199 	struct brcmf_rev_info_le revinfo;
200 	struct brcmf_rev_info *ri;
201 	char *clmver;
202 	char *ptr;
203 	s32 err;
204 
205 	/* retreive mac address */
206 	err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
207 				       sizeof(ifp->mac_addr));
208 	if (err < 0) {
209 		bphy_err(drvr, "Retrieving cur_etheraddr failed, %d\n", err);
210 		goto done;
211 	}
212 	memcpy(ifp->drvr->wiphy->perm_addr, ifp->drvr->mac, ETH_ALEN);
213 	memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
214 
215 	bus = ifp->drvr->bus_if;
216 	ri = &ifp->drvr->revinfo;
217 
218 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_REVINFO,
219 				     &revinfo, sizeof(revinfo));
220 	if (err < 0) {
221 		bphy_err(drvr, "retrieving revision info failed, %d\n", err);
222 		strlcpy(ri->chipname, "UNKNOWN", sizeof(ri->chipname));
223 	} else {
224 		ri->vendorid = le32_to_cpu(revinfo.vendorid);
225 		ri->deviceid = le32_to_cpu(revinfo.deviceid);
226 		ri->radiorev = le32_to_cpu(revinfo.radiorev);
227 		ri->corerev = le32_to_cpu(revinfo.corerev);
228 		ri->boardid = le32_to_cpu(revinfo.boardid);
229 		ri->boardvendor = le32_to_cpu(revinfo.boardvendor);
230 		ri->boardrev = le32_to_cpu(revinfo.boardrev);
231 		ri->driverrev = le32_to_cpu(revinfo.driverrev);
232 		ri->ucoderev = le32_to_cpu(revinfo.ucoderev);
233 		ri->bus = le32_to_cpu(revinfo.bus);
234 		ri->phytype = le32_to_cpu(revinfo.phytype);
235 		ri->phyrev = le32_to_cpu(revinfo.phyrev);
236 		ri->anarev = le32_to_cpu(revinfo.anarev);
237 		ri->chippkg = le32_to_cpu(revinfo.chippkg);
238 		ri->nvramrev = le32_to_cpu(revinfo.nvramrev);
239 
240 		/* use revinfo if not known yet */
241 		if (!bus->chip) {
242 			bus->chip = le32_to_cpu(revinfo.chipnum);
243 			bus->chiprev = le32_to_cpu(revinfo.chiprev);
244 		}
245 	}
246 	ri->result = err;
247 
248 	if (bus->chip)
249 		brcmf_chip_name(bus->chip, bus->chiprev,
250 				ri->chipname, sizeof(ri->chipname));
251 
252 	/* Do any CLM downloading */
253 	err = brcmf_c_process_clm_blob(ifp);
254 	if (err < 0) {
255 		bphy_err(drvr, "download CLM blob file failed, %d\n", err);
256 		goto done;
257 	}
258 
259 	/* query for 'ver' to get version info from firmware */
260 	memset(buf, 0, sizeof(buf));
261 	strlcpy(buf, "ver", sizeof(buf));
262 	err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
263 	if (err < 0) {
264 		bphy_err(drvr, "Retrieving version information failed, %d\n",
265 			 err);
266 		goto done;
267 	}
268 	ptr = (char *)buf;
269 	strsep(&ptr, "\n");
270 
271 	/* Print fw version info */
272 	brcmf_info("Firmware: %s %s\n", ri->chipname, buf);
273 
274 	/* locate firmware version number for ethtool */
275 	ptr = strrchr(buf, ' ') + 1;
276 	strlcpy(ifp->drvr->fwver, ptr, sizeof(ifp->drvr->fwver));
277 
278 	/* Query for 'clmver' to get CLM version info from firmware */
279 	memset(buf, 0, sizeof(buf));
280 	err = brcmf_fil_iovar_data_get(ifp, "clmver", buf, sizeof(buf));
281 	if (err) {
282 		brcmf_dbg(TRACE, "retrieving clmver failed, %d\n", err);
283 	} else {
284 		clmver = (char *)buf;
285 		/* store CLM version for adding it to revinfo debugfs file */
286 		memcpy(ifp->drvr->clmver, clmver, sizeof(ifp->drvr->clmver));
287 
288 		/* Replace all newline/linefeed characters with space
289 		 * character
290 		 */
291 		strreplace(clmver, '\n', ' ');
292 
293 		brcmf_dbg(INFO, "CLM version = %s\n", clmver);
294 	}
295 
296 	/* set mpc */
297 	err = brcmf_fil_iovar_int_set(ifp, "mpc", 1);
298 	if (err) {
299 		bphy_err(drvr, "failed setting mpc\n");
300 		goto done;
301 	}
302 
303 	brcmf_c_set_joinpref_default(ifp);
304 
305 	/* Setup event_msgs, enable E_IF */
306 	err = brcmf_fil_iovar_data_get(ifp, "event_msgs", eventmask,
307 				       BRCMF_EVENTING_MASK_LEN);
308 	if (err) {
309 		bphy_err(drvr, "Get event_msgs error (%d)\n", err);
310 		goto done;
311 	}
312 	setbit(eventmask, BRCMF_E_IF);
313 	err = brcmf_fil_iovar_data_set(ifp, "event_msgs", eventmask,
314 				       BRCMF_EVENTING_MASK_LEN);
315 	if (err) {
316 		bphy_err(drvr, "Set event_msgs error (%d)\n", err);
317 		goto done;
318 	}
319 
320 	/* Setup default scan channel time */
321 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
322 				    BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
323 	if (err) {
324 		bphy_err(drvr, "BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
325 			 err);
326 		goto done;
327 	}
328 
329 	/* Setup default scan unassoc time */
330 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
331 				    BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
332 	if (err) {
333 		bphy_err(drvr, "BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
334 			 err);
335 		goto done;
336 	}
337 
338 	/* Enable tx beamforming, errors can be ignored (not supported) */
339 	(void)brcmf_fil_iovar_int_set(ifp, "txbf", 1);
340 done:
341 	return err;
342 }
343 
344 #ifndef CONFIG_BRCM_TRACING
345 void __brcmf_err(struct brcmf_bus *bus, const char *func, const char *fmt, ...)
346 {
347 	struct va_format vaf;
348 	va_list args;
349 
350 	va_start(args, fmt);
351 
352 	vaf.fmt = fmt;
353 	vaf.va = &args;
354 	if (bus)
355 		dev_err(bus->dev, "%s: %pV", func, &vaf);
356 	else
357 		pr_err("%s: %pV", func, &vaf);
358 
359 	va_end(args);
360 }
361 #endif
362 
363 #if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
364 void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
365 {
366 	struct va_format vaf = {
367 		.fmt = fmt,
368 	};
369 	va_list args;
370 
371 	va_start(args, fmt);
372 	vaf.va = &args;
373 	if (brcmf_msg_level & level)
374 		pr_debug("%s %pV", func, &vaf);
375 	trace_brcmf_dbg(level, func, &vaf);
376 	va_end(args);
377 }
378 #endif
379 
380 static void brcmf_mp_attach(void)
381 {
382 	/* If module param firmware path is set then this will always be used,
383 	 * if not set then if available use the platform data version. To make
384 	 * sure it gets initialized at all, always copy the module param version
385 	 */
386 	strlcpy(brcmf_mp_global.firmware_path, brcmf_firmware_path,
387 		BRCMF_FW_ALTPATH_LEN);
388 	if ((brcmfmac_pdata) && (brcmfmac_pdata->fw_alternative_path) &&
389 	    (brcmf_mp_global.firmware_path[0] == '\0')) {
390 		strlcpy(brcmf_mp_global.firmware_path,
391 			brcmfmac_pdata->fw_alternative_path,
392 			BRCMF_FW_ALTPATH_LEN);
393 	}
394 }
395 
396 struct brcmf_mp_device *brcmf_get_module_param(struct device *dev,
397 					       enum brcmf_bus_type bus_type,
398 					       u32 chip, u32 chiprev)
399 {
400 	struct brcmf_mp_device *settings;
401 	struct brcmfmac_pd_device *device_pd;
402 	bool found;
403 	int i;
404 
405 	brcmf_dbg(INFO, "Enter, bus=%d, chip=%d, rev=%d\n", bus_type, chip,
406 		  chiprev);
407 	settings = kzalloc(sizeof(*settings), GFP_ATOMIC);
408 	if (!settings)
409 		return NULL;
410 
411 	/* start by using the module paramaters */
412 	settings->p2p_enable = !!brcmf_p2p_enable;
413 	settings->feature_disable = brcmf_feature_disable;
414 	settings->fcmode = brcmf_fcmode;
415 	settings->roamoff = !!brcmf_roamoff;
416 	settings->iapp = !!brcmf_iapp_enable;
417 #ifdef DEBUG
418 	settings->ignore_probe_fail = !!brcmf_ignore_probe_fail;
419 #endif
420 
421 	if (bus_type == BRCMF_BUSTYPE_SDIO)
422 		settings->bus.sdio.txglomsz = brcmf_sdiod_txglomsz;
423 
424 	/* See if there is any device specific platform data configured */
425 	found = false;
426 	if (brcmfmac_pdata) {
427 		for (i = 0; i < brcmfmac_pdata->device_count; i++) {
428 			device_pd = &brcmfmac_pdata->devices[i];
429 			if ((device_pd->bus_type == bus_type) &&
430 			    (device_pd->id == chip) &&
431 			    ((device_pd->rev == chiprev) ||
432 			     (device_pd->rev == -1))) {
433 				brcmf_dbg(INFO, "Platform data for device found\n");
434 				settings->country_codes =
435 						device_pd->country_codes;
436 				if (device_pd->bus_type == BRCMF_BUSTYPE_SDIO)
437 					memcpy(&settings->bus.sdio,
438 					       &device_pd->bus.sdio,
439 					       sizeof(settings->bus.sdio));
440 				found = true;
441 				break;
442 			}
443 		}
444 	}
445 	if (!found) {
446 		/* No platform data for this device, try OF and DMI data */
447 		brcmf_dmi_probe(settings, chip, chiprev);
448 		brcmf_of_probe(dev, bus_type, settings);
449 	}
450 	return settings;
451 }
452 
453 void brcmf_release_module_param(struct brcmf_mp_device *module_param)
454 {
455 	kfree(module_param);
456 }
457 
458 static int __init brcmf_common_pd_probe(struct platform_device *pdev)
459 {
460 	brcmf_dbg(INFO, "Enter\n");
461 
462 	brcmfmac_pdata = dev_get_platdata(&pdev->dev);
463 
464 	if (brcmfmac_pdata->power_on)
465 		brcmfmac_pdata->power_on();
466 
467 	return 0;
468 }
469 
470 static int brcmf_common_pd_remove(struct platform_device *pdev)
471 {
472 	brcmf_dbg(INFO, "Enter\n");
473 
474 	if (brcmfmac_pdata->power_off)
475 		brcmfmac_pdata->power_off();
476 
477 	return 0;
478 }
479 
480 static struct platform_driver brcmf_pd = {
481 	.remove		= brcmf_common_pd_remove,
482 	.driver		= {
483 		.name	= BRCMFMAC_PDATA_NAME,
484 	}
485 };
486 
487 static int __init brcmfmac_module_init(void)
488 {
489 	int err;
490 
491 	/* Get the platform data (if available) for our devices */
492 	err = platform_driver_probe(&brcmf_pd, brcmf_common_pd_probe);
493 	if (err == -ENODEV)
494 		brcmf_dbg(INFO, "No platform data available.\n");
495 
496 	/* Initialize global module paramaters */
497 	brcmf_mp_attach();
498 
499 	/* Continue the initialization by registering the different busses */
500 	err = brcmf_core_init();
501 	if (err) {
502 		if (brcmfmac_pdata)
503 			platform_driver_unregister(&brcmf_pd);
504 	}
505 
506 	return err;
507 }
508 
509 static void __exit brcmfmac_module_exit(void)
510 {
511 	brcmf_core_exit();
512 	if (brcmfmac_pdata)
513 		platform_driver_unregister(&brcmf_pd);
514 }
515 
516 module_init(brcmfmac_module_init);
517 module_exit(brcmfmac_module_exit);
518 
519