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