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 
114 	err = brcmf_fil_iovar_data_set(ifp, "clmload", dload_buf,
115 				       struct_size(dload_buf, data, 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 	u32 chunk_len;
127 	u32 datalen;
128 	u32 cumulative_len;
129 	u16 dl_flag = DL_BEGIN;
130 	u32 status;
131 	s32 err;
132 
133 	brcmf_dbg(TRACE, "Enter\n");
134 
135 	err = brcmf_bus_get_blob(bus, &clm, BRCMF_BLOB_CLM);
136 	if (err || !clm) {
137 		brcmf_info("no clm_blob available (err=%d), device may have limited channels available\n",
138 			   err);
139 		return 0;
140 	}
141 
142 	chunk_buf = kzalloc(struct_size(chunk_buf, data, MAX_CHUNK_LEN),
143 			    GFP_KERNEL);
144 	if (!chunk_buf) {
145 		err = -ENOMEM;
146 		goto done;
147 	}
148 
149 	datalen = clm->size;
150 	cumulative_len = 0;
151 	do {
152 		if (datalen > MAX_CHUNK_LEN) {
153 			chunk_len = MAX_CHUNK_LEN;
154 		} else {
155 			chunk_len = datalen;
156 			dl_flag |= DL_END;
157 		}
158 		memcpy(chunk_buf->data, clm->data + cumulative_len, chunk_len);
159 
160 		err = brcmf_c_download(ifp, dl_flag, chunk_buf, chunk_len);
161 
162 		dl_flag &= ~DL_BEGIN;
163 
164 		cumulative_len += chunk_len;
165 		datalen -= chunk_len;
166 	} while ((datalen > 0) && (err == 0));
167 
168 	if (err) {
169 		bphy_err(drvr, "clmload (%zu byte file) failed (%d)\n",
170 			 clm->size, err);
171 		/* Retrieve clmload_status and print */
172 		err = brcmf_fil_iovar_int_get(ifp, "clmload_status", &status);
173 		if (err)
174 			bphy_err(drvr, "get clmload_status failed (%d)\n", err);
175 		else
176 			brcmf_dbg(INFO, "clmload_status=%d\n", status);
177 		err = -EIO;
178 	}
179 
180 	kfree(chunk_buf);
181 done:
182 	release_firmware(clm);
183 	return err;
184 }
185 
186 int brcmf_c_set_cur_etheraddr(struct brcmf_if *ifp, const u8 *addr)
187 {
188 	s32 err;
189 
190 	err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", addr, ETH_ALEN);
191 	if (err < 0)
192 		bphy_err(ifp->drvr, "Setting cur_etheraddr failed, %d\n", err);
193 
194 	return err;
195 }
196 
197 /* On some boards there is no eeprom to hold the nvram, in this case instead
198  * a board specific nvram is loaded from /lib/firmware. On most boards the
199  * macaddr setting in the /lib/firmware nvram file is ignored because the
200  * wifibt chip has a unique MAC programmed into the chip itself.
201  * But in some cases the actual MAC from the /lib/firmware nvram file gets
202  * used, leading to MAC conflicts.
203  * The MAC addresses in the troublesome nvram files seem to all come from
204  * the same nvram file template, so we only need to check for 1 known
205  * address to detect this.
206  */
207 static const u8 brcmf_default_mac_address[ETH_ALEN] = {
208 	0x00, 0x90, 0x4c, 0xc5, 0x12, 0x38
209 };
210 
211 int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
212 {
213 	struct brcmf_pub *drvr = ifp->drvr;
214 	s8 eventmask[BRCMF_EVENTING_MASK_LEN];
215 	u8 buf[BRCMF_DCMD_SMLEN];
216 	struct brcmf_bus *bus;
217 	struct brcmf_rev_info_le revinfo;
218 	struct brcmf_rev_info *ri;
219 	char *clmver;
220 	char *ptr;
221 	s32 err;
222 
223 	if (is_valid_ether_addr(ifp->mac_addr)) {
224 		/* set mac address */
225 		err = brcmf_c_set_cur_etheraddr(ifp, ifp->mac_addr);
226 		if (err < 0)
227 			goto done;
228 	} else {
229 		/* retrieve mac address */
230 		err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
231 					       sizeof(ifp->mac_addr));
232 		if (err < 0) {
233 			bphy_err(drvr, "Retrieving cur_etheraddr failed, %d\n", err);
234 			goto done;
235 		}
236 
237 		if (ether_addr_equal_unaligned(ifp->mac_addr, brcmf_default_mac_address)) {
238 			bphy_err(drvr, "Default MAC is used, replacing with random MAC to avoid conflicts\n");
239 			eth_random_addr(ifp->mac_addr);
240 			ifp->ndev->addr_assign_type = NET_ADDR_RANDOM;
241 			err = brcmf_c_set_cur_etheraddr(ifp, ifp->mac_addr);
242 			if (err < 0)
243 				goto done;
244 		}
245 	}
246 
247 	memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
248 	memcpy(ifp->drvr->wiphy->perm_addr, ifp->drvr->mac, ETH_ALEN);
249 
250 	bus = ifp->drvr->bus_if;
251 	ri = &ifp->drvr->revinfo;
252 
253 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_REVINFO,
254 				     &revinfo, sizeof(revinfo));
255 	if (err < 0) {
256 		bphy_err(drvr, "retrieving revision info failed, %d\n", err);
257 		strscpy(ri->chipname, "UNKNOWN", sizeof(ri->chipname));
258 	} else {
259 		ri->vendorid = le32_to_cpu(revinfo.vendorid);
260 		ri->deviceid = le32_to_cpu(revinfo.deviceid);
261 		ri->radiorev = le32_to_cpu(revinfo.radiorev);
262 		ri->corerev = le32_to_cpu(revinfo.corerev);
263 		ri->boardid = le32_to_cpu(revinfo.boardid);
264 		ri->boardvendor = le32_to_cpu(revinfo.boardvendor);
265 		ri->boardrev = le32_to_cpu(revinfo.boardrev);
266 		ri->driverrev = le32_to_cpu(revinfo.driverrev);
267 		ri->ucoderev = le32_to_cpu(revinfo.ucoderev);
268 		ri->bus = le32_to_cpu(revinfo.bus);
269 		ri->phytype = le32_to_cpu(revinfo.phytype);
270 		ri->phyrev = le32_to_cpu(revinfo.phyrev);
271 		ri->anarev = le32_to_cpu(revinfo.anarev);
272 		ri->chippkg = le32_to_cpu(revinfo.chippkg);
273 		ri->nvramrev = le32_to_cpu(revinfo.nvramrev);
274 
275 		/* use revinfo if not known yet */
276 		if (!bus->chip) {
277 			bus->chip = le32_to_cpu(revinfo.chipnum);
278 			bus->chiprev = le32_to_cpu(revinfo.chiprev);
279 		}
280 	}
281 	ri->result = err;
282 
283 	if (bus->chip)
284 		brcmf_chip_name(bus->chip, bus->chiprev,
285 				ri->chipname, sizeof(ri->chipname));
286 
287 	/* Do any CLM downloading */
288 	err = brcmf_c_process_clm_blob(ifp);
289 	if (err < 0) {
290 		bphy_err(drvr, "download CLM blob file failed, %d\n", err);
291 		goto done;
292 	}
293 
294 	/* query for 'ver' to get version info from firmware */
295 	memset(buf, 0, sizeof(buf));
296 	err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
297 	if (err < 0) {
298 		bphy_err(drvr, "Retrieving version information failed, %d\n",
299 			 err);
300 		goto done;
301 	}
302 	ptr = (char *)buf;
303 	strsep(&ptr, "\n");
304 
305 	/* Print fw version info */
306 	brcmf_info("Firmware: %s %s\n", ri->chipname, buf);
307 
308 	/* locate firmware version number for ethtool */
309 	ptr = strrchr(buf, ' ');
310 	if (!ptr) {
311 		bphy_err(drvr, "Retrieving version number failed");
312 		goto done;
313 	}
314 	strscpy(ifp->drvr->fwver, ptr + 1, sizeof(ifp->drvr->fwver));
315 
316 	/* Query for 'clmver' to get CLM version info from firmware */
317 	memset(buf, 0, sizeof(buf));
318 	err = brcmf_fil_iovar_data_get(ifp, "clmver", buf, sizeof(buf));
319 	if (err) {
320 		brcmf_dbg(TRACE, "retrieving clmver failed, %d\n", err);
321 	} else {
322 		clmver = (char *)buf;
323 		/* store CLM version for adding it to revinfo debugfs file */
324 		memcpy(ifp->drvr->clmver, clmver, sizeof(ifp->drvr->clmver));
325 
326 		/* Replace all newline/linefeed characters with space
327 		 * character
328 		 */
329 		strreplace(clmver, '\n', ' ');
330 
331 		brcmf_dbg(INFO, "CLM version = %s\n", clmver);
332 	}
333 
334 	/* set mpc */
335 	err = brcmf_fil_iovar_int_set(ifp, "mpc", 1);
336 	if (err) {
337 		bphy_err(drvr, "failed setting mpc\n");
338 		goto done;
339 	}
340 
341 	brcmf_c_set_joinpref_default(ifp);
342 
343 	/* Setup event_msgs, enable E_IF */
344 	err = brcmf_fil_iovar_data_get(ifp, "event_msgs", eventmask,
345 				       BRCMF_EVENTING_MASK_LEN);
346 	if (err) {
347 		bphy_err(drvr, "Get event_msgs error (%d)\n", err);
348 		goto done;
349 	}
350 	setbit(eventmask, BRCMF_E_IF);
351 	err = brcmf_fil_iovar_data_set(ifp, "event_msgs", eventmask,
352 				       BRCMF_EVENTING_MASK_LEN);
353 	if (err) {
354 		bphy_err(drvr, "Set event_msgs error (%d)\n", err);
355 		goto done;
356 	}
357 
358 	/* Setup default scan channel time */
359 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
360 				    BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
361 	if (err) {
362 		bphy_err(drvr, "BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
363 			 err);
364 		goto done;
365 	}
366 
367 	/* Setup default scan unassoc time */
368 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
369 				    BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
370 	if (err) {
371 		bphy_err(drvr, "BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
372 			 err);
373 		goto done;
374 	}
375 
376 	/* Enable tx beamforming, errors can be ignored (not supported) */
377 	(void)brcmf_fil_iovar_int_set(ifp, "txbf", 1);
378 done:
379 	return err;
380 }
381 
382 #ifndef CONFIG_BRCM_TRACING
383 void __brcmf_err(struct brcmf_bus *bus, const char *func, const char *fmt, ...)
384 {
385 	struct va_format vaf;
386 	va_list args;
387 
388 	va_start(args, fmt);
389 
390 	vaf.fmt = fmt;
391 	vaf.va = &args;
392 	if (bus)
393 		dev_err(bus->dev, "%s: %pV", func, &vaf);
394 	else
395 		pr_err("%s: %pV", func, &vaf);
396 
397 	va_end(args);
398 }
399 #endif
400 
401 #if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
402 void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
403 {
404 	struct va_format vaf = {
405 		.fmt = fmt,
406 	};
407 	va_list args;
408 
409 	va_start(args, fmt);
410 	vaf.va = &args;
411 	if (brcmf_msg_level & level)
412 		pr_debug("%s %pV", func, &vaf);
413 	trace_brcmf_dbg(level, func, &vaf);
414 	va_end(args);
415 }
416 #endif
417 
418 static void brcmf_mp_attach(void)
419 {
420 	/* If module param firmware path is set then this will always be used,
421 	 * if not set then if available use the platform data version. To make
422 	 * sure it gets initialized at all, always copy the module param version
423 	 */
424 	strscpy(brcmf_mp_global.firmware_path, brcmf_firmware_path,
425 		BRCMF_FW_ALTPATH_LEN);
426 	if ((brcmfmac_pdata) && (brcmfmac_pdata->fw_alternative_path) &&
427 	    (brcmf_mp_global.firmware_path[0] == '\0')) {
428 		strscpy(brcmf_mp_global.firmware_path,
429 			brcmfmac_pdata->fw_alternative_path,
430 			BRCMF_FW_ALTPATH_LEN);
431 	}
432 }
433 
434 struct brcmf_mp_device *brcmf_get_module_param(struct device *dev,
435 					       enum brcmf_bus_type bus_type,
436 					       u32 chip, u32 chiprev)
437 {
438 	struct brcmf_mp_device *settings;
439 	struct brcmfmac_pd_device *device_pd;
440 	bool found;
441 	int i;
442 
443 	brcmf_dbg(INFO, "Enter, bus=%d, chip=%d, rev=%d\n", bus_type, chip,
444 		  chiprev);
445 	settings = kzalloc(sizeof(*settings), GFP_ATOMIC);
446 	if (!settings)
447 		return NULL;
448 
449 	/* start by using the module paramaters */
450 	settings->p2p_enable = !!brcmf_p2p_enable;
451 	settings->feature_disable = brcmf_feature_disable;
452 	settings->fcmode = brcmf_fcmode;
453 	settings->roamoff = !!brcmf_roamoff;
454 	settings->iapp = !!brcmf_iapp_enable;
455 #ifdef DEBUG
456 	settings->ignore_probe_fail = !!brcmf_ignore_probe_fail;
457 #endif
458 
459 	if (bus_type == BRCMF_BUSTYPE_SDIO)
460 		settings->bus.sdio.txglomsz = brcmf_sdiod_txglomsz;
461 
462 	/* See if there is any device specific platform data configured */
463 	found = false;
464 	if (brcmfmac_pdata) {
465 		for (i = 0; i < brcmfmac_pdata->device_count; i++) {
466 			device_pd = &brcmfmac_pdata->devices[i];
467 			if ((device_pd->bus_type == bus_type) &&
468 			    (device_pd->id == chip) &&
469 			    ((device_pd->rev == chiprev) ||
470 			     (device_pd->rev == -1))) {
471 				brcmf_dbg(INFO, "Platform data for device found\n");
472 				settings->country_codes =
473 						device_pd->country_codes;
474 				if (device_pd->bus_type == BRCMF_BUSTYPE_SDIO)
475 					memcpy(&settings->bus.sdio,
476 					       &device_pd->bus.sdio,
477 					       sizeof(settings->bus.sdio));
478 				found = true;
479 				break;
480 			}
481 		}
482 	}
483 	if (!found) {
484 		/* No platform data for this device, try OF and DMI data */
485 		brcmf_dmi_probe(settings, chip, chiprev);
486 		brcmf_of_probe(dev, bus_type, settings);
487 	}
488 	return settings;
489 }
490 
491 void brcmf_release_module_param(struct brcmf_mp_device *module_param)
492 {
493 	kfree(module_param);
494 }
495 
496 static int __init brcmf_common_pd_probe(struct platform_device *pdev)
497 {
498 	brcmf_dbg(INFO, "Enter\n");
499 
500 	brcmfmac_pdata = dev_get_platdata(&pdev->dev);
501 
502 	if (brcmfmac_pdata->power_on)
503 		brcmfmac_pdata->power_on();
504 
505 	return 0;
506 }
507 
508 static int brcmf_common_pd_remove(struct platform_device *pdev)
509 {
510 	brcmf_dbg(INFO, "Enter\n");
511 
512 	if (brcmfmac_pdata->power_off)
513 		brcmfmac_pdata->power_off();
514 
515 	return 0;
516 }
517 
518 static struct platform_driver brcmf_pd = {
519 	.remove		= brcmf_common_pd_remove,
520 	.driver		= {
521 		.name	= BRCMFMAC_PDATA_NAME,
522 	}
523 };
524 
525 static int __init brcmfmac_module_init(void)
526 {
527 	int err;
528 
529 	/* Get the platform data (if available) for our devices */
530 	err = platform_driver_probe(&brcmf_pd, brcmf_common_pd_probe);
531 	if (err == -ENODEV)
532 		brcmf_dbg(INFO, "No platform data available.\n");
533 
534 	/* Initialize global module paramaters */
535 	brcmf_mp_attach();
536 
537 	/* Continue the initialization by registering the different busses */
538 	err = brcmf_core_init();
539 	if (err) {
540 		if (brcmfmac_pdata)
541 			platform_driver_unregister(&brcmf_pd);
542 	}
543 
544 	return err;
545 }
546 
547 static void __exit brcmfmac_module_exit(void)
548 {
549 	brcmf_core_exit();
550 	if (brcmfmac_pdata)
551 		platform_driver_unregister(&brcmf_pd);
552 }
553 
554 module_init(brcmfmac_module_init);
555 module_exit(brcmfmac_module_exit);
556 
557