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