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
24MODULE_AUTHOR("Broadcom Corporation");
25MODULE_DESCRIPTION("Broadcom 802.11 wireless LAN fullmac driver.");
26MODULE_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
36static int brcmf_sdiod_txglomsz = BRCMF_DEFAULT_TXGLOM_SIZE;
37module_param_named(txglomsz, brcmf_sdiod_txglomsz, int, 0);
38MODULE_PARM_DESC(txglomsz, "Maximum tx packet chain size [SDIO]");
39
40/* Debug level configuration. See debug.h for bits, sysfs modifiable */
41int brcmf_msg_level;
42module_param_named(debug, brcmf_msg_level, int, 0600);
43MODULE_PARM_DESC(debug, "Level of debug output");
44
45static int brcmf_p2p_enable;
46module_param_named(p2pon, brcmf_p2p_enable, int, 0);
47MODULE_PARM_DESC(p2pon, "Enable legacy p2p management functionality");
48
49static int brcmf_feature_disable;
50module_param_named(feature_disable, brcmf_feature_disable, int, 0);
51MODULE_PARM_DESC(feature_disable, "Disable features");
52
53static char brcmf_firmware_path[BRCMF_FW_ALTPATH_LEN];
54module_param_string(alternative_fw_path, brcmf_firmware_path,
55		    BRCMF_FW_ALTPATH_LEN, 0400);
56MODULE_PARM_DESC(alternative_fw_path, "Alternative firmware path");
57
58static int brcmf_fcmode;
59module_param_named(fcmode, brcmf_fcmode, int, 0);
60MODULE_PARM_DESC(fcmode, "Mode of firmware signalled flow control");
61
62static int brcmf_roamoff;
63module_param_named(roamoff, brcmf_roamoff, int, 0400);
64MODULE_PARM_DESC(roamoff, "Do not use internal roaming engine");
65
66static int brcmf_iapp_enable;
67module_param_named(iapp, brcmf_iapp_enable, int, 0);
68MODULE_PARM_DESC(iapp, "Enable partial support for the obsoleted Inter-Access Point Protocol");
69
70#ifdef DEBUG
71/* always succeed brcmf_bus_started() */
72static int brcmf_ignore_probe_fail;
73module_param_named(ignore_probe_fail, brcmf_ignore_probe_fail, int, 0);
74MODULE_PARM_DESC(ignore_probe_fail, "always succeed probe for debugging");
75#endif
76
77static struct brcmfmac_platform_data *brcmfmac_pdata;
78struct brcmf_mp_global_t brcmf_mp_global;
79
80void 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
102static int brcmf_c_download(struct brcmf_if *ifp, u16 flag,
103			    struct brcmf_dload_data_le *dload_buf,
104			    u32 len, const char *var)
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, var, dload_buf,
115				       struct_size(dload_buf, data, len));
116
117	return err;
118}
119
120static int brcmf_c_download_blob(struct brcmf_if *ifp,
121				 const void *data, size_t size,
122				 const char *loadvar, const char *statvar)
123{
124	struct brcmf_pub *drvr = ifp->drvr;
125	struct brcmf_dload_data_le *chunk_buf;
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	chunk_buf = kzalloc(struct_size(chunk_buf, data, MAX_CHUNK_LEN),
136			    GFP_KERNEL);
137	if (!chunk_buf) {
138		err = -ENOMEM;
139		return -ENOMEM;
140	}
141
142	datalen = size;
143	cumulative_len = 0;
144	do {
145		if (datalen > MAX_CHUNK_LEN) {
146			chunk_len = MAX_CHUNK_LEN;
147		} else {
148			chunk_len = datalen;
149			dl_flag |= DL_END;
150		}
151		memcpy(chunk_buf->data, data + cumulative_len, chunk_len);
152
153		err = brcmf_c_download(ifp, dl_flag, chunk_buf, chunk_len,
154				       loadvar);
155
156		dl_flag &= ~DL_BEGIN;
157
158		cumulative_len += chunk_len;
159		datalen -= chunk_len;
160	} while ((datalen > 0) && (err == 0));
161
162	if (err) {
163		bphy_err(drvr, "%s (%zu byte file) failed (%d)\n",
164			 loadvar, size, err);
165		/* Retrieve status and print */
166		err = brcmf_fil_iovar_int_get(ifp, statvar, &status);
167		if (err)
168			bphy_err(drvr, "get %s failed (%d)\n", statvar, err);
169		else
170			brcmf_dbg(INFO, "%s=%d\n", statvar, status);
171		err = -EIO;
172	}
173
174	kfree(chunk_buf);
175	return err;
176}
177
178static int brcmf_c_process_clm_blob(struct brcmf_if *ifp)
179{
180	struct brcmf_pub *drvr = ifp->drvr;
181	struct brcmf_bus *bus = drvr->bus_if;
182	const struct firmware *fw = NULL;
183	s32 err;
184
185	brcmf_dbg(TRACE, "Enter\n");
186
187	err = brcmf_bus_get_blob(bus, &fw, BRCMF_BLOB_CLM);
188	if (err || !fw) {
189		brcmf_info("no clm_blob available (err=%d), device may have limited channels available\n",
190			   err);
191		return 0;
192	}
193
194	err = brcmf_c_download_blob(ifp, fw->data, fw->size,
195				    "clmload", "clmload_status");
196
197	release_firmware(fw);
198	return err;
199}
200
201static int brcmf_c_process_txcap_blob(struct brcmf_if *ifp)
202{
203	struct brcmf_pub *drvr = ifp->drvr;
204	struct brcmf_bus *bus = drvr->bus_if;
205	const struct firmware *fw = NULL;
206	s32 err;
207
208	brcmf_dbg(TRACE, "Enter\n");
209
210	err = brcmf_bus_get_blob(bus, &fw, BRCMF_BLOB_TXCAP);
211	if (err || !fw) {
212		brcmf_info("no txcap_blob available (err=%d)\n", err);
213		return 0;
214	}
215
216	brcmf_info("TxCap blob found, loading\n");
217	err = brcmf_c_download_blob(ifp, fw->data, fw->size,
218				    "txcapload", "txcapload_status");
219
220	release_firmware(fw);
221	return err;
222}
223
224int brcmf_c_set_cur_etheraddr(struct brcmf_if *ifp, const u8 *addr)
225{
226	s32 err;
227
228	err = brcmf_fil_iovar_data_set(ifp, "cur_etheraddr", addr, ETH_ALEN);
229	if (err < 0)
230		bphy_err(ifp->drvr, "Setting cur_etheraddr failed, %d\n", err);
231
232	return err;
233}
234
235/* On some boards there is no eeprom to hold the nvram, in this case instead
236 * a board specific nvram is loaded from /lib/firmware. On most boards the
237 * macaddr setting in the /lib/firmware nvram file is ignored because the
238 * wifibt chip has a unique MAC programmed into the chip itself.
239 * But in some cases the actual MAC from the /lib/firmware nvram file gets
240 * used, leading to MAC conflicts.
241 * The MAC addresses in the troublesome nvram files seem to all come from
242 * the same nvram file template, so we only need to check for 1 known
243 * address to detect this.
244 */
245static const u8 brcmf_default_mac_address[ETH_ALEN] = {
246	0x00, 0x90, 0x4c, 0xc5, 0x12, 0x38
247};
248
249static int brcmf_c_process_cal_blob(struct brcmf_if *ifp)
250{
251	struct brcmf_pub *drvr = ifp->drvr;
252	struct brcmf_mp_device *settings = drvr->settings;
253	s32 err;
254
255	brcmf_dbg(TRACE, "Enter\n");
256
257	if (!settings->cal_blob || !settings->cal_size)
258		return 0;
259
260	brcmf_info("Calibration blob provided by platform, loading\n");
261	err = brcmf_c_download_blob(ifp, settings->cal_blob, settings->cal_size,
262				    "calload", "calload_status");
263	return err;
264}
265
266int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
267{
268	struct brcmf_pub *drvr = ifp->drvr;
269	s8 eventmask[BRCMF_EVENTING_MASK_LEN];
270	u8 buf[BRCMF_DCMD_SMLEN];
271	struct brcmf_bus *bus;
272	struct brcmf_rev_info_le revinfo;
273	struct brcmf_rev_info *ri;
274	char *clmver;
275	char *ptr;
276	s32 err;
277
278	if (is_valid_ether_addr(ifp->mac_addr)) {
279		/* set mac address */
280		err = brcmf_c_set_cur_etheraddr(ifp, ifp->mac_addr);
281		if (err < 0)
282			goto done;
283	} else {
284		/* retrieve mac address */
285		err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
286					       sizeof(ifp->mac_addr));
287		if (err < 0) {
288			bphy_err(drvr, "Retrieving cur_etheraddr failed, %d\n", err);
289			goto done;
290		}
291
292		if (ether_addr_equal_unaligned(ifp->mac_addr, brcmf_default_mac_address)) {
293			bphy_err(drvr, "Default MAC is used, replacing with random MAC to avoid conflicts\n");
294			eth_random_addr(ifp->mac_addr);
295			ifp->ndev->addr_assign_type = NET_ADDR_RANDOM;
296			err = brcmf_c_set_cur_etheraddr(ifp, ifp->mac_addr);
297			if (err < 0)
298				goto done;
299		}
300	}
301
302	memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
303	memcpy(ifp->drvr->wiphy->perm_addr, ifp->drvr->mac, ETH_ALEN);
304
305	bus = ifp->drvr->bus_if;
306	ri = &ifp->drvr->revinfo;
307
308	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_REVINFO,
309				     &revinfo, sizeof(revinfo));
310	if (err < 0) {
311		bphy_err(drvr, "retrieving revision info failed, %d\n", err);
312		strscpy(ri->chipname, "UNKNOWN", sizeof(ri->chipname));
313	} else {
314		ri->vendorid = le32_to_cpu(revinfo.vendorid);
315		ri->deviceid = le32_to_cpu(revinfo.deviceid);
316		ri->radiorev = le32_to_cpu(revinfo.radiorev);
317		ri->corerev = le32_to_cpu(revinfo.corerev);
318		ri->boardid = le32_to_cpu(revinfo.boardid);
319		ri->boardvendor = le32_to_cpu(revinfo.boardvendor);
320		ri->boardrev = le32_to_cpu(revinfo.boardrev);
321		ri->driverrev = le32_to_cpu(revinfo.driverrev);
322		ri->ucoderev = le32_to_cpu(revinfo.ucoderev);
323		ri->bus = le32_to_cpu(revinfo.bus);
324		ri->phytype = le32_to_cpu(revinfo.phytype);
325		ri->phyrev = le32_to_cpu(revinfo.phyrev);
326		ri->anarev = le32_to_cpu(revinfo.anarev);
327		ri->chippkg = le32_to_cpu(revinfo.chippkg);
328		ri->nvramrev = le32_to_cpu(revinfo.nvramrev);
329
330		/* use revinfo if not known yet */
331		if (!bus->chip) {
332			bus->chip = le32_to_cpu(revinfo.chipnum);
333			bus->chiprev = le32_to_cpu(revinfo.chiprev);
334		}
335	}
336	ri->result = err;
337
338	if (bus->chip)
339		brcmf_chip_name(bus->chip, bus->chiprev,
340				ri->chipname, sizeof(ri->chipname));
341
342	/* Do any CLM downloading */
343	err = brcmf_c_process_clm_blob(ifp);
344	if (err < 0) {
345		bphy_err(drvr, "download CLM blob file failed, %d\n", err);
346		goto done;
347	}
348
349	/* Do TxCap downloading, if needed */
350	err = brcmf_c_process_txcap_blob(ifp);
351	if (err < 0) {
352		bphy_err(drvr, "download TxCap blob file failed, %d\n", err);
353		goto done;
354	}
355
356	/* Download external calibration blob, if available */
357	err = brcmf_c_process_cal_blob(ifp);
358	if (err < 0) {
359		bphy_err(drvr, "download calibration blob file failed, %d\n", err);
360		goto done;
361	}
362
363	/* query for 'ver' to get version info from firmware */
364	memset(buf, 0, sizeof(buf));
365	err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
366	if (err < 0) {
367		bphy_err(drvr, "Retrieving version information failed, %d\n",
368			 err);
369		goto done;
370	}
371	buf[sizeof(buf) - 1] = '\0';
372	ptr = (char *)buf;
373	strsep(&ptr, "\n");
374
375	/* Print fw version info */
376	brcmf_info("Firmware: %s %s\n", ri->chipname, buf);
377
378	/* locate firmware version number for ethtool */
379	ptr = strrchr(buf, ' ');
380	if (!ptr) {
381		bphy_err(drvr, "Retrieving version number failed");
382		goto done;
383	}
384	strscpy(ifp->drvr->fwver, ptr + 1, sizeof(ifp->drvr->fwver));
385
386	/* Query for 'clmver' to get CLM version info from firmware */
387	memset(buf, 0, sizeof(buf));
388	err = brcmf_fil_iovar_data_get(ifp, "clmver", buf, sizeof(buf));
389	if (err) {
390		brcmf_dbg(TRACE, "retrieving clmver failed, %d\n", err);
391	} else {
392		buf[sizeof(buf) - 1] = '\0';
393		clmver = (char *)buf;
394
395		/* Replace all newline/linefeed characters with space
396		 * character
397		 */
398		strreplace(clmver, '\n', ' ');
399
400		/* store CLM version for adding it to revinfo debugfs file */
401		memcpy(ifp->drvr->clmver, clmver, sizeof(ifp->drvr->clmver));
402
403		brcmf_dbg(INFO, "CLM version = %s\n", clmver);
404	}
405
406	/* set mpc */
407	err = brcmf_fil_iovar_int_set(ifp, "mpc", 1);
408	if (err) {
409		bphy_err(drvr, "failed setting mpc\n");
410		goto done;
411	}
412
413	brcmf_c_set_joinpref_default(ifp);
414
415	/* Setup event_msgs, enable E_IF */
416	err = brcmf_fil_iovar_data_get(ifp, "event_msgs", eventmask,
417				       BRCMF_EVENTING_MASK_LEN);
418	if (err) {
419		bphy_err(drvr, "Get event_msgs error (%d)\n", err);
420		goto done;
421	}
422	setbit(eventmask, BRCMF_E_IF);
423	err = brcmf_fil_iovar_data_set(ifp, "event_msgs", eventmask,
424				       BRCMF_EVENTING_MASK_LEN);
425	if (err) {
426		bphy_err(drvr, "Set event_msgs error (%d)\n", err);
427		goto done;
428	}
429
430	/* Setup default scan channel time */
431	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
432				    BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
433	if (err) {
434		bphy_err(drvr, "BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
435			 err);
436		goto done;
437	}
438
439	/* Setup default scan unassoc time */
440	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
441				    BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
442	if (err) {
443		bphy_err(drvr, "BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
444			 err);
445		goto done;
446	}
447
448	/* Enable tx beamforming, errors can be ignored (not supported) */
449	(void)brcmf_fil_iovar_int_set(ifp, "txbf", 1);
450done:
451	return err;
452}
453
454#ifndef CONFIG_BRCM_TRACING
455void __brcmf_err(struct brcmf_bus *bus, const char *func, const char *fmt, ...)
456{
457	struct va_format vaf;
458	va_list args;
459
460	va_start(args, fmt);
461
462	vaf.fmt = fmt;
463	vaf.va = &args;
464	if (bus)
465		dev_err(bus->dev, "%s: %pV", func, &vaf);
466	else
467		pr_err("%s: %pV", func, &vaf);
468
469	va_end(args);
470}
471#endif
472
473#if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
474void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
475{
476	struct va_format vaf = {
477		.fmt = fmt,
478	};
479	va_list args;
480
481	va_start(args, fmt);
482	vaf.va = &args;
483	if (brcmf_msg_level & level)
484		pr_debug("%s %pV", func, &vaf);
485	trace_brcmf_dbg(level, func, &vaf);
486	va_end(args);
487}
488#endif
489
490static void brcmf_mp_attach(void)
491{
492	/* If module param firmware path is set then this will always be used,
493	 * if not set then if available use the platform data version. To make
494	 * sure it gets initialized at all, always copy the module param version
495	 */
496	strscpy(brcmf_mp_global.firmware_path, brcmf_firmware_path,
497		BRCMF_FW_ALTPATH_LEN);
498	if ((brcmfmac_pdata) && (brcmfmac_pdata->fw_alternative_path) &&
499	    (brcmf_mp_global.firmware_path[0] == '\0')) {
500		strscpy(brcmf_mp_global.firmware_path,
501			brcmfmac_pdata->fw_alternative_path,
502			BRCMF_FW_ALTPATH_LEN);
503	}
504}
505
506struct brcmf_mp_device *brcmf_get_module_param(struct device *dev,
507					       enum brcmf_bus_type bus_type,
508					       u32 chip, u32 chiprev)
509{
510	struct brcmf_mp_device *settings;
511	struct brcmfmac_pd_device *device_pd;
512	bool found;
513	int i;
514
515	brcmf_dbg(INFO, "Enter, bus=%d, chip=%d, rev=%d\n", bus_type, chip,
516		  chiprev);
517	settings = kzalloc(sizeof(*settings), GFP_ATOMIC);
518	if (!settings)
519		return NULL;
520
521	/* start by using the module paramaters */
522	settings->p2p_enable = !!brcmf_p2p_enable;
523	settings->feature_disable = brcmf_feature_disable;
524	settings->fcmode = brcmf_fcmode;
525	settings->roamoff = !!brcmf_roamoff;
526	settings->iapp = !!brcmf_iapp_enable;
527#ifdef DEBUG
528	settings->ignore_probe_fail = !!brcmf_ignore_probe_fail;
529#endif
530
531	if (bus_type == BRCMF_BUSTYPE_SDIO)
532		settings->bus.sdio.txglomsz = brcmf_sdiod_txglomsz;
533
534	/* See if there is any device specific platform data configured */
535	found = false;
536	if (brcmfmac_pdata) {
537		for (i = 0; i < brcmfmac_pdata->device_count; i++) {
538			device_pd = &brcmfmac_pdata->devices[i];
539			if ((device_pd->bus_type == bus_type) &&
540			    (device_pd->id == chip) &&
541			    ((device_pd->rev == chiprev) ||
542			     (device_pd->rev == -1))) {
543				brcmf_dbg(INFO, "Platform data for device found\n");
544				settings->country_codes =
545						device_pd->country_codes;
546				if (device_pd->bus_type == BRCMF_BUSTYPE_SDIO)
547					memcpy(&settings->bus.sdio,
548					       &device_pd->bus.sdio,
549					       sizeof(settings->bus.sdio));
550				found = true;
551				break;
552			}
553		}
554	}
555	if (!found) {
556		/* No platform data for this device, try OF and DMI data */
557		brcmf_dmi_probe(settings, chip, chiprev);
558		brcmf_of_probe(dev, bus_type, settings);
559		brcmf_acpi_probe(dev, bus_type, settings);
560	}
561	return settings;
562}
563
564void brcmf_release_module_param(struct brcmf_mp_device *module_param)
565{
566	kfree(module_param);
567}
568
569static int __init brcmf_common_pd_probe(struct platform_device *pdev)
570{
571	brcmf_dbg(INFO, "Enter\n");
572
573	brcmfmac_pdata = dev_get_platdata(&pdev->dev);
574
575	if (brcmfmac_pdata->power_on)
576		brcmfmac_pdata->power_on();
577
578	return 0;
579}
580
581static int brcmf_common_pd_remove(struct platform_device *pdev)
582{
583	brcmf_dbg(INFO, "Enter\n");
584
585	if (brcmfmac_pdata->power_off)
586		brcmfmac_pdata->power_off();
587
588	return 0;
589}
590
591static struct platform_driver brcmf_pd = {
592	.remove		= brcmf_common_pd_remove,
593	.driver		= {
594		.name	= BRCMFMAC_PDATA_NAME,
595	}
596};
597
598static int __init brcmfmac_module_init(void)
599{
600	int err;
601
602	/* Get the platform data (if available) for our devices */
603	err = platform_driver_probe(&brcmf_pd, brcmf_common_pd_probe);
604	if (err == -ENODEV)
605		brcmf_dbg(INFO, "No platform data available.\n");
606
607	/* Initialize global module paramaters */
608	brcmf_mp_attach();
609
610	/* Continue the initialization by registering the different busses */
611	err = brcmf_core_init();
612	if (err) {
613		if (brcmfmac_pdata)
614			platform_driver_unregister(&brcmf_pd);
615	}
616
617	return err;
618}
619
620static void __exit brcmfmac_module_exit(void)
621{
622	brcmf_core_exit();
623	if (brcmfmac_pdata)
624		platform_driver_unregister(&brcmf_pd);
625}
626
627module_init(brcmfmac_module_init);
628module_exit(brcmfmac_module_exit);
629
630