1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/types.h>
23#include <linux/netdevice.h>
24
25#include <brcmu_utils.h>
26#include <brcmu_wifi.h>
27
28#include "dhd.h"
29#include "dhd_proto.h"
30#include "dhd_bus.h"
31#include "fwsignal.h"
32#include "dhd_dbg.h"
33
34struct brcmf_proto_cdc_dcmd {
35 __le32 cmd;
36 __le32 len;
37
38 __le32 flags;
39 __le32 status;
40};
41
42
43#define CDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN)
44
45
46#define CDC_DCMD_ERROR 0x01
47#define CDC_DCMD_SET 0x02
48#define CDC_DCMD_IF_MASK 0xF000
49#define CDC_DCMD_IF_SHIFT 12
50#define CDC_DCMD_ID_MASK 0xFFFF0000
51#define CDC_DCMD_ID_SHIFT 16
52#define CDC_DCMD_ID(flags) \
53 (((flags) & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT)
54
55
56
57
58
59#define BDC_HEADER_LEN 4
60#define BDC_PROTO_VER 2
61#define BDC_FLAG_VER_MASK 0xf0
62#define BDC_FLAG_VER_SHIFT 4
63#define BDC_FLAG_SUM_GOOD 0x04
64#define BDC_FLAG_SUM_NEEDED 0x08
65#define BDC_PRIORITY_MASK 0x7
66#define BDC_FLAG2_IF_MASK 0x0f
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
75
76
77
78
79
80
81
82
83struct brcmf_proto_bdc_header {
84 u8 flags;
85 u8 priority;
86 u8 flags2;
87 u8 data_offset;
88};
89
90
91
92
93
94#define BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES 12
95
96#define RETRIES 2
97#define BUS_HEADER_LEN (16+64)
98
99
100
101
102#define ROUND_UP_MARGIN 2048
103
104
105
106
107struct brcmf_proto {
108 u16 reqid;
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
114static 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
120 brcmf_dbg(CDC, "Enter\n");
121
122
123
124
125
126 if (len > CDC_MAX_MSG_SIZE)
127 len = CDC_MAX_MSG_SIZE;
128
129
130 return brcmf_bus_txctl(drvr->bus_if, (unsigned char *)&prot->msg, len);
131}
132
133static 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
138 brcmf_dbg(CDC, "Enter\n");
139 len += sizeof(struct brcmf_proto_cdc_dcmd);
140 do {
141 ret = brcmf_bus_rxctl(drvr->bus_if, (unsigned char *)&prot->msg,
142 len);
143 if (ret < 0)
144 break;
145 } while (CDC_DCMD_ID(le32_to_cpu(prot->msg.flags)) != id);
146
147 return ret;
148}
149
150int
151brcmf_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
160 brcmf_dbg(CDC, "Enter, cmd %d len %d\n", cmd, len);
161
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) {
176 brcmf_err("brcmf_proto_cdc_msg failed w/status %d\n",
177 ret);
178 goto done;
179 }
180
181retry:
182
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) {
193 brcmf_err("%s: unexpected request id %d (expected %d)\n",
194 brcmf_ifname(drvr, ifidx), id, prot->reqid);
195 ret = -EINVAL;
196 goto done;
197 }
198
199
200 info = (void *)&msg[1];
201
202
203 if (buf) {
204 if (ret < (int)len)
205 len = ret;
206 memcpy(buf, info, len);
207 }
208
209
210 if (flags & CDC_DCMD_ERROR)
211 ret = le32_to_cpu(msg->status);
212
213done:
214 return ret;
215}
216
217int 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
225 brcmf_dbg(CDC, "Enter, cmd %d len %d\n", cmd, len);
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) {
251 brcmf_err("%s: unexpected request id %d (expected %d)\n",
252 brcmf_ifname(drvr, ifidx), id, prot->reqid);
253 ret = -EINVAL;
254 goto done;
255 }
256
257
258 if (flags & CDC_DCMD_ERROR)
259 ret = le32_to_cpu(msg->status);
260
261done:
262 return ret;
263}
264
265static bool pkt_sum_needed(struct sk_buff *skb)
266{
267 return skb->ip_summed == CHECKSUM_PARTIAL;
268}
269
270static void pkt_set_sum_good(struct sk_buff *skb, bool x)
271{
272 skb->ip_summed = (x ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
273}
274
275void brcmf_proto_hdrpush(struct brcmf_pub *drvr, int ifidx, u8 offset,
276 struct sk_buff *pktbuf)
277{
278 struct brcmf_proto_bdc_header *h;
279
280 brcmf_dbg(CDC, "Enter\n");
281
282
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;
293 h->data_offset = offset;
294 BDC_SET_IF_IDX(h, ifidx);
295}
296
297int brcmf_proto_hdrpull(struct brcmf_pub *drvr, bool do_fws, u8 *ifidx,
298 struct sk_buff *pktbuf)
299{
300 struct brcmf_proto_bdc_header *h;
301
302 brcmf_dbg(CDC, "Enter\n");
303
304
305
306 if (pktbuf->len <= BDC_HEADER_LEN) {
307 brcmf_dbg(INFO, "rx data too short (%d <= %d)\n",
308 pktbuf->len, BDC_HEADER_LEN);
309 return -EBADE;
310 }
311
312 h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
313
314 *ifidx = BDC_GET_IF_IDX(h);
315 if (*ifidx >= BRCMF_MAX_IFS) {
316 brcmf_err("rx data ifnum out of range (%d)\n", *ifidx);
317 return -EBADE;
318 }
319
320
321
322
323
324
325 if (*ifidx)
326 (*ifidx)++;
327
328 if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
329 BDC_PROTO_VER) {
330 brcmf_err("%s: non-BDC packet received, flags 0x%x\n",
331 brcmf_ifname(drvr, *ifidx), h->flags);
332 return -EBADE;
333 }
334
335 if (h->flags & BDC_FLAG_SUM_GOOD) {
336 brcmf_dbg(CDC, "%s: BDC rcv, good checksum, flags 0x%x\n",
337 brcmf_ifname(drvr, *ifidx), h->flags);
338 pkt_set_sum_good(pktbuf, true);
339 }
340
341 pktbuf->priority = h->priority & BDC_PRIORITY_MASK;
342
343 skb_pull(pktbuf, BDC_HEADER_LEN);
344 if (do_fws)
345 brcmf_fws_hdrpull(drvr, *ifidx, h->data_offset << 2, pktbuf);
346 else
347 skb_pull(pktbuf, h->data_offset << 2);
348
349 if (pktbuf->len == 0)
350 return -ENODATA;
351 return 0;
352}
353
354int brcmf_proto_attach(struct brcmf_pub *drvr)
355{
356 struct brcmf_proto *cdc;
357
358 cdc = kzalloc(sizeof(struct brcmf_proto), GFP_ATOMIC);
359 if (!cdc)
360 goto fail;
361
362
363 if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) {
364 brcmf_err("struct brcmf_proto is not correctly defined\n");
365 goto fail;
366 }
367
368 drvr->prot = cdc;
369 drvr->hdrlen += BDC_HEADER_LEN + BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES;
370 drvr->bus_if->maxctl = BRCMF_DCMD_MAXLEN +
371 sizeof(struct brcmf_proto_cdc_dcmd) + ROUND_UP_MARGIN;
372 return 0;
373
374fail:
375 kfree(cdc);
376 return -ENOMEM;
377}
378
379
380void brcmf_proto_detach(struct brcmf_pub *drvr)
381{
382 kfree(drvr->prot);
383 drvr->prot = NULL;
384}
385
386void brcmf_proto_stop(struct brcmf_pub *drvr)
387{
388
389}
390