1 /*
2 * Copyright (c) 2010 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/kernel.h>
18 #include <linux/string.h>
19 #include <linux/netdevice.h>
20 #include <brcmu_wifi.h>
21 #include <brcmu_utils.h>
22 #include "dhd.h"
23 #include "dhd_bus.h"
24 #include "dhd_dbg.h"
25 #include "fwil.h"
26 #include "fwil_types.h"
27 #include "tracepoint.h"
28
29 #define PKTFILTER_BUF_SIZE 128
30 #define BRCMF_DEFAULT_BCN_TIMEOUT 3
31 #define BRCMF_DEFAULT_SCAN_CHANNEL_TIME 40
32 #define BRCMF_DEFAULT_SCAN_UNASSOC_TIME 40
33 #define BRCMF_DEFAULT_PACKET_FILTER "100 0 0 0 0x01 0x00"
34
35 /* boost value for RSSI_DELTA in preferred join selection */
36 #define BRCMF_JOIN_PREF_RSSI_BOOST 8
37
38
brcmf_c_prec_enq(struct device * dev,struct pktq * q,struct sk_buff * pkt,int prec)39 bool brcmf_c_prec_enq(struct device *dev, struct pktq *q,
40 struct sk_buff *pkt, int prec)
41 {
42 struct sk_buff *p;
43 int eprec = -1; /* precedence to evict from */
44 bool discard_oldest;
45 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
46 struct brcmf_pub *drvr = bus_if->drvr;
47
48 /* Fast case, precedence queue is not full and we are also not
49 * exceeding total queue length
50 */
51 if (!pktq_pfull(q, prec) && !pktq_full(q)) {
52 brcmu_pktq_penq(q, prec, pkt);
53 return true;
54 }
55
56 /* Determine precedence from which to evict packet, if any */
57 if (pktq_pfull(q, prec))
58 eprec = prec;
59 else if (pktq_full(q)) {
60 p = brcmu_pktq_peek_tail(q, &eprec);
61 if (eprec > prec)
62 return false;
63 }
64
65 /* Evict if needed */
66 if (eprec >= 0) {
67 /* Detect queueing to unconfigured precedence */
68 discard_oldest = ac_bitmap_tst(drvr->wme_dp, eprec);
69 if (eprec == prec && !discard_oldest)
70 return false; /* refuse newer (incoming) packet */
71 /* Evict packet according to discard policy */
72 p = discard_oldest ? brcmu_pktq_pdeq(q, eprec) :
73 brcmu_pktq_pdeq_tail(q, eprec);
74 if (p == NULL)
75 brcmf_err("brcmu_pktq_penq() failed, oldest %d\n",
76 discard_oldest);
77
78 brcmu_pkt_buf_free_skb(p);
79 }
80
81 /* Enqueue */
82 p = brcmu_pktq_penq(q, prec, pkt);
83 if (p == NULL)
84 brcmf_err("brcmu_pktq_penq() failed\n");
85
86 return p != NULL;
87 }
88
89 /* Convert user's input in hex pattern to byte-size mask */
brcmf_c_pattern_atoh(char * src,char * dst)90 static int brcmf_c_pattern_atoh(char *src, char *dst)
91 {
92 int i;
93 if (strncmp(src, "0x", 2) != 0 && strncmp(src, "0X", 2) != 0) {
94 brcmf_err("Mask invalid format. Needs to start with 0x\n");
95 return -EINVAL;
96 }
97 src = src + 2; /* Skip past 0x */
98 if (strlen(src) % 2 != 0) {
99 brcmf_err("Mask invalid format. Length must be even.\n");
100 return -EINVAL;
101 }
102 for (i = 0; *src != '\0'; i++) {
103 unsigned long res;
104 char num[3];
105 strncpy(num, src, 2);
106 num[2] = '\0';
107 if (kstrtoul(num, 16, &res))
108 return -EINVAL;
109 dst[i] = (u8)res;
110 src += 2;
111 }
112 return i;
113 }
114
115 static void
brcmf_c_pktfilter_offload_enable(struct brcmf_if * ifp,char * arg,int enable,int master_mode)116 brcmf_c_pktfilter_offload_enable(struct brcmf_if *ifp, char *arg, int enable,
117 int master_mode)
118 {
119 unsigned long res;
120 char *argv;
121 char *arg_save = NULL, *arg_org = NULL;
122 s32 err;
123 struct brcmf_pkt_filter_enable_le enable_parm;
124
125 arg_save = kstrdup(arg, GFP_ATOMIC);
126 if (!arg_save)
127 goto fail;
128
129 arg_org = arg_save;
130
131 argv = strsep(&arg_save, " ");
132
133 if (argv == NULL) {
134 brcmf_err("No args provided\n");
135 goto fail;
136 }
137
138 /* Parse packet filter id. */
139 enable_parm.id = 0;
140 if (!kstrtoul(argv, 0, &res))
141 enable_parm.id = cpu_to_le32((u32)res);
142
143 /* Enable/disable the specified filter. */
144 enable_parm.enable = cpu_to_le32(enable);
145
146 err = brcmf_fil_iovar_data_set(ifp, "pkt_filter_enable", &enable_parm,
147 sizeof(enable_parm));
148 if (err)
149 brcmf_err("Set pkt_filter_enable error (%d)\n", err);
150
151 /* Control the master mode */
152 err = brcmf_fil_iovar_int_set(ifp, "pkt_filter_mode", master_mode);
153 if (err)
154 brcmf_err("Set pkt_filter_mode error (%d)\n", err);
155
156 fail:
157 kfree(arg_org);
158 }
159
brcmf_c_pktfilter_offload_set(struct brcmf_if * ifp,char * arg)160 static void brcmf_c_pktfilter_offload_set(struct brcmf_if *ifp, char *arg)
161 {
162 struct brcmf_pkt_filter_le *pkt_filter;
163 unsigned long res;
164 int buf_len;
165 s32 err;
166 u32 mask_size;
167 u32 pattern_size;
168 char *argv[8], *buf = NULL;
169 int i = 0;
170 char *arg_save = NULL, *arg_org = NULL;
171
172 arg_save = kstrdup(arg, GFP_ATOMIC);
173 if (!arg_save)
174 goto fail;
175
176 arg_org = arg_save;
177
178 buf = kmalloc(PKTFILTER_BUF_SIZE, GFP_ATOMIC);
179 if (!buf)
180 goto fail;
181
182 argv[i] = strsep(&arg_save, " ");
183 while (argv[i]) {
184 i++;
185 if (i >= 8) {
186 brcmf_err("Too many parameters\n");
187 goto fail;
188 }
189 argv[i] = strsep(&arg_save, " ");
190 }
191
192 if (i != 6) {
193 brcmf_err("Not enough args provided %d\n", i);
194 goto fail;
195 }
196
197 pkt_filter = (struct brcmf_pkt_filter_le *)buf;
198
199 /* Parse packet filter id. */
200 pkt_filter->id = 0;
201 if (!kstrtoul(argv[0], 0, &res))
202 pkt_filter->id = cpu_to_le32((u32)res);
203
204 /* Parse filter polarity. */
205 pkt_filter->negate_match = 0;
206 if (!kstrtoul(argv[1], 0, &res))
207 pkt_filter->negate_match = cpu_to_le32((u32)res);
208
209 /* Parse filter type. */
210 pkt_filter->type = 0;
211 if (!kstrtoul(argv[2], 0, &res))
212 pkt_filter->type = cpu_to_le32((u32)res);
213
214 /* Parse pattern filter offset. */
215 pkt_filter->u.pattern.offset = 0;
216 if (!kstrtoul(argv[3], 0, &res))
217 pkt_filter->u.pattern.offset = cpu_to_le32((u32)res);
218
219 /* Parse pattern filter mask. */
220 mask_size = brcmf_c_pattern_atoh(argv[4],
221 (char *)pkt_filter->u.pattern.mask_and_pattern);
222
223 /* Parse pattern filter pattern. */
224 pattern_size = brcmf_c_pattern_atoh(argv[5],
225 (char *)&pkt_filter->u.pattern.mask_and_pattern[mask_size]);
226
227 if (mask_size != pattern_size) {
228 brcmf_err("Mask and pattern not the same size\n");
229 goto fail;
230 }
231
232 pkt_filter->u.pattern.size_bytes = cpu_to_le32(mask_size);
233 buf_len = offsetof(struct brcmf_pkt_filter_le,
234 u.pattern.mask_and_pattern);
235 buf_len += mask_size + pattern_size;
236
237 err = brcmf_fil_iovar_data_set(ifp, "pkt_filter_add", pkt_filter,
238 buf_len);
239 if (err)
240 brcmf_err("Set pkt_filter_add error (%d)\n", err);
241
242 fail:
243 kfree(arg_org);
244
245 kfree(buf);
246 }
247
brcmf_c_preinit_dcmds(struct brcmf_if * ifp)248 int brcmf_c_preinit_dcmds(struct brcmf_if *ifp)
249 {
250 s8 eventmask[BRCMF_EVENTING_MASK_LEN];
251 u8 buf[BRCMF_DCMD_SMLEN];
252 struct brcmf_join_pref_params join_pref_params[2];
253 char *ptr;
254 s32 err;
255
256 /* retreive mac address */
257 err = brcmf_fil_iovar_data_get(ifp, "cur_etheraddr", ifp->mac_addr,
258 sizeof(ifp->mac_addr));
259 if (err < 0) {
260 brcmf_err("Retreiving cur_etheraddr failed, %d\n",
261 err);
262 goto done;
263 }
264 memcpy(ifp->drvr->mac, ifp->mac_addr, sizeof(ifp->drvr->mac));
265
266 /* query for 'ver' to get version info from firmware */
267 memset(buf, 0, sizeof(buf));
268 strcpy(buf, "ver");
269 err = brcmf_fil_iovar_data_get(ifp, "ver", buf, sizeof(buf));
270 if (err < 0) {
271 brcmf_err("Retreiving version information failed, %d\n",
272 err);
273 goto done;
274 }
275 ptr = (char *)buf;
276 strsep(&ptr, "\n");
277
278 /* Print fw version info */
279 brcmf_err("Firmware version = %s\n", buf);
280
281 /* locate firmware version number for ethtool */
282 ptr = strrchr(buf, ' ') + 1;
283 strlcpy(ifp->drvr->fwver, ptr, sizeof(ifp->drvr->fwver));
284
285 /* set mpc */
286 err = brcmf_fil_iovar_int_set(ifp, "mpc", 1);
287 if (err) {
288 brcmf_err("failed setting mpc\n");
289 goto done;
290 }
291
292 /*
293 * Setup timeout if Beacons are lost and roam is off to report
294 * link down
295 */
296 err = brcmf_fil_iovar_int_set(ifp, "bcn_timeout",
297 BRCMF_DEFAULT_BCN_TIMEOUT);
298 if (err) {
299 brcmf_err("bcn_timeout error (%d)\n", err);
300 goto done;
301 }
302
303 /* Enable/Disable build-in roaming to allowed ext supplicant to take
304 * of romaing
305 */
306 err = brcmf_fil_iovar_int_set(ifp, "roam_off", 1);
307 if (err) {
308 brcmf_err("roam_off error (%d)\n", err);
309 goto done;
310 }
311
312 /* Setup join_pref to select target by RSSI(with boost on 5GHz) */
313 join_pref_params[0].type = BRCMF_JOIN_PREF_RSSI_DELTA;
314 join_pref_params[0].len = 2;
315 join_pref_params[0].rssi_gain = BRCMF_JOIN_PREF_RSSI_BOOST;
316 join_pref_params[0].band = WLC_BAND_5G;
317 join_pref_params[1].type = BRCMF_JOIN_PREF_RSSI;
318 join_pref_params[1].len = 2;
319 join_pref_params[1].rssi_gain = 0;
320 join_pref_params[1].band = 0;
321 err = brcmf_fil_iovar_data_set(ifp, "join_pref", join_pref_params,
322 sizeof(join_pref_params));
323 if (err)
324 brcmf_err("Set join_pref error (%d)\n", err);
325
326 /* Setup event_msgs, enable E_IF */
327 err = brcmf_fil_iovar_data_get(ifp, "event_msgs", eventmask,
328 BRCMF_EVENTING_MASK_LEN);
329 if (err) {
330 brcmf_err("Get event_msgs error (%d)\n", err);
331 goto done;
332 }
333 setbit(eventmask, BRCMF_E_IF);
334 err = brcmf_fil_iovar_data_set(ifp, "event_msgs", eventmask,
335 BRCMF_EVENTING_MASK_LEN);
336 if (err) {
337 brcmf_err("Set event_msgs error (%d)\n", err);
338 goto done;
339 }
340
341 /* Setup default scan channel time */
342 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
343 BRCMF_DEFAULT_SCAN_CHANNEL_TIME);
344 if (err) {
345 brcmf_err("BRCMF_C_SET_SCAN_CHANNEL_TIME error (%d)\n",
346 err);
347 goto done;
348 }
349
350 /* Setup default scan unassoc time */
351 err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
352 BRCMF_DEFAULT_SCAN_UNASSOC_TIME);
353 if (err) {
354 brcmf_err("BRCMF_C_SET_SCAN_UNASSOC_TIME error (%d)\n",
355 err);
356 goto done;
357 }
358
359 /* Setup packet filter */
360 brcmf_c_pktfilter_offload_set(ifp, BRCMF_DEFAULT_PACKET_FILTER);
361 brcmf_c_pktfilter_offload_enable(ifp, BRCMF_DEFAULT_PACKET_FILTER,
362 0, true);
363
364 /* do bus specific preinit here */
365 err = brcmf_bus_preinit(ifp->drvr->bus_if);
366 done:
367 return err;
368 }
369
370 #ifdef CONFIG_BRCM_TRACING
__brcmf_err(const char * func,const char * fmt,...)371 void __brcmf_err(const char *func, const char *fmt, ...)
372 {
373 struct va_format vaf = {
374 .fmt = fmt,
375 };
376 va_list args;
377
378 va_start(args, fmt);
379 vaf.va = &args;
380 pr_err("%s: %pV", func, &vaf);
381 trace_brcmf_err(func, &vaf);
382 va_end(args);
383 }
384 #endif
385 #if defined(CONFIG_BRCM_TRACING) || defined(CONFIG_BRCMDBG)
__brcmf_dbg(u32 level,const char * func,const char * fmt,...)386 void __brcmf_dbg(u32 level, const char *func, const char *fmt, ...)
387 {
388 struct va_format vaf = {
389 .fmt = fmt,
390 };
391 va_list args;
392
393 va_start(args, fmt);
394 vaf.va = &args;
395 if (brcmf_msg_level & level)
396 pr_debug("%s %pV", func, &vaf);
397 trace_brcmf_dbg(level, func, &vaf);
398 va_end(args);
399 }
400 #endif
401