Commit | Line | Data |
---|---|---|
5b435de0 AS |
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 | /******************************************************************************* | |
18 | * Communicates with the dongle by using dcmd codes. | |
19 | * For certain dcmd codes, the dongle interprets string data from the host. | |
20 | ******************************************************************************/ | |
21 | ||
22 | #include <linux/types.h> | |
23 | #include <linux/netdevice.h> | |
24 | #include <linux/sched.h> | |
25 | #include <defs.h> | |
26 | ||
27 | #include <brcmu_utils.h> | |
28 | #include <brcmu_wifi.h> | |
29 | ||
30 | #include "dhd.h" | |
31 | #include "dhd_proto.h" | |
32 | #include "dhd_bus.h" | |
33 | #include "dhd_dbg.h" | |
34 | ||
35 | struct brcmf_proto_cdc_dcmd { | |
36 | __le32 cmd; /* dongle command value */ | |
37 | __le32 len; /* lower 16: output buflen; | |
38 | * upper 16: input buflen (excludes header) */ | |
39 | __le32 flags; /* flag defns given below */ | |
40 | __le32 status; /* status code returned from the device */ | |
41 | }; | |
42 | ||
43 | /* Max valid buffer size that can be sent to the dongle */ | |
44 | #define CDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN) | |
45 | ||
46 | /* CDC flag definitions */ | |
47 | #define CDC_DCMD_ERROR 0x01 /* 1=cmd failed */ | |
48 | #define CDC_DCMD_SET 0x02 /* 0=get, 1=set cmd */ | |
49 | #define CDC_DCMD_IF_MASK 0xF000 /* I/F index */ | |
50 | #define CDC_DCMD_IF_SHIFT 12 | |
51 | #define CDC_DCMD_ID_MASK 0xFFFF0000 /* id an cmd pairing */ | |
52 | #define CDC_DCMD_ID_SHIFT 16 /* ID Mask shift bits */ | |
53 | #define CDC_DCMD_ID(flags) \ | |
54 | (((flags) & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT) | |
55 | ||
56 | /* | |
57 | * BDC header - Broadcom specific extension of CDC. | |
58 | * Used on data packets to convey priority across USB. | |
59 | */ | |
60 | #define BDC_HEADER_LEN 4 | |
e40aed06 | 61 | #define BDC_PROTO_VER 2 /* Protocol version */ |
5b435de0 AS |
62 | #define BDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */ |
63 | #define BDC_FLAG_VER_SHIFT 4 /* Protocol version shift */ | |
64 | #define BDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */ | |
65 | #define BDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */ | |
66 | #define BDC_PRIORITY_MASK 0x7 | |
67 | #define BDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */ | |
68 | #define BDC_FLAG2_IF_SHIFT 0 | |
69 | ||
70 | #define BDC_GET_IF_IDX(hdr) \ | |
71 | ((int)((((hdr)->flags2) & BDC_FLAG2_IF_MASK) >> BDC_FLAG2_IF_SHIFT)) | |
72 | #define BDC_SET_IF_IDX(hdr, idx) \ | |
73 | ((hdr)->flags2 = (((hdr)->flags2 & ~BDC_FLAG2_IF_MASK) | \ | |
74 | ((idx) << BDC_FLAG2_IF_SHIFT))) | |
75 | ||
76 | struct brcmf_proto_bdc_header { | |
77 | u8 flags; | |
78 | u8 priority; /* 802.1d Priority, 4:7 flow control info for usb */ | |
79 | u8 flags2; | |
e40aed06 | 80 | u8 data_offset; |
5b435de0 AS |
81 | }; |
82 | ||
83 | ||
84 | #define RETRIES 2 /* # of retries to retrieve matching dcmd response */ | |
85 | #define BUS_HEADER_LEN (16+BRCMF_SDALIGN) /* Must be atleast SDPCM_RESERVE | |
86 | * (amount of header tha might be added) | |
87 | * plus any space that might be needed | |
88 | * for alignment padding. | |
89 | */ | |
90 | #define ROUND_UP_MARGIN 2048 /* Biggest SDIO block size possible for | |
91 | * round off at the end of buffer | |
92 | */ | |
93 | ||
94 | struct brcmf_proto { | |
95 | u16 reqid; | |
96 | u8 pending; | |
97 | u32 lastcmd; | |
98 | u8 bus_header[BUS_HEADER_LEN]; | |
99 | struct brcmf_proto_cdc_dcmd msg; | |
100 | unsigned char buf[BRCMF_DCMD_MAXLEN + ROUND_UP_MARGIN]; | |
101 | }; | |
102 | ||
103 | static int brcmf_proto_cdc_msg(struct brcmf_pub *drvr) | |
104 | { | |
105 | struct brcmf_proto *prot = drvr->prot; | |
106 | int len = le32_to_cpu(prot->msg.len) + | |
107 | sizeof(struct brcmf_proto_cdc_dcmd); | |
108 | ||
109 | brcmf_dbg(TRACE, "Enter\n"); | |
110 | ||
111 | /* NOTE : cdc->msg.len holds the desired length of the buffer to be | |
112 | * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area | |
113 | * is actually sent to the dongle | |
114 | */ | |
115 | if (len > CDC_MAX_MSG_SIZE) | |
116 | len = CDC_MAX_MSG_SIZE; | |
117 | ||
118 | /* Send request */ | |
47a1ce78 | 119 | return brcmf_sdbrcm_bus_txctl(drvr->dev, (unsigned char *)&prot->msg, |
5b435de0 AS |
120 | len); |
121 | } | |
122 | ||
123 | static int brcmf_proto_cdc_cmplt(struct brcmf_pub *drvr, u32 id, u32 len) | |
124 | { | |
125 | int ret; | |
126 | struct brcmf_proto *prot = drvr->prot; | |
127 | ||
128 | brcmf_dbg(TRACE, "Enter\n"); | |
129 | ||
130 | do { | |
532cdd3b | 131 | ret = brcmf_sdbrcm_bus_rxctl(drvr->dev, |
5b435de0 AS |
132 | (unsigned char *)&prot->msg, |
133 | len + sizeof(struct brcmf_proto_cdc_dcmd)); | |
134 | if (ret < 0) | |
135 | break; | |
136 | } while (CDC_DCMD_ID(le32_to_cpu(prot->msg.flags)) != id); | |
137 | ||
138 | return ret; | |
139 | } | |
140 | ||
141 | int | |
142 | brcmf_proto_cdc_query_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd, | |
143 | void *buf, uint len) | |
144 | { | |
145 | struct brcmf_proto *prot = drvr->prot; | |
146 | struct brcmf_proto_cdc_dcmd *msg = &prot->msg; | |
147 | void *info; | |
148 | int ret = 0, retries = 0; | |
149 | u32 id, flags; | |
150 | ||
151 | brcmf_dbg(TRACE, "Enter\n"); | |
152 | brcmf_dbg(CTL, "cmd %d len %d\n", cmd, len); | |
153 | ||
154 | /* Respond "bcmerror" and "bcmerrorstr" with local cache */ | |
155 | if (cmd == BRCMF_C_GET_VAR && buf) { | |
156 | if (!strcmp((char *)buf, "bcmerrorstr")) { | |
157 | strncpy((char *)buf, "bcm_error", | |
158 | BCME_STRLEN); | |
159 | goto done; | |
160 | } else if (!strcmp((char *)buf, "bcmerror")) { | |
161 | *(int *)buf = drvr->dongle_error; | |
162 | goto done; | |
163 | } | |
164 | } | |
165 | ||
166 | memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd)); | |
167 | ||
168 | msg->cmd = cpu_to_le32(cmd); | |
169 | msg->len = cpu_to_le32(len); | |
170 | flags = (++prot->reqid << CDC_DCMD_ID_SHIFT); | |
171 | flags = (flags & ~CDC_DCMD_IF_MASK) | | |
172 | (ifidx << CDC_DCMD_IF_SHIFT); | |
173 | msg->flags = cpu_to_le32(flags); | |
174 | ||
175 | if (buf) | |
176 | memcpy(prot->buf, buf, len); | |
177 | ||
178 | ret = brcmf_proto_cdc_msg(drvr); | |
179 | if (ret < 0) { | |
180 | brcmf_dbg(ERROR, "brcmf_proto_cdc_msg failed w/status %d\n", | |
181 | ret); | |
182 | goto done; | |
183 | } | |
184 | ||
185 | retry: | |
186 | /* wait for interrupt and get first fragment */ | |
187 | ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len); | |
188 | if (ret < 0) | |
189 | goto done; | |
190 | ||
191 | flags = le32_to_cpu(msg->flags); | |
192 | id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT; | |
193 | ||
194 | if ((id < prot->reqid) && (++retries < RETRIES)) | |
195 | goto retry; | |
196 | if (id != prot->reqid) { | |
197 | brcmf_dbg(ERROR, "%s: unexpected request id %d (expected %d)\n", | |
198 | brcmf_ifname(drvr, ifidx), id, prot->reqid); | |
199 | ret = -EINVAL; | |
200 | goto done; | |
201 | } | |
202 | ||
203 | /* Check info buffer */ | |
204 | info = (void *)&msg[1]; | |
205 | ||
206 | /* Copy info buffer */ | |
207 | if (buf) { | |
208 | if (ret < (int)len) | |
209 | len = ret; | |
210 | memcpy(buf, info, len); | |
211 | } | |
212 | ||
213 | /* Check the ERROR flag */ | |
214 | if (flags & CDC_DCMD_ERROR) { | |
215 | ret = le32_to_cpu(msg->status); | |
216 | /* Cache error from dongle */ | |
217 | drvr->dongle_error = ret; | |
218 | } | |
219 | ||
220 | done: | |
221 | return ret; | |
222 | } | |
223 | ||
224 | int brcmf_proto_cdc_set_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd, | |
225 | void *buf, uint len) | |
226 | { | |
227 | struct brcmf_proto *prot = drvr->prot; | |
228 | struct brcmf_proto_cdc_dcmd *msg = &prot->msg; | |
229 | int ret = 0; | |
230 | u32 flags, id; | |
231 | ||
232 | brcmf_dbg(TRACE, "Enter\n"); | |
233 | brcmf_dbg(CTL, "cmd %d len %d\n", cmd, len); | |
234 | ||
235 | memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd)); | |
236 | ||
237 | msg->cmd = cpu_to_le32(cmd); | |
238 | msg->len = cpu_to_le32(len); | |
239 | flags = (++prot->reqid << CDC_DCMD_ID_SHIFT) | CDC_DCMD_SET; | |
240 | flags = (flags & ~CDC_DCMD_IF_MASK) | | |
241 | (ifidx << CDC_DCMD_IF_SHIFT); | |
242 | msg->flags = cpu_to_le32(flags); | |
243 | ||
244 | if (buf) | |
245 | memcpy(prot->buf, buf, len); | |
246 | ||
247 | ret = brcmf_proto_cdc_msg(drvr); | |
248 | if (ret < 0) | |
249 | goto done; | |
250 | ||
251 | ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len); | |
252 | if (ret < 0) | |
253 | goto done; | |
254 | ||
255 | flags = le32_to_cpu(msg->flags); | |
256 | id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT; | |
257 | ||
258 | if (id != prot->reqid) { | |
259 | brcmf_dbg(ERROR, "%s: unexpected request id %d (expected %d)\n", | |
260 | brcmf_ifname(drvr, ifidx), id, prot->reqid); | |
261 | ret = -EINVAL; | |
262 | goto done; | |
263 | } | |
264 | ||
265 | /* Check the ERROR flag */ | |
266 | if (flags & CDC_DCMD_ERROR) { | |
267 | ret = le32_to_cpu(msg->status); | |
268 | /* Cache error from dongle */ | |
269 | drvr->dongle_error = ret; | |
270 | } | |
271 | ||
272 | done: | |
273 | return ret; | |
274 | } | |
275 | ||
276 | int | |
277 | brcmf_proto_dcmd(struct brcmf_pub *drvr, int ifidx, struct brcmf_dcmd *dcmd, | |
278 | int len) | |
279 | { | |
280 | struct brcmf_proto *prot = drvr->prot; | |
281 | int ret = -1; | |
282 | ||
8d169aa0 | 283 | if (drvr->bus_if->state == BRCMF_BUS_DOWN) { |
5b435de0 AS |
284 | brcmf_dbg(ERROR, "bus is down. we have nothing to do.\n"); |
285 | return ret; | |
286 | } | |
1ae00421 | 287 | mutex_lock(&drvr->proto_block); |
5b435de0 AS |
288 | |
289 | brcmf_dbg(TRACE, "Enter\n"); | |
290 | ||
291 | if (len > BRCMF_DCMD_MAXLEN) | |
292 | goto done; | |
293 | ||
294 | if (prot->pending == true) { | |
295 | brcmf_dbg(TRACE, "CDC packet is pending!!!! cmd=0x%x (%lu) lastcmd=0x%x (%lu)\n", | |
296 | dcmd->cmd, (unsigned long)dcmd->cmd, prot->lastcmd, | |
297 | (unsigned long)prot->lastcmd); | |
298 | if (dcmd->cmd == BRCMF_C_SET_VAR || | |
299 | dcmd->cmd == BRCMF_C_GET_VAR) | |
300 | brcmf_dbg(TRACE, "iovar cmd=%s\n", (char *)dcmd->buf); | |
301 | ||
302 | goto done; | |
303 | } | |
304 | ||
305 | prot->pending = true; | |
306 | prot->lastcmd = dcmd->cmd; | |
307 | if (dcmd->set) | |
308 | ret = brcmf_proto_cdc_set_dcmd(drvr, ifidx, dcmd->cmd, | |
309 | dcmd->buf, len); | |
310 | else { | |
311 | ret = brcmf_proto_cdc_query_dcmd(drvr, ifidx, dcmd->cmd, | |
312 | dcmd->buf, len); | |
313 | if (ret > 0) | |
314 | dcmd->used = ret - | |
315 | sizeof(struct brcmf_proto_cdc_dcmd); | |
316 | } | |
317 | ||
318 | if (ret >= 0) | |
319 | ret = 0; | |
320 | else { | |
321 | struct brcmf_proto_cdc_dcmd *msg = &prot->msg; | |
322 | /* len == needed when set/query fails from dongle */ | |
323 | dcmd->needed = le32_to_cpu(msg->len); | |
324 | } | |
325 | ||
326 | /* Intercept the wme_dp dongle cmd here */ | |
327 | if (!ret && dcmd->cmd == BRCMF_C_SET_VAR && | |
328 | !strcmp(dcmd->buf, "wme_dp")) { | |
329 | int slen; | |
330 | __le32 val = 0; | |
331 | ||
332 | slen = strlen("wme_dp") + 1; | |
333 | if (len >= (int)(slen + sizeof(int))) | |
334 | memcpy(&val, (char *)dcmd->buf + slen, sizeof(int)); | |
335 | drvr->wme_dp = (u8) le32_to_cpu(val); | |
336 | } | |
337 | ||
338 | prot->pending = false; | |
339 | ||
340 | done: | |
1ae00421 | 341 | mutex_unlock(&drvr->proto_block); |
5b435de0 AS |
342 | |
343 | return ret; | |
344 | } | |
345 | ||
346 | static bool pkt_sum_needed(struct sk_buff *skb) | |
347 | { | |
348 | return skb->ip_summed == CHECKSUM_PARTIAL; | |
349 | } | |
350 | ||
351 | static void pkt_set_sum_good(struct sk_buff *skb, bool x) | |
352 | { | |
353 | skb->ip_summed = (x ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE); | |
354 | } | |
355 | ||
356 | void brcmf_proto_hdrpush(struct brcmf_pub *drvr, int ifidx, | |
357 | struct sk_buff *pktbuf) | |
358 | { | |
359 | struct brcmf_proto_bdc_header *h; | |
360 | ||
361 | brcmf_dbg(TRACE, "Enter\n"); | |
362 | ||
363 | /* Push BDC header used to convey priority for buses that don't */ | |
364 | ||
365 | skb_push(pktbuf, BDC_HEADER_LEN); | |
366 | ||
367 | h = (struct brcmf_proto_bdc_header *)(pktbuf->data); | |
368 | ||
369 | h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT); | |
370 | if (pkt_sum_needed(pktbuf)) | |
371 | h->flags |= BDC_FLAG_SUM_NEEDED; | |
372 | ||
373 | h->priority = (pktbuf->priority & BDC_PRIORITY_MASK); | |
374 | h->flags2 = 0; | |
e40aed06 | 375 | h->data_offset = 0; |
5b435de0 AS |
376 | BDC_SET_IF_IDX(h, ifidx); |
377 | } | |
378 | ||
379 | int brcmf_proto_hdrpull(struct brcmf_pub *drvr, int *ifidx, | |
380 | struct sk_buff *pktbuf) | |
381 | { | |
382 | struct brcmf_proto_bdc_header *h; | |
383 | ||
384 | brcmf_dbg(TRACE, "Enter\n"); | |
385 | ||
386 | /* Pop BDC header used to convey priority for buses that don't */ | |
387 | ||
388 | if (pktbuf->len < BDC_HEADER_LEN) { | |
389 | brcmf_dbg(ERROR, "rx data too short (%d < %d)\n", | |
390 | pktbuf->len, BDC_HEADER_LEN); | |
391 | return -EBADE; | |
392 | } | |
393 | ||
394 | h = (struct brcmf_proto_bdc_header *)(pktbuf->data); | |
395 | ||
396 | *ifidx = BDC_GET_IF_IDX(h); | |
397 | if (*ifidx >= BRCMF_MAX_IFS) { | |
398 | brcmf_dbg(ERROR, "rx data ifnum out of range (%d)\n", *ifidx); | |
399 | return -EBADE; | |
400 | } | |
401 | ||
402 | if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) != | |
403 | BDC_PROTO_VER) { | |
404 | brcmf_dbg(ERROR, "%s: non-BDC packet received, flags 0x%x\n", | |
405 | brcmf_ifname(drvr, *ifidx), h->flags); | |
406 | return -EBADE; | |
407 | } | |
408 | ||
409 | if (h->flags & BDC_FLAG_SUM_GOOD) { | |
410 | brcmf_dbg(INFO, "%s: BDC packet received with good rx-csum, flags 0x%x\n", | |
411 | brcmf_ifname(drvr, *ifidx), h->flags); | |
412 | pkt_set_sum_good(pktbuf, true); | |
413 | } | |
414 | ||
415 | pktbuf->priority = h->priority & BDC_PRIORITY_MASK; | |
416 | ||
417 | skb_pull(pktbuf, BDC_HEADER_LEN); | |
418 | ||
419 | return 0; | |
420 | } | |
421 | ||
422 | int brcmf_proto_attach(struct brcmf_pub *drvr) | |
423 | { | |
424 | struct brcmf_proto *cdc; | |
425 | ||
426 | cdc = kzalloc(sizeof(struct brcmf_proto), GFP_ATOMIC); | |
427 | if (!cdc) | |
428 | goto fail; | |
429 | ||
430 | /* ensure that the msg buf directly follows the cdc msg struct */ | |
431 | if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) { | |
432 | brcmf_dbg(ERROR, "struct brcmf_proto is not correctly defined\n"); | |
433 | goto fail; | |
434 | } | |
435 | ||
436 | drvr->prot = cdc; | |
437 | drvr->hdrlen += BDC_HEADER_LEN; | |
438 | drvr->maxctl = BRCMF_DCMD_MAXLEN + | |
439 | sizeof(struct brcmf_proto_cdc_dcmd) + ROUND_UP_MARGIN; | |
440 | return 0; | |
441 | ||
442 | fail: | |
443 | kfree(cdc); | |
444 | return -ENOMEM; | |
445 | } | |
446 | ||
447 | /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */ | |
448 | void brcmf_proto_detach(struct brcmf_pub *drvr) | |
449 | { | |
450 | kfree(drvr->prot); | |
451 | drvr->prot = NULL; | |
452 | } | |
453 | ||
454 | void brcmf_proto_dstats(struct brcmf_pub *drvr) | |
455 | { | |
456 | /* No stats from dongle added yet, copy bus stats */ | |
457 | drvr->dstats.tx_packets = drvr->tx_packets; | |
458 | drvr->dstats.tx_errors = drvr->tx_errors; | |
459 | drvr->dstats.rx_packets = drvr->rx_packets; | |
460 | drvr->dstats.rx_errors = drvr->rx_errors; | |
461 | drvr->dstats.rx_dropped = drvr->rx_dropped; | |
462 | drvr->dstats.multicast = drvr->rx_multicast; | |
463 | return; | |
464 | } | |
465 | ||
466 | int brcmf_proto_init(struct brcmf_pub *drvr) | |
467 | { | |
468 | int ret = 0; | |
469 | char buf[128]; | |
470 | ||
471 | brcmf_dbg(TRACE, "Enter\n"); | |
472 | ||
1ae00421 | 473 | mutex_lock(&drvr->proto_block); |
5b435de0 AS |
474 | |
475 | /* Get the device MAC address */ | |
476 | strcpy(buf, "cur_etheraddr"); | |
477 | ret = brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR, | |
478 | buf, sizeof(buf)); | |
479 | if (ret < 0) { | |
1ae00421 | 480 | mutex_unlock(&drvr->proto_block); |
5b435de0 AS |
481 | return ret; |
482 | } | |
483 | memcpy(drvr->mac, buf, ETH_ALEN); | |
484 | ||
1ae00421 | 485 | mutex_unlock(&drvr->proto_block); |
5b435de0 AS |
486 | |
487 | ret = brcmf_c_preinit_dcmds(drvr); | |
488 | ||
489 | /* Always assumes wl for now */ | |
490 | drvr->iswl = true; | |
491 | ||
492 | return ret; | |
493 | } | |
494 | ||
495 | void brcmf_proto_stop(struct brcmf_pub *drvr) | |
496 | { | |
497 | /* Nothing to do for CDC */ | |
498 | } |