• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core.h"
29 #include "bus.h"
30 #include "fwsignal.h"
31 #include "debug.h"
32 #include "tracepoint.h"
33 #include "proto.h"
34 #include "bcdc.h"
35 
36 struct brcmf_proto_bcdc_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 /* BCDC flag definitions */
45 #define BCDC_DCMD_ERROR		0x01		/* 1=cmd failed */
46 #define BCDC_DCMD_SET		0x02		/* 0=get, 1=set cmd */
47 #define BCDC_DCMD_IF_MASK	0xF000		/* I/F index */
48 #define BCDC_DCMD_IF_SHIFT	12
49 #define BCDC_DCMD_ID_MASK	0xFFFF0000	/* id an cmd pairing */
50 #define BCDC_DCMD_ID_SHIFT	16		/* ID Mask shift bits */
51 #define BCDC_DCMD_ID(flags)	\
52 	(((flags) & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT)
53 
54 /*
55  * BCDC header - Broadcom specific extension of CDC.
56  * Used on data packets to convey priority across USB.
57  */
58 #define	BCDC_HEADER_LEN		4
59 #define BCDC_PROTO_VER		2	/* Protocol version */
60 #define BCDC_FLAG_VER_MASK	0xf0	/* Protocol version mask */
61 #define BCDC_FLAG_VER_SHIFT	4	/* Protocol version shift */
62 #define BCDC_FLAG_SUM_GOOD	0x04	/* Good RX checksums */
63 #define BCDC_FLAG_SUM_NEEDED	0x08	/* Dongle needs to do TX checksums */
64 #define BCDC_PRIORITY_MASK	0x7
65 #define BCDC_FLAG2_IF_MASK	0x0f	/* packet rx interface in APSTA */
66 #define BCDC_FLAG2_IF_SHIFT	0
67 
68 #define BCDC_GET_IF_IDX(hdr) \
69 	((int)((((hdr)->flags2) & BCDC_FLAG2_IF_MASK) >> BCDC_FLAG2_IF_SHIFT))
70 #define BCDC_SET_IF_IDX(hdr, idx) \
71 	((hdr)->flags2 = (((hdr)->flags2 & ~BCDC_FLAG2_IF_MASK) | \
72 	((idx) << BCDC_FLAG2_IF_SHIFT)))
73 
74 /**
75  * struct brcmf_proto_bcdc_header - BCDC header format
76  *
77  * @flags: flags contain protocol and checksum info.
78  * @priority: 802.1d priority and USB flow control info (bit 4:7).
79  * @flags2: additional flags containing dongle interface index.
80  * @data_offset: start of packet data. header is following by firmware signals.
81  */
82 struct brcmf_proto_bcdc_header {
83 	u8 flags;
84 	u8 priority;
85 	u8 flags2;
86 	u8 data_offset;
87 };
88 
89 /*
90  * maximum length of firmware signal data between
91  * the BCDC header and packet data in the tx path.
92  */
93 #define BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES	12
94 
95 #define RETRIES 2 /* # of retries to retrieve matching dcmd response */
96 #define BUS_HEADER_LEN	(16+64)		/* Must be atleast SDPCM_RESERVE
97 					 * (amount of header tha might be added)
98 					 * plus any space that might be needed
99 					 * for bus alignment padding.
100 					 */
101 struct brcmf_bcdc {
102 	u16 reqid;
103 	u8 bus_header[BUS_HEADER_LEN];
104 	struct brcmf_proto_bcdc_dcmd msg;
105 	unsigned char buf[BRCMF_DCMD_MAXLEN];
106 };
107 
108 
109 static int
brcmf_proto_bcdc_msg(struct brcmf_pub * drvr,int ifidx,uint cmd,void * buf,uint len,bool set)110 brcmf_proto_bcdc_msg(struct brcmf_pub *drvr, int ifidx, uint cmd, void *buf,
111 		     uint len, bool set)
112 {
113 	struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
114 	struct brcmf_proto_bcdc_dcmd *msg = &bcdc->msg;
115 	u32 flags;
116 
117 	brcmf_dbg(BCDC, "Enter\n");
118 
119 	memset(msg, 0, sizeof(struct brcmf_proto_bcdc_dcmd));
120 
121 	msg->cmd = cpu_to_le32(cmd);
122 	msg->len = cpu_to_le32(len);
123 	flags = (++bcdc->reqid << BCDC_DCMD_ID_SHIFT);
124 	if (set)
125 		flags |= BCDC_DCMD_SET;
126 	flags = (flags & ~BCDC_DCMD_IF_MASK) |
127 		(ifidx << BCDC_DCMD_IF_SHIFT);
128 	msg->flags = cpu_to_le32(flags);
129 
130 	if (buf)
131 		memcpy(bcdc->buf, buf, len);
132 
133 	len += sizeof(*msg);
134 	if (len > BRCMF_TX_IOCTL_MAX_MSG_SIZE)
135 		len = BRCMF_TX_IOCTL_MAX_MSG_SIZE;
136 
137 	/* Send request */
138 	return brcmf_bus_txctl(drvr->bus_if, (unsigned char *)&bcdc->msg, len);
139 }
140 
brcmf_proto_bcdc_cmplt(struct brcmf_pub * drvr,u32 id,u32 len)141 static int brcmf_proto_bcdc_cmplt(struct brcmf_pub *drvr, u32 id, u32 len)
142 {
143 	int ret;
144 	struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
145 
146 	brcmf_dbg(BCDC, "Enter\n");
147 	len += sizeof(struct brcmf_proto_bcdc_dcmd);
148 	do {
149 		ret = brcmf_bus_rxctl(drvr->bus_if, (unsigned char *)&bcdc->msg,
150 				      len);
151 		if (ret < 0)
152 			break;
153 	} while (BCDC_DCMD_ID(le32_to_cpu(bcdc->msg.flags)) != id);
154 
155 	return ret;
156 }
157 
158 static int
brcmf_proto_bcdc_query_dcmd(struct brcmf_pub * drvr,int ifidx,uint cmd,void * buf,uint len)159 brcmf_proto_bcdc_query_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
160 			    void *buf, uint len)
161 {
162 	struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
163 	struct brcmf_proto_bcdc_dcmd *msg = &bcdc->msg;
164 	void *info;
165 	int ret = 0, retries = 0;
166 	u32 id, flags;
167 
168 	brcmf_dbg(BCDC, "Enter, cmd %d len %d\n", cmd, len);
169 
170 	ret = brcmf_proto_bcdc_msg(drvr, ifidx, cmd, buf, len, false);
171 	if (ret < 0) {
172 		brcmf_err("brcmf_proto_bcdc_msg failed w/status %d\n",
173 			  ret);
174 		goto done;
175 	}
176 
177 retry:
178 	/* wait for interrupt and get first fragment */
179 	ret = brcmf_proto_bcdc_cmplt(drvr, bcdc->reqid, len);
180 	if (ret < 0)
181 		goto done;
182 
183 	flags = le32_to_cpu(msg->flags);
184 	id = (flags & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT;
185 
186 	if ((id < bcdc->reqid) && (++retries < RETRIES))
187 		goto retry;
188 	if (id != bcdc->reqid) {
189 		brcmf_err("%s: unexpected request id %d (expected %d)\n",
190 			  brcmf_ifname(drvr, ifidx), id, bcdc->reqid);
191 		ret = -EINVAL;
192 		goto done;
193 	}
194 
195 	/* Check info buffer */
196 	info = (void *)&msg[1];
197 
198 	/* Copy info buffer */
199 	if (buf) {
200 		if (ret < (int)len)
201 			len = ret;
202 		memcpy(buf, info, len);
203 	}
204 
205 	/* Check the ERROR flag */
206 	if (flags & BCDC_DCMD_ERROR)
207 		ret = le32_to_cpu(msg->status);
208 
209 done:
210 	return ret;
211 }
212 
213 static int
brcmf_proto_bcdc_set_dcmd(struct brcmf_pub * drvr,int ifidx,uint cmd,void * buf,uint len)214 brcmf_proto_bcdc_set_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
215 			  void *buf, uint len)
216 {
217 	struct brcmf_bcdc *bcdc = (struct brcmf_bcdc *)drvr->proto->pd;
218 	struct brcmf_proto_bcdc_dcmd *msg = &bcdc->msg;
219 	int ret = 0;
220 	u32 flags, id;
221 
222 	brcmf_dbg(BCDC, "Enter, cmd %d len %d\n", cmd, len);
223 
224 	ret = brcmf_proto_bcdc_msg(drvr, ifidx, cmd, buf, len, true);
225 	if (ret < 0)
226 		goto done;
227 
228 	ret = brcmf_proto_bcdc_cmplt(drvr, bcdc->reqid, len);
229 	if (ret < 0)
230 		goto done;
231 
232 	flags = le32_to_cpu(msg->flags);
233 	id = (flags & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT;
234 
235 	if (id != bcdc->reqid) {
236 		brcmf_err("%s: unexpected request id %d (expected %d)\n",
237 			  brcmf_ifname(drvr, ifidx), id, bcdc->reqid);
238 		ret = -EINVAL;
239 		goto done;
240 	}
241 
242 	/* Check the ERROR flag */
243 	if (flags & BCDC_DCMD_ERROR)
244 		ret = le32_to_cpu(msg->status);
245 
246 done:
247 	return ret;
248 }
249 
250 static void
brcmf_proto_bcdc_hdrpush(struct brcmf_pub * drvr,int ifidx,u8 offset,struct sk_buff * pktbuf)251 brcmf_proto_bcdc_hdrpush(struct brcmf_pub *drvr, int ifidx, u8 offset,
252 			 struct sk_buff *pktbuf)
253 {
254 	struct brcmf_proto_bcdc_header *h;
255 
256 	brcmf_dbg(BCDC, "Enter\n");
257 
258 	/* Push BDC header used to convey priority for buses that don't */
259 	skb_push(pktbuf, BCDC_HEADER_LEN);
260 
261 	h = (struct brcmf_proto_bcdc_header *)(pktbuf->data);
262 
263 	h->flags = (BCDC_PROTO_VER << BCDC_FLAG_VER_SHIFT);
264 	if (pktbuf->ip_summed == CHECKSUM_PARTIAL)
265 		h->flags |= BCDC_FLAG_SUM_NEEDED;
266 
267 	h->priority = (pktbuf->priority & BCDC_PRIORITY_MASK);
268 	h->flags2 = 0;
269 	h->data_offset = offset;
270 	BCDC_SET_IF_IDX(h, ifidx);
271 	trace_brcmf_bcdchdr(pktbuf->data);
272 }
273 
274 static int
brcmf_proto_bcdc_hdrpull(struct brcmf_pub * drvr,bool do_fws,struct sk_buff * pktbuf,struct brcmf_if ** ifp)275 brcmf_proto_bcdc_hdrpull(struct brcmf_pub *drvr, bool do_fws,
276 			 struct sk_buff *pktbuf, struct brcmf_if **ifp)
277 {
278 	struct brcmf_proto_bcdc_header *h;
279 	struct brcmf_if *tmp_if;
280 
281 	brcmf_dbg(BCDC, "Enter\n");
282 
283 	/* Pop BCDC header used to convey priority for buses that don't */
284 	if (pktbuf->len <= BCDC_HEADER_LEN) {
285 		brcmf_dbg(INFO, "rx data too short (%d <= %d)\n",
286 			  pktbuf->len, BCDC_HEADER_LEN);
287 		return -EBADE;
288 	}
289 
290 	trace_brcmf_bcdchdr(pktbuf->data);
291 	h = (struct brcmf_proto_bcdc_header *)(pktbuf->data);
292 
293 	tmp_if = brcmf_get_ifp(drvr, BCDC_GET_IF_IDX(h));
294 	if (!tmp_if) {
295 		brcmf_dbg(INFO, "no matching ifp found\n");
296 		return -EBADE;
297 	}
298 	if (((h->flags & BCDC_FLAG_VER_MASK) >> BCDC_FLAG_VER_SHIFT) !=
299 	    BCDC_PROTO_VER) {
300 		brcmf_err("%s: non-BCDC packet received, flags 0x%x\n",
301 			  brcmf_ifname(drvr, tmp_if->ifidx), h->flags);
302 		return -EBADE;
303 	}
304 
305 	if (h->flags & BCDC_FLAG_SUM_GOOD) {
306 		brcmf_dbg(BCDC, "%s: BDC rcv, good checksum, flags 0x%x\n",
307 			  brcmf_ifname(drvr, tmp_if->ifidx), h->flags);
308 		pktbuf->ip_summed = CHECKSUM_UNNECESSARY;
309 	}
310 
311 	pktbuf->priority = h->priority & BCDC_PRIORITY_MASK;
312 
313 	skb_pull(pktbuf, BCDC_HEADER_LEN);
314 	if (do_fws)
315 		brcmf_fws_hdrpull(tmp_if, h->data_offset << 2, pktbuf);
316 	else
317 		skb_pull(pktbuf, h->data_offset << 2);
318 
319 	if (pktbuf->len == 0)
320 		return -ENODATA;
321 
322 	*ifp = tmp_if;
323 	return 0;
324 }
325 
326 static int
brcmf_proto_bcdc_txdata(struct brcmf_pub * drvr,int ifidx,u8 offset,struct sk_buff * pktbuf)327 brcmf_proto_bcdc_txdata(struct brcmf_pub *drvr, int ifidx, u8 offset,
328 			struct sk_buff *pktbuf)
329 {
330 	brcmf_proto_bcdc_hdrpush(drvr, ifidx, offset, pktbuf);
331 	return brcmf_bus_txdata(drvr->bus_if, pktbuf);
332 }
333 
334 static void
brcmf_proto_bcdc_configure_addr_mode(struct brcmf_pub * drvr,int ifidx,enum proto_addr_mode addr_mode)335 brcmf_proto_bcdc_configure_addr_mode(struct brcmf_pub *drvr, int ifidx,
336 				     enum proto_addr_mode addr_mode)
337 {
338 }
339 
340 static void
brcmf_proto_bcdc_delete_peer(struct brcmf_pub * drvr,int ifidx,u8 peer[ETH_ALEN])341 brcmf_proto_bcdc_delete_peer(struct brcmf_pub *drvr, int ifidx,
342 			     u8 peer[ETH_ALEN])
343 {
344 }
345 
346 static void
brcmf_proto_bcdc_add_tdls_peer(struct brcmf_pub * drvr,int ifidx,u8 peer[ETH_ALEN])347 brcmf_proto_bcdc_add_tdls_peer(struct brcmf_pub *drvr, int ifidx,
348 			       u8 peer[ETH_ALEN])
349 {
350 }
351 
brcmf_proto_bcdc_attach(struct brcmf_pub * drvr)352 int brcmf_proto_bcdc_attach(struct brcmf_pub *drvr)
353 {
354 	struct brcmf_bcdc *bcdc;
355 
356 	bcdc = kzalloc(sizeof(*bcdc), GFP_ATOMIC);
357 	if (!bcdc)
358 		goto fail;
359 
360 	/* ensure that the msg buf directly follows the cdc msg struct */
361 	if ((unsigned long)(&bcdc->msg + 1) != (unsigned long)bcdc->buf) {
362 		brcmf_err("struct brcmf_proto_bcdc is not correctly defined\n");
363 		goto fail;
364 	}
365 
366 	drvr->proto->hdrpull = brcmf_proto_bcdc_hdrpull;
367 	drvr->proto->query_dcmd = brcmf_proto_bcdc_query_dcmd;
368 	drvr->proto->set_dcmd = brcmf_proto_bcdc_set_dcmd;
369 	drvr->proto->txdata = brcmf_proto_bcdc_txdata;
370 	drvr->proto->configure_addr_mode = brcmf_proto_bcdc_configure_addr_mode;
371 	drvr->proto->delete_peer = brcmf_proto_bcdc_delete_peer;
372 	drvr->proto->add_tdls_peer = brcmf_proto_bcdc_add_tdls_peer;
373 	drvr->proto->pd = bcdc;
374 
375 	drvr->hdrlen += BCDC_HEADER_LEN + BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES;
376 	drvr->bus_if->maxctl = BRCMF_DCMD_MAXLEN +
377 			sizeof(struct brcmf_proto_bcdc_dcmd);
378 	return 0;
379 
380 fail:
381 	kfree(bcdc);
382 	return -ENOMEM;
383 }
384 
brcmf_proto_bcdc_detach(struct brcmf_pub * drvr)385 void brcmf_proto_bcdc_detach(struct brcmf_pub *drvr)
386 {
387 	kfree(drvr->proto->pd);
388 	drvr->proto->pd = NULL;
389 }
390