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