1 /*
2  * Copyright (c) 2014 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/netdevice.h>
18 #include <linux/module.h>
19 
20 #include <brcm_hw_ids.h>
21 #include <brcmu_wifi.h>
22 #include "core.h"
23 #include "bus.h"
24 #include "debug.h"
25 #include "fwil.h"
26 #include "fwil_types.h"
27 #include "feature.h"
28 #include "common.h"
29 
30 #define BRCMF_FW_UNSUPPORTED	23
31 
32 /*
33  * expand feature list to array of feature strings.
34  */
35 #define BRCMF_FEAT_DEF(_f) \
36 	#_f,
37 static const char *brcmf_feat_names[] = {
38 	BRCMF_FEAT_LIST
39 };
40 #undef BRCMF_FEAT_DEF
41 
42 struct brcmf_feat_fwcap {
43 	enum brcmf_feat_id feature;
44 	const char * const fwcap_id;
45 };
46 
47 static const struct brcmf_feat_fwcap brcmf_fwcap_map[] = {
48 	{ BRCMF_FEAT_MBSS, "mbss" },
49 	{ BRCMF_FEAT_MCHAN, "mchan" },
50 	{ BRCMF_FEAT_P2P, "p2p" },
51 	{ BRCMF_FEAT_MONITOR, "monitor" },
52 	{ BRCMF_FEAT_MONITOR_FMT_RADIOTAP, "rtap" },
53 };
54 
55 #ifdef DEBUG
56 /*
57  * expand quirk list to array of quirk strings.
58  */
59 #define BRCMF_QUIRK_DEF(_q) \
60 	#_q,
61 static const char * const brcmf_quirk_names[] = {
62 	BRCMF_QUIRK_LIST
63 };
64 #undef BRCMF_QUIRK_DEF
65 
66 /**
67  * brcmf_feat_debugfs_read() - expose feature info to debugfs.
68  *
69  * @seq: sequence for debugfs entry.
70  * @data: raw data pointer.
71  */
72 static int brcmf_feat_debugfs_read(struct seq_file *seq, void *data)
73 {
74 	struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
75 	u32 feats = bus_if->drvr->feat_flags;
76 	u32 quirks = bus_if->drvr->chip_quirks;
77 	int id;
78 
79 	seq_printf(seq, "Features: %08x\n", feats);
80 	for (id = 0; id < BRCMF_FEAT_LAST; id++)
81 		if (feats & BIT(id))
82 			seq_printf(seq, "\t%s\n", brcmf_feat_names[id]);
83 	seq_printf(seq, "\nQuirks:   %08x\n", quirks);
84 	for (id = 0; id < BRCMF_FEAT_QUIRK_LAST; id++)
85 		if (quirks & BIT(id))
86 			seq_printf(seq, "\t%s\n", brcmf_quirk_names[id]);
87 	return 0;
88 }
89 #else
90 static int brcmf_feat_debugfs_read(struct seq_file *seq, void *data)
91 {
92 	return 0;
93 }
94 #endif /* DEBUG */
95 
96 struct brcmf_feat_fwfeat {
97 	const char * const fwid;
98 	u32 feat_flags;
99 };
100 
101 static const struct brcmf_feat_fwfeat brcmf_feat_fwfeat_map[] = {
102 	/* brcmfmac43602-pcie.ap.bin from linux-firmware.git commit ea1178515b88 */
103 	{ "01-6cb8e269", BIT(BRCMF_FEAT_MONITOR) },
104 	/* brcmfmac4366b-pcie.bin from linux-firmware.git commit 52442afee990 */
105 	{ "01-c47a91a4", BIT(BRCMF_FEAT_MONITOR) },
106 };
107 
108 static void brcmf_feat_firmware_overrides(struct brcmf_pub *drv)
109 {
110 	const struct brcmf_feat_fwfeat *e;
111 	u32 feat_flags = 0;
112 	int i;
113 
114 	for (i = 0; i < ARRAY_SIZE(brcmf_feat_fwfeat_map); i++) {
115 		e = &brcmf_feat_fwfeat_map[i];
116 		if (!strcmp(e->fwid, drv->fwver)) {
117 			feat_flags = e->feat_flags;
118 			break;
119 		}
120 	}
121 
122 	if (!feat_flags)
123 		return;
124 
125 	for (i = 0; i < BRCMF_FEAT_LAST; i++)
126 		if (feat_flags & BIT(i))
127 			brcmf_dbg(INFO, "enabling firmware feature: %s\n",
128 				  brcmf_feat_names[i]);
129 	drv->feat_flags |= feat_flags;
130 }
131 
132 /**
133  * brcmf_feat_iovar_int_get() - determine feature through iovar query.
134  *
135  * @ifp: interface to query.
136  * @id: feature id.
137  * @name: iovar name.
138  */
139 static void brcmf_feat_iovar_int_get(struct brcmf_if *ifp,
140 				     enum brcmf_feat_id id, char *name)
141 {
142 	u32 data;
143 	int err;
144 
145 	/* we need to know firmware error */
146 	ifp->fwil_fwerr = true;
147 
148 	err = brcmf_fil_iovar_int_get(ifp, name, &data);
149 	if (err == 0) {
150 		brcmf_dbg(INFO, "enabling feature: %s\n", brcmf_feat_names[id]);
151 		ifp->drvr->feat_flags |= BIT(id);
152 	} else {
153 		brcmf_dbg(TRACE, "%s feature check failed: %d\n",
154 			  brcmf_feat_names[id], err);
155 	}
156 
157 	ifp->fwil_fwerr = false;
158 }
159 
160 static void brcmf_feat_iovar_data_set(struct brcmf_if *ifp,
161 				      enum brcmf_feat_id id, char *name,
162 				      const void *data, size_t len)
163 {
164 	int err;
165 
166 	/* we need to know firmware error */
167 	ifp->fwil_fwerr = true;
168 
169 	err = brcmf_fil_iovar_data_set(ifp, name, data, len);
170 	if (err != -BRCMF_FW_UNSUPPORTED) {
171 		brcmf_dbg(INFO, "enabling feature: %s\n", brcmf_feat_names[id]);
172 		ifp->drvr->feat_flags |= BIT(id);
173 	} else {
174 		brcmf_dbg(TRACE, "%s feature check failed: %d\n",
175 			  brcmf_feat_names[id], err);
176 	}
177 
178 	ifp->fwil_fwerr = false;
179 }
180 
181 #define MAX_CAPS_BUFFER_SIZE	768
182 static void brcmf_feat_firmware_capabilities(struct brcmf_if *ifp)
183 {
184 	char caps[MAX_CAPS_BUFFER_SIZE];
185 	enum brcmf_feat_id id;
186 	int i, err;
187 
188 	err = brcmf_fil_iovar_data_get(ifp, "cap", caps, sizeof(caps));
189 	if (err) {
190 		brcmf_err("could not get firmware cap (%d)\n", err);
191 		return;
192 	}
193 
194 	brcmf_dbg(INFO, "[ %s]\n", caps);
195 
196 	for (i = 0; i < ARRAY_SIZE(brcmf_fwcap_map); i++) {
197 		if (strnstr(caps, brcmf_fwcap_map[i].fwcap_id, sizeof(caps))) {
198 			id = brcmf_fwcap_map[i].feature;
199 			brcmf_dbg(INFO, "enabling feature: %s\n",
200 				  brcmf_feat_names[id]);
201 			ifp->drvr->feat_flags |= BIT(id);
202 		}
203 	}
204 }
205 
206 /**
207  * brcmf_feat_fwcap_debugfs_read() - expose firmware capabilities to debugfs.
208  *
209  * @seq: sequence for debugfs entry.
210  * @data: raw data pointer.
211  */
212 static int brcmf_feat_fwcap_debugfs_read(struct seq_file *seq, void *data)
213 {
214 	struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
215 	struct brcmf_if *ifp = brcmf_get_ifp(bus_if->drvr, 0);
216 	char caps[MAX_CAPS_BUFFER_SIZE + 1] = { };
217 	char *tmp;
218 	int err;
219 
220 	err = brcmf_fil_iovar_data_get(ifp, "cap", caps, sizeof(caps));
221 	if (err) {
222 		brcmf_err("could not get firmware cap (%d)\n", err);
223 		return err;
224 	}
225 
226 	/* Put every capability in a new line */
227 	for (tmp = caps; *tmp; tmp++) {
228 		if (*tmp == ' ')
229 			*tmp = '\n';
230 	}
231 
232 	/* Usually there is a space at the end of capabilities string */
233 	seq_printf(seq, "%s", caps);
234 	/* So make sure we don't print two line breaks */
235 	if (tmp > caps && *(tmp - 1) != '\n')
236 		seq_printf(seq, "\n");
237 
238 	return 0;
239 }
240 
241 void brcmf_feat_attach(struct brcmf_pub *drvr)
242 {
243 	struct brcmf_if *ifp = brcmf_get_ifp(drvr, 0);
244 	struct brcmf_pno_macaddr_le pfn_mac;
245 	struct brcmf_gscan_config gscan_cfg;
246 	u32 wowl_cap;
247 	s32 err;
248 
249 	brcmf_feat_firmware_capabilities(ifp);
250 	memset(&gscan_cfg, 0, sizeof(gscan_cfg));
251 	if (drvr->bus_if->chip != BRCM_CC_43430_CHIP_ID &&
252 	    drvr->bus_if->chip != BRCM_CC_4345_CHIP_ID)
253 		brcmf_feat_iovar_data_set(ifp, BRCMF_FEAT_GSCAN,
254 					  "pfn_gscan_cfg",
255 					  &gscan_cfg, sizeof(gscan_cfg));
256 	brcmf_feat_iovar_int_get(ifp, BRCMF_FEAT_PNO, "pfn");
257 	if (drvr->bus_if->wowl_supported)
258 		brcmf_feat_iovar_int_get(ifp, BRCMF_FEAT_WOWL, "wowl");
259 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL)) {
260 		err = brcmf_fil_iovar_int_get(ifp, "wowl_cap", &wowl_cap);
261 		if (!err) {
262 			ifp->drvr->feat_flags |= BIT(BRCMF_FEAT_WOWL_ARP_ND);
263 			if (wowl_cap & BRCMF_WOWL_PFN_FOUND)
264 				ifp->drvr->feat_flags |=
265 					BIT(BRCMF_FEAT_WOWL_ND);
266 			if (wowl_cap & BRCMF_WOWL_GTK_FAILURE)
267 				ifp->drvr->feat_flags |=
268 					BIT(BRCMF_FEAT_WOWL_GTK);
269 		}
270 	}
271 	/* MBSS does not work for 43362 */
272 	if (drvr->bus_if->chip == BRCM_CC_43362_CHIP_ID)
273 		ifp->drvr->feat_flags &= ~BIT(BRCMF_FEAT_MBSS);
274 	brcmf_feat_iovar_int_get(ifp, BRCMF_FEAT_RSDB, "rsdb_mode");
275 	brcmf_feat_iovar_int_get(ifp, BRCMF_FEAT_TDLS, "tdls_enable");
276 	brcmf_feat_iovar_int_get(ifp, BRCMF_FEAT_MFP, "mfp");
277 
278 	pfn_mac.version = BRCMF_PFN_MACADDR_CFG_VER;
279 	err = brcmf_fil_iovar_data_get(ifp, "pfn_macaddr", &pfn_mac,
280 				       sizeof(pfn_mac));
281 	if (!err)
282 		ifp->drvr->feat_flags |= BIT(BRCMF_FEAT_SCAN_RANDOM_MAC);
283 
284 	if (drvr->settings->feature_disable) {
285 		brcmf_dbg(INFO, "Features: 0x%02x, disable: 0x%02x\n",
286 			  ifp->drvr->feat_flags,
287 			  drvr->settings->feature_disable);
288 		ifp->drvr->feat_flags &= ~drvr->settings->feature_disable;
289 	}
290 	brcmf_feat_iovar_int_get(ifp, BRCMF_FEAT_FWSUP, "sup_wpa");
291 
292 	brcmf_feat_firmware_overrides(drvr);
293 
294 	/* set chip related quirks */
295 	switch (drvr->bus_if->chip) {
296 	case BRCM_CC_43236_CHIP_ID:
297 		drvr->chip_quirks |= BIT(BRCMF_FEAT_QUIRK_AUTO_AUTH);
298 		break;
299 	case BRCM_CC_4329_CHIP_ID:
300 		drvr->chip_quirks |= BIT(BRCMF_FEAT_QUIRK_NEED_MPC);
301 		break;
302 	default:
303 		/* no quirks */
304 		break;
305 	}
306 }
307 
308 void brcmf_feat_debugfs_create(struct brcmf_pub *drvr)
309 {
310 	brcmf_debugfs_add_entry(drvr, "features", brcmf_feat_debugfs_read);
311 	brcmf_debugfs_add_entry(drvr, "fwcap", brcmf_feat_fwcap_debugfs_read);
312 }
313 
314 bool brcmf_feat_is_enabled(struct brcmf_if *ifp, enum brcmf_feat_id id)
315 {
316 	return (ifp->drvr->feat_flags & BIT(id));
317 }
318 
319 bool brcmf_feat_is_quirk_enabled(struct brcmf_if *ifp,
320 				 enum brcmf_feat_quirk quirk)
321 {
322 	return (ifp->drvr->chip_quirks & BIT(quirk));
323 }
324