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> | |
5b435de0 AS |
24 | |
25 | #include <brcmu_utils.h> | |
26 | #include <brcmu_wifi.h> | |
27 | ||
28 | #include "dhd.h" | |
5b435de0 | 29 | #include "dhd_bus.h" |
349e7104 | 30 | #include "fwsignal.h" |
5b435de0 | 31 | #include "dhd_dbg.h" |
ea0737d6 | 32 | #include "tracepoint.h" |
85b84133 HM |
33 | #include "proto.h" |
34 | #include "bcdc.h" | |
5b435de0 AS |
35 | |
36 | struct brcmf_proto_cdc_dcmd { | |
37 | __le32 cmd; /* dongle command value */ | |
38 | __le32 len; /* lower 16: output buflen; | |
39 | * upper 16: input buflen (excludes header) */ | |
40 | __le32 flags; /* flag defns given below */ | |
41 | __le32 status; /* status code returned from the device */ | |
42 | }; | |
43 | ||
44 | /* Max valid buffer size that can be sent to the dongle */ | |
45 | #define CDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN) | |
46 | ||
47 | /* CDC flag definitions */ | |
48 | #define CDC_DCMD_ERROR 0x01 /* 1=cmd failed */ | |
49 | #define CDC_DCMD_SET 0x02 /* 0=get, 1=set cmd */ | |
50 | #define CDC_DCMD_IF_MASK 0xF000 /* I/F index */ | |
51 | #define CDC_DCMD_IF_SHIFT 12 | |
52 | #define CDC_DCMD_ID_MASK 0xFFFF0000 /* id an cmd pairing */ | |
53 | #define CDC_DCMD_ID_SHIFT 16 /* ID Mask shift bits */ | |
54 | #define CDC_DCMD_ID(flags) \ | |
55 | (((flags) & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT) | |
56 | ||
57 | /* | |
58 | * BDC header - Broadcom specific extension of CDC. | |
59 | * Used on data packets to convey priority across USB. | |
60 | */ | |
61 | #define BDC_HEADER_LEN 4 | |
e40aed06 | 62 | #define BDC_PROTO_VER 2 /* Protocol version */ |
5b435de0 AS |
63 | #define BDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */ |
64 | #define BDC_FLAG_VER_SHIFT 4 /* Protocol version shift */ | |
65 | #define BDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */ | |
66 | #define BDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */ | |
67 | #define BDC_PRIORITY_MASK 0x7 | |
68 | #define BDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */ | |
69 | #define BDC_FLAG2_IF_SHIFT 0 | |
70 | ||
71 | #define BDC_GET_IF_IDX(hdr) \ | |
72 | ((int)((((hdr)->flags2) & BDC_FLAG2_IF_MASK) >> BDC_FLAG2_IF_SHIFT)) | |
73 | #define BDC_SET_IF_IDX(hdr, idx) \ | |
74 | ((hdr)->flags2 = (((hdr)->flags2 & ~BDC_FLAG2_IF_MASK) | \ | |
75 | ((idx) << BDC_FLAG2_IF_SHIFT))) | |
76 | ||
bee1b848 AS |
77 | /** |
78 | * struct brcmf_proto_bdc_header - BDC header format | |
79 | * | |
80 | * @flags: flags contain protocol and checksum info. | |
81 | * @priority: 802.1d priority and USB flow control info (bit 4:7). | |
82 | * @flags2: additional flags containing dongle interface index. | |
83 | * @data_offset: start of packet data. header is following by firmware signals. | |
84 | */ | |
5b435de0 AS |
85 | struct brcmf_proto_bdc_header { |
86 | u8 flags; | |
bee1b848 | 87 | u8 priority; |
5b435de0 | 88 | u8 flags2; |
e40aed06 | 89 | u8 data_offset; |
5b435de0 AS |
90 | }; |
91 | ||
bee1b848 AS |
92 | /* |
93 | * maximum length of firmware signal data between | |
94 | * the BDC header and packet data in the tx path. | |
95 | */ | |
96 | #define BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES 12 | |
5b435de0 AS |
97 | |
98 | #define RETRIES 2 /* # of retries to retrieve matching dcmd response */ | |
6e3c7128 | 99 | #define BUS_HEADER_LEN (16+64) /* Must be atleast SDPCM_RESERVE |
5b435de0 AS |
100 | * (amount of header tha might be added) |
101 | * plus any space that might be needed | |
6e3c7128 | 102 | * for bus alignment padding. |
5b435de0 | 103 | */ |
6e3c7128 | 104 | #define ROUND_UP_MARGIN 2048 /* Biggest bus block size possible for |
5b435de0 | 105 | * round off at the end of buffer |
6e3c7128 | 106 | * Currently is SDIO |
5b435de0 AS |
107 | */ |
108 | ||
85b84133 | 109 | struct brcmf_bcdc { |
5b435de0 | 110 | u16 reqid; |
5b435de0 AS |
111 | u8 bus_header[BUS_HEADER_LEN]; |
112 | struct brcmf_proto_cdc_dcmd msg; | |
113 | unsigned char buf[BRCMF_DCMD_MAXLEN + ROUND_UP_MARGIN]; | |
114 | }; | |
115 | ||
116 | static int brcmf_proto_cdc_msg(struct brcmf_pub *drvr) | |
117 | { | |
85b84133 HM |
118 | struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd; |
119 | int len = le32_to_cpu(bcdc->msg.len) + | |
5b435de0 AS |
120 | sizeof(struct brcmf_proto_cdc_dcmd); |
121 | ||
a6cfb147 | 122 | brcmf_dbg(CDC, "Enter\n"); |
5b435de0 AS |
123 | |
124 | /* NOTE : cdc->msg.len holds the desired length of the buffer to be | |
125 | * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area | |
126 | * is actually sent to the dongle | |
127 | */ | |
128 | if (len > CDC_MAX_MSG_SIZE) | |
129 | len = CDC_MAX_MSG_SIZE; | |
130 | ||
131 | /* Send request */ | |
85b84133 | 132 | return brcmf_bus_txctl(drvr->bus_if, (unsigned char *)&bcdc->msg, len); |
5b435de0 AS |
133 | } |
134 | ||
135 | static int brcmf_proto_cdc_cmplt(struct brcmf_pub *drvr, u32 id, u32 len) | |
136 | { | |
137 | int ret; | |
85b84133 | 138 | struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd; |
5b435de0 | 139 | |
a6cfb147 | 140 | brcmf_dbg(CDC, "Enter\n"); |
d9cb2596 | 141 | len += sizeof(struct brcmf_proto_cdc_dcmd); |
5b435de0 | 142 | do { |
85b84133 | 143 | ret = brcmf_bus_rxctl(drvr->bus_if, (unsigned char *)&bcdc->msg, |
d9cb2596 | 144 | len); |
5b435de0 AS |
145 | if (ret < 0) |
146 | break; | |
85b84133 | 147 | } while (CDC_DCMD_ID(le32_to_cpu(bcdc->msg.flags)) != id); |
5b435de0 AS |
148 | |
149 | return ret; | |
150 | } | |
151 | ||
85b84133 HM |
152 | static int |
153 | brcmf_proto_bcdc_query_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd, | |
154 | void *buf, uint len) | |
5b435de0 | 155 | { |
85b84133 HM |
156 | struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd; |
157 | struct brcmf_proto_cdc_dcmd *msg = &bcdc->msg; | |
5b435de0 AS |
158 | void *info; |
159 | int ret = 0, retries = 0; | |
160 | u32 id, flags; | |
161 | ||
a6cfb147 | 162 | brcmf_dbg(CDC, "Enter, cmd %d len %d\n", cmd, len); |
5b435de0 | 163 | |
5b435de0 AS |
164 | memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd)); |
165 | ||
166 | msg->cmd = cpu_to_le32(cmd); | |
167 | msg->len = cpu_to_le32(len); | |
85b84133 | 168 | flags = (++bcdc->reqid << CDC_DCMD_ID_SHIFT); |
5b435de0 AS |
169 | flags = (flags & ~CDC_DCMD_IF_MASK) | |
170 | (ifidx << CDC_DCMD_IF_SHIFT); | |
171 | msg->flags = cpu_to_le32(flags); | |
172 | ||
173 | if (buf) | |
85b84133 | 174 | memcpy(bcdc->buf, buf, len); |
5b435de0 AS |
175 | |
176 | ret = brcmf_proto_cdc_msg(drvr); | |
177 | if (ret < 0) { | |
5e8149f5 | 178 | brcmf_err("brcmf_proto_cdc_msg failed w/status %d\n", |
5b435de0 AS |
179 | ret); |
180 | goto done; | |
181 | } | |
182 | ||
183 | retry: | |
184 | /* wait for interrupt and get first fragment */ | |
85b84133 | 185 | ret = brcmf_proto_cdc_cmplt(drvr, bcdc->reqid, len); |
5b435de0 AS |
186 | if (ret < 0) |
187 | goto done; | |
188 | ||
189 | flags = le32_to_cpu(msg->flags); | |
190 | id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT; | |
191 | ||
85b84133 | 192 | if ((id < bcdc->reqid) && (++retries < RETRIES)) |
5b435de0 | 193 | goto retry; |
85b84133 | 194 | if (id != bcdc->reqid) { |
5e8149f5 | 195 | brcmf_err("%s: unexpected request id %d (expected %d)\n", |
85b84133 | 196 | brcmf_ifname(drvr, ifidx), id, bcdc->reqid); |
5b435de0 AS |
197 | ret = -EINVAL; |
198 | goto done; | |
199 | } | |
200 | ||
201 | /* Check info buffer */ | |
202 | info = (void *)&msg[1]; | |
203 | ||
204 | /* Copy info buffer */ | |
205 | if (buf) { | |
206 | if (ret < (int)len) | |
207 | len = ret; | |
208 | memcpy(buf, info, len); | |
209 | } | |
210 | ||
211 | /* Check the ERROR flag */ | |
03abad08 | 212 | if (flags & CDC_DCMD_ERROR) |
5b435de0 | 213 | ret = le32_to_cpu(msg->status); |
5b435de0 AS |
214 | |
215 | done: | |
216 | return ret; | |
217 | } | |
218 | ||
85b84133 HM |
219 | static int |
220 | brcmf_proto_bcdc_set_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd, | |
221 | void *buf, uint len) | |
5b435de0 | 222 | { |
85b84133 HM |
223 | struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd; |
224 | struct brcmf_proto_cdc_dcmd *msg = &bcdc->msg; | |
5b435de0 AS |
225 | int ret = 0; |
226 | u32 flags, id; | |
227 | ||
a6cfb147 | 228 | brcmf_dbg(CDC, "Enter, cmd %d len %d\n", cmd, len); |
5b435de0 AS |
229 | |
230 | memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd)); | |
231 | ||
232 | msg->cmd = cpu_to_le32(cmd); | |
233 | msg->len = cpu_to_le32(len); | |
85b84133 | 234 | flags = (++bcdc->reqid << CDC_DCMD_ID_SHIFT) | CDC_DCMD_SET; |
5b435de0 AS |
235 | flags = (flags & ~CDC_DCMD_IF_MASK) | |
236 | (ifidx << CDC_DCMD_IF_SHIFT); | |
237 | msg->flags = cpu_to_le32(flags); | |
238 | ||
239 | if (buf) | |
85b84133 | 240 | memcpy(bcdc->buf, buf, len); |
5b435de0 AS |
241 | |
242 | ret = brcmf_proto_cdc_msg(drvr); | |
243 | if (ret < 0) | |
244 | goto done; | |
245 | ||
85b84133 | 246 | ret = brcmf_proto_cdc_cmplt(drvr, bcdc->reqid, len); |
5b435de0 AS |
247 | if (ret < 0) |
248 | goto done; | |
249 | ||
250 | flags = le32_to_cpu(msg->flags); | |
251 | id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT; | |
252 | ||
85b84133 | 253 | if (id != bcdc->reqid) { |
5e8149f5 | 254 | brcmf_err("%s: unexpected request id %d (expected %d)\n", |
85b84133 | 255 | brcmf_ifname(drvr, ifidx), id, bcdc->reqid); |
5b435de0 AS |
256 | ret = -EINVAL; |
257 | goto done; | |
258 | } | |
259 | ||
260 | /* Check the ERROR flag */ | |
03abad08 | 261 | if (flags & CDC_DCMD_ERROR) |
5b435de0 | 262 | ret = le32_to_cpu(msg->status); |
5b435de0 AS |
263 | |
264 | done: | |
265 | return ret; | |
266 | } | |
267 | ||
5b435de0 AS |
268 | static bool pkt_sum_needed(struct sk_buff *skb) |
269 | { | |
270 | return skb->ip_summed == CHECKSUM_PARTIAL; | |
271 | } | |
272 | ||
273 | static void pkt_set_sum_good(struct sk_buff *skb, bool x) | |
274 | { | |
275 | skb->ip_summed = (x ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE); | |
276 | } | |
277 | ||
85b84133 HM |
278 | static void |
279 | brcmf_proto_bcdc_hdrpush(struct brcmf_pub *drvr, int ifidx, u8 offset, | |
5b435de0 AS |
280 | struct sk_buff *pktbuf) |
281 | { | |
282 | struct brcmf_proto_bdc_header *h; | |
283 | ||
a6cfb147 | 284 | brcmf_dbg(CDC, "Enter\n"); |
5b435de0 AS |
285 | |
286 | /* Push BDC header used to convey priority for buses that don't */ | |
5b435de0 AS |
287 | skb_push(pktbuf, BDC_HEADER_LEN); |
288 | ||
289 | h = (struct brcmf_proto_bdc_header *)(pktbuf->data); | |
290 | ||
291 | h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT); | |
292 | if (pkt_sum_needed(pktbuf)) | |
293 | h->flags |= BDC_FLAG_SUM_NEEDED; | |
294 | ||
295 | h->priority = (pktbuf->priority & BDC_PRIORITY_MASK); | |
296 | h->flags2 = 0; | |
8f0c3b6d | 297 | h->data_offset = offset; |
5b435de0 | 298 | BDC_SET_IF_IDX(h, ifidx); |
ea0737d6 | 299 | trace_brcmf_bdchdr(pktbuf->data); |
5b435de0 AS |
300 | } |
301 | ||
85b84133 HM |
302 | static int |
303 | brcmf_proto_bcdc_hdrpull(struct brcmf_pub *drvr, bool do_fws, u8 *ifidx, | |
304 | struct sk_buff *pktbuf) | |
5b435de0 AS |
305 | { |
306 | struct brcmf_proto_bdc_header *h; | |
307 | ||
a6cfb147 | 308 | brcmf_dbg(CDC, "Enter\n"); |
5b435de0 AS |
309 | |
310 | /* Pop BDC header used to convey priority for buses that don't */ | |
311 | ||
5cd02c77 PH |
312 | if (pktbuf->len <= BDC_HEADER_LEN) { |
313 | brcmf_dbg(INFO, "rx data too short (%d <= %d)\n", | |
5b435de0 AS |
314 | pktbuf->len, BDC_HEADER_LEN); |
315 | return -EBADE; | |
316 | } | |
317 | ||
ea0737d6 | 318 | trace_brcmf_bdchdr(pktbuf->data); |
5b435de0 AS |
319 | h = (struct brcmf_proto_bdc_header *)(pktbuf->data); |
320 | ||
321 | *ifidx = BDC_GET_IF_IDX(h); | |
322 | if (*ifidx >= BRCMF_MAX_IFS) { | |
5e8149f5 | 323 | brcmf_err("rx data ifnum out of range (%d)\n", *ifidx); |
5b435de0 AS |
324 | return -EBADE; |
325 | } | |
2880b868 HM |
326 | /* The ifidx is the idx to map to matching netdev/ifp. When receiving |
327 | * events this is easy because it contains the bssidx which maps | |
328 | * 1-on-1 to the netdev/ifp. But for data frames the ifidx is rcvd. | |
329 | * bssidx 1 is used for p2p0 and no data can be received or | |
330 | * transmitted on it. Therefor bssidx is ifidx + 1 if ifidx > 0 | |
331 | */ | |
332 | if (*ifidx) | |
333 | (*ifidx)++; | |
5b435de0 AS |
334 | |
335 | if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) != | |
336 | BDC_PROTO_VER) { | |
5e8149f5 | 337 | brcmf_err("%s: non-BDC packet received, flags 0x%x\n", |
5b435de0 AS |
338 | brcmf_ifname(drvr, *ifidx), h->flags); |
339 | return -EBADE; | |
340 | } | |
341 | ||
342 | if (h->flags & BDC_FLAG_SUM_GOOD) { | |
a6cfb147 | 343 | brcmf_dbg(CDC, "%s: BDC rcv, good checksum, flags 0x%x\n", |
5b435de0 AS |
344 | brcmf_ifname(drvr, *ifidx), h->flags); |
345 | pkt_set_sum_good(pktbuf, true); | |
346 | } | |
347 | ||
348 | pktbuf->priority = h->priority & BDC_PRIORITY_MASK; | |
349 | ||
350 | skb_pull(pktbuf, BDC_HEADER_LEN); | |
349e7104 AS |
351 | if (do_fws) |
352 | brcmf_fws_hdrpull(drvr, *ifidx, h->data_offset << 2, pktbuf); | |
353 | else | |
354 | skb_pull(pktbuf, h->data_offset << 2); | |
5b435de0 | 355 | |
a43af515 AS |
356 | if (pktbuf->len == 0) |
357 | return -ENODATA; | |
5b435de0 AS |
358 | return 0; |
359 | } | |
360 | ||
85b84133 | 361 | int brcmf_proto_bcdc_attach(struct brcmf_pub *drvr) |
5b435de0 | 362 | { |
85b84133 | 363 | struct brcmf_bcdc *bcdc; |
5b435de0 | 364 | |
85b84133 HM |
365 | bcdc = kzalloc(sizeof(*bcdc), GFP_ATOMIC); |
366 | if (!bcdc) | |
5b435de0 AS |
367 | goto fail; |
368 | ||
369 | /* ensure that the msg buf directly follows the cdc msg struct */ | |
85b84133 HM |
370 | if ((unsigned long)(&bcdc->msg + 1) != (unsigned long)bcdc->buf) { |
371 | brcmf_err("struct brcmf_proto_bcdc is not correctly defined\n"); | |
5b435de0 AS |
372 | goto fail; |
373 | } | |
374 | ||
85b84133 HM |
375 | drvr->proto->hdrpush = brcmf_proto_bcdc_hdrpush; |
376 | drvr->proto->hdrpull = brcmf_proto_bcdc_hdrpull; | |
377 | drvr->proto->query_dcmd = brcmf_proto_bcdc_query_dcmd; | |
378 | drvr->proto->set_dcmd = brcmf_proto_bcdc_set_dcmd; | |
379 | drvr->proto->pd = bcdc; | |
380 | ||
bee1b848 | 381 | drvr->hdrlen += BDC_HEADER_LEN + BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES; |
b01a6b3c | 382 | drvr->bus_if->maxctl = BRCMF_DCMD_MAXLEN + |
5b435de0 AS |
383 | sizeof(struct brcmf_proto_cdc_dcmd) + ROUND_UP_MARGIN; |
384 | return 0; | |
385 | ||
386 | fail: | |
85b84133 | 387 | kfree(bcdc); |
5b435de0 AS |
388 | return -ENOMEM; |
389 | } | |
390 | ||
85b84133 | 391 | void brcmf_proto_bcdc_detach(struct brcmf_pub *drvr) |
5b435de0 | 392 | { |
85b84133 HM |
393 | kfree(drvr->proto->pd); |
394 | drvr->proto->pd = NULL; | |
5b435de0 | 395 | } |