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