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