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