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