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