1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2010 Broadcom Corporation
6 /*******************************************************************************
7 * Communicates with the dongle by using dcmd codes.
8 * For certain dcmd codes, the dongle interprets string data from the host.
9 ******************************************************************************/
11 #include <linux/types.h>
12 #include <linux/netdevice.h>
14 #include <brcmu_utils.h>
15 #include <brcmu_wifi.h>
21 #include "tracepoint.h"
25 struct brcmf_proto_bcdc_dcmd
{
26 __le32 cmd
; /* dongle command value */
27 __le32 len
; /* lower 16: output buflen;
28 * upper 16: input buflen (excludes header) */
29 __le32 flags
; /* flag defns given below */
30 __le32 status
; /* status code returned from the device */
33 /* BCDC flag definitions */
34 #define BCDC_DCMD_ERROR 0x01 /* 1=cmd failed */
35 #define BCDC_DCMD_SET 0x02 /* 0=get, 1=set cmd */
36 #define BCDC_DCMD_IF_MASK 0xF000 /* I/F index */
37 #define BCDC_DCMD_IF_SHIFT 12
38 #define BCDC_DCMD_ID_MASK 0xFFFF0000 /* id an cmd pairing */
39 #define BCDC_DCMD_ID_SHIFT 16 /* ID Mask shift bits */
40 #define BCDC_DCMD_ID(flags) \
41 (((flags) & BCDC_DCMD_ID_MASK) >> BCDC_DCMD_ID_SHIFT)
44 * BCDC header - Broadcom specific extension of CDC.
45 * Used on data packets to convey priority across USB.
47 #define BCDC_HEADER_LEN 4
48 #define BCDC_PROTO_VER 2 /* Protocol version */
49 #define BCDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */
50 #define BCDC_FLAG_VER_SHIFT 4 /* Protocol version shift */
51 #define BCDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */
52 #define BCDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */
53 #define BCDC_PRIORITY_MASK 0x7
54 #define BCDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */
55 #define BCDC_FLAG2_IF_SHIFT 0
57 #define BCDC_GET_IF_IDX(hdr) \
58 ((int)((((hdr)->flags2) & BCDC_FLAG2_IF_MASK) >> BCDC_FLAG2_IF_SHIFT))
59 #define BCDC_SET_IF_IDX(hdr, idx) \
60 ((hdr)->flags2 = (((hdr)->flags2 & ~BCDC_FLAG2_IF_MASK) | \
61 ((idx) << BCDC_FLAG2_IF_SHIFT)))
64 * struct brcmf_proto_bcdc_header - BCDC header format
66 * @flags: flags contain protocol and checksum info.
67 * @priority: 802.1d priority and USB flow control info (bit 4:7).
68 * @flags2: additional flags containing dongle interface index.
69 * @data_offset: start of packet data. header is following by firmware signals.
71 struct brcmf_proto_bcdc_header
{
79 * maximum length of firmware signal data between
80 * the BCDC header and packet data in the tx path.
82 #define BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES 12
84 #define RETRIES 2 /* # of retries to retrieve matching dcmd response */
85 #define BUS_HEADER_LEN (16+64) /* Must be atleast SDPCM_RESERVE
86 * (amount of header tha might be added)
87 * plus any space that might be needed
88 * for bus alignment padding.
92 u8 bus_header
[BUS_HEADER_LEN
];
93 struct brcmf_proto_bcdc_dcmd msg
;
94 unsigned char buf
[BRCMF_DCMD_MAXLEN
];
95 struct brcmf_fws_info
*fws
;
99 struct brcmf_fws_info
*drvr_to_fws(struct brcmf_pub
*drvr
)
101 struct brcmf_bcdc
*bcdc
= drvr
->proto
->pd
;
107 brcmf_proto_bcdc_msg(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
, void *buf
,
110 struct brcmf_bcdc
*bcdc
= (struct brcmf_bcdc
*)drvr
->proto
->pd
;
111 struct brcmf_proto_bcdc_dcmd
*msg
= &bcdc
->msg
;
114 brcmf_dbg(BCDC
, "Enter\n");
116 memset(msg
, 0, sizeof(struct brcmf_proto_bcdc_dcmd
));
118 msg
->cmd
= cpu_to_le32(cmd
);
119 msg
->len
= cpu_to_le32(len
);
120 flags
= (++bcdc
->reqid
<< BCDC_DCMD_ID_SHIFT
);
122 flags
|= BCDC_DCMD_SET
;
123 flags
= (flags
& ~BCDC_DCMD_IF_MASK
) |
124 (ifidx
<< BCDC_DCMD_IF_SHIFT
);
125 msg
->flags
= cpu_to_le32(flags
);
128 memcpy(bcdc
->buf
, buf
, len
);
131 if (len
> BRCMF_TX_IOCTL_MAX_MSG_SIZE
)
132 len
= BRCMF_TX_IOCTL_MAX_MSG_SIZE
;
135 return brcmf_bus_txctl(drvr
->bus_if
, (unsigned char *)&bcdc
->msg
, len
);
138 static int brcmf_proto_bcdc_cmplt(struct brcmf_pub
*drvr
, u32 id
, u32 len
)
141 struct brcmf_bcdc
*bcdc
= (struct brcmf_bcdc
*)drvr
->proto
->pd
;
143 brcmf_dbg(BCDC
, "Enter\n");
144 len
+= sizeof(struct brcmf_proto_bcdc_dcmd
);
146 ret
= brcmf_bus_rxctl(drvr
->bus_if
, (unsigned char *)&bcdc
->msg
,
150 } while (BCDC_DCMD_ID(le32_to_cpu(bcdc
->msg
.flags
)) != id
);
156 brcmf_proto_bcdc_query_dcmd(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
,
157 void *buf
, uint len
, int *fwerr
)
159 struct brcmf_bcdc
*bcdc
= (struct brcmf_bcdc
*)drvr
->proto
->pd
;
160 struct brcmf_proto_bcdc_dcmd
*msg
= &bcdc
->msg
;
162 int ret
= 0, retries
= 0;
165 brcmf_dbg(BCDC
, "Enter, cmd %d len %d\n", cmd
, len
);
168 ret
= brcmf_proto_bcdc_msg(drvr
, ifidx
, cmd
, buf
, len
, false);
170 bphy_err(drvr
, "brcmf_proto_bcdc_msg failed w/status %d\n",
176 /* wait for interrupt and get first fragment */
177 ret
= brcmf_proto_bcdc_cmplt(drvr
, bcdc
->reqid
, len
);
181 flags
= le32_to_cpu(msg
->flags
);
182 id
= (flags
& BCDC_DCMD_ID_MASK
) >> BCDC_DCMD_ID_SHIFT
;
184 if ((id
< bcdc
->reqid
) && (++retries
< RETRIES
))
186 if (id
!= bcdc
->reqid
) {
187 bphy_err(drvr
, "%s: unexpected request id %d (expected %d)\n",
188 brcmf_ifname(brcmf_get_ifp(drvr
, ifidx
)), id
,
194 /* Check info buffer */
195 info
= (void *)&bcdc
->buf
[0];
197 /* Copy info buffer */
201 memcpy(buf
, info
, len
);
206 /* Check the ERROR flag */
207 if (flags
& BCDC_DCMD_ERROR
)
208 *fwerr
= le32_to_cpu(msg
->status
);
214 brcmf_proto_bcdc_set_dcmd(struct brcmf_pub
*drvr
, int ifidx
, uint cmd
,
215 void *buf
, uint len
, int *fwerr
)
217 struct brcmf_bcdc
*bcdc
= (struct brcmf_bcdc
*)drvr
->proto
->pd
;
218 struct brcmf_proto_bcdc_dcmd
*msg
= &bcdc
->msg
;
222 brcmf_dbg(BCDC
, "Enter, cmd %d len %d\n", cmd
, len
);
225 ret
= brcmf_proto_bcdc_msg(drvr
, ifidx
, cmd
, buf
, len
, true);
229 ret
= brcmf_proto_bcdc_cmplt(drvr
, bcdc
->reqid
, len
);
233 flags
= le32_to_cpu(msg
->flags
);
234 id
= (flags
& BCDC_DCMD_ID_MASK
) >> BCDC_DCMD_ID_SHIFT
;
236 if (id
!= bcdc
->reqid
) {
237 bphy_err(drvr
, "%s: unexpected request id %d (expected %d)\n",
238 brcmf_ifname(brcmf_get_ifp(drvr
, ifidx
)), id
,
246 /* Check the ERROR flag */
247 if (flags
& BCDC_DCMD_ERROR
)
248 *fwerr
= le32_to_cpu(msg
->status
);
255 brcmf_proto_bcdc_hdrpush(struct brcmf_pub
*drvr
, int ifidx
, u8 offset
,
256 struct sk_buff
*pktbuf
)
258 struct brcmf_proto_bcdc_header
*h
;
260 brcmf_dbg(BCDC
, "Enter\n");
262 /* Push BDC header used to convey priority for buses that don't */
263 skb_push(pktbuf
, BCDC_HEADER_LEN
);
265 h
= (struct brcmf_proto_bcdc_header
*)(pktbuf
->data
);
267 h
->flags
= (BCDC_PROTO_VER
<< BCDC_FLAG_VER_SHIFT
);
268 if (pktbuf
->ip_summed
== CHECKSUM_PARTIAL
)
269 h
->flags
|= BCDC_FLAG_SUM_NEEDED
;
271 h
->priority
= (pktbuf
->priority
& BCDC_PRIORITY_MASK
);
273 h
->data_offset
= offset
;
274 BCDC_SET_IF_IDX(h
, ifidx
);
275 trace_brcmf_bcdchdr(pktbuf
->data
);
279 brcmf_proto_bcdc_hdrpull(struct brcmf_pub
*drvr
, bool do_fws
,
280 struct sk_buff
*pktbuf
, struct brcmf_if
**ifp
)
282 struct brcmf_proto_bcdc_header
*h
;
283 struct brcmf_if
*tmp_if
;
285 brcmf_dbg(BCDC
, "Enter\n");
287 /* Pop BCDC header used to convey priority for buses that don't */
288 if (pktbuf
->len
<= BCDC_HEADER_LEN
) {
289 brcmf_dbg(INFO
, "rx data too short (%d <= %d)\n",
290 pktbuf
->len
, BCDC_HEADER_LEN
);
294 trace_brcmf_bcdchdr(pktbuf
->data
);
295 h
= (struct brcmf_proto_bcdc_header
*)(pktbuf
->data
);
297 tmp_if
= brcmf_get_ifp(drvr
, BCDC_GET_IF_IDX(h
));
299 brcmf_dbg(INFO
, "no matching ifp found\n");
302 if (((h
->flags
& BCDC_FLAG_VER_MASK
) >> BCDC_FLAG_VER_SHIFT
) !=
304 bphy_err(drvr
, "%s: non-BCDC packet received, flags 0x%x\n",
305 brcmf_ifname(tmp_if
), h
->flags
);
309 if (h
->flags
& BCDC_FLAG_SUM_GOOD
) {
310 brcmf_dbg(BCDC
, "%s: BDC rcv, good checksum, flags 0x%x\n",
311 brcmf_ifname(tmp_if
), h
->flags
);
312 pktbuf
->ip_summed
= CHECKSUM_UNNECESSARY
;
315 pktbuf
->priority
= h
->priority
& BCDC_PRIORITY_MASK
;
317 skb_pull(pktbuf
, BCDC_HEADER_LEN
);
319 brcmf_fws_hdrpull(tmp_if
, h
->data_offset
<< 2, pktbuf
);
321 skb_pull(pktbuf
, h
->data_offset
<< 2);
323 if (pktbuf
->len
== 0)
331 static int brcmf_proto_bcdc_tx_queue_data(struct brcmf_pub
*drvr
, int ifidx
,
334 struct brcmf_if
*ifp
= brcmf_get_ifp(drvr
, ifidx
);
335 struct brcmf_bcdc
*bcdc
= drvr
->proto
->pd
;
337 if (!brcmf_fws_queue_skbs(bcdc
->fws
))
338 return brcmf_proto_txdata(drvr
, ifidx
, 0, skb
);
340 return brcmf_fws_process_skb(ifp
, skb
);
344 brcmf_proto_bcdc_txdata(struct brcmf_pub
*drvr
, int ifidx
, u8 offset
,
345 struct sk_buff
*pktbuf
)
347 brcmf_proto_bcdc_hdrpush(drvr
, ifidx
, offset
, pktbuf
);
348 return brcmf_bus_txdata(drvr
->bus_if
, pktbuf
);
351 void brcmf_proto_bcdc_txflowblock(struct device
*dev
, bool state
)
353 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
354 struct brcmf_pub
*drvr
= bus_if
->drvr
;
356 brcmf_dbg(TRACE
, "Enter\n");
358 brcmf_fws_bus_blocked(drvr
, state
);
362 brcmf_proto_bcdc_txcomplete(struct device
*dev
, struct sk_buff
*txp
,
365 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
366 struct brcmf_bcdc
*bcdc
= bus_if
->drvr
->proto
->pd
;
367 struct brcmf_if
*ifp
;
369 /* await txstatus signal for firmware if active */
370 if (brcmf_fws_fc_active(bcdc
->fws
)) {
372 brcmf_fws_bustxfail(bcdc
->fws
, txp
);
374 if (brcmf_proto_bcdc_hdrpull(bus_if
->drvr
, false, txp
, &ifp
))
375 brcmu_pkt_buf_free_skb(txp
);
377 brcmf_txfinalize(ifp
, txp
, success
);
382 brcmf_proto_bcdc_configure_addr_mode(struct brcmf_pub
*drvr
, int ifidx
,
383 enum proto_addr_mode addr_mode
)
388 brcmf_proto_bcdc_delete_peer(struct brcmf_pub
*drvr
, int ifidx
,
394 brcmf_proto_bcdc_add_tdls_peer(struct brcmf_pub
*drvr
, int ifidx
,
399 static void brcmf_proto_bcdc_rxreorder(struct brcmf_if
*ifp
,
400 struct sk_buff
*skb
, bool inirq
)
402 brcmf_fws_rxreorder(ifp
, skb
, inirq
);
406 brcmf_proto_bcdc_add_if(struct brcmf_if
*ifp
)
408 brcmf_fws_add_interface(ifp
);
412 brcmf_proto_bcdc_del_if(struct brcmf_if
*ifp
)
414 brcmf_fws_del_interface(ifp
);
418 brcmf_proto_bcdc_reset_if(struct brcmf_if
*ifp
)
420 brcmf_fws_reset_interface(ifp
);
424 brcmf_proto_bcdc_init_done(struct brcmf_pub
*drvr
)
426 struct brcmf_bcdc
*bcdc
= drvr
->proto
->pd
;
427 struct brcmf_fws_info
*fws
;
429 fws
= brcmf_fws_attach(drvr
);
437 static void brcmf_proto_bcdc_debugfs_create(struct brcmf_pub
*drvr
)
439 brcmf_fws_debugfs_create(drvr
);
442 int brcmf_proto_bcdc_attach(struct brcmf_pub
*drvr
)
444 struct brcmf_bcdc
*bcdc
;
446 bcdc
= kzalloc(sizeof(*bcdc
), GFP_ATOMIC
);
450 /* ensure that the msg buf directly follows the cdc msg struct */
451 if ((unsigned long)(&bcdc
->msg
+ 1) != (unsigned long)bcdc
->buf
) {
452 bphy_err(drvr
, "struct brcmf_proto_bcdc is not correctly defined\n");
456 drvr
->proto
->hdrpull
= brcmf_proto_bcdc_hdrpull
;
457 drvr
->proto
->query_dcmd
= brcmf_proto_bcdc_query_dcmd
;
458 drvr
->proto
->set_dcmd
= brcmf_proto_bcdc_set_dcmd
;
459 drvr
->proto
->tx_queue_data
= brcmf_proto_bcdc_tx_queue_data
;
460 drvr
->proto
->txdata
= brcmf_proto_bcdc_txdata
;
461 drvr
->proto
->configure_addr_mode
= brcmf_proto_bcdc_configure_addr_mode
;
462 drvr
->proto
->delete_peer
= brcmf_proto_bcdc_delete_peer
;
463 drvr
->proto
->add_tdls_peer
= brcmf_proto_bcdc_add_tdls_peer
;
464 drvr
->proto
->rxreorder
= brcmf_proto_bcdc_rxreorder
;
465 drvr
->proto
->add_if
= brcmf_proto_bcdc_add_if
;
466 drvr
->proto
->del_if
= brcmf_proto_bcdc_del_if
;
467 drvr
->proto
->reset_if
= brcmf_proto_bcdc_reset_if
;
468 drvr
->proto
->init_done
= brcmf_proto_bcdc_init_done
;
469 drvr
->proto
->debugfs_create
= brcmf_proto_bcdc_debugfs_create
;
470 drvr
->proto
->pd
= bcdc
;
472 drvr
->hdrlen
+= BCDC_HEADER_LEN
+ BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES
;
473 drvr
->bus_if
->maxctl
= BRCMF_DCMD_MAXLEN
+
474 sizeof(struct brcmf_proto_bcdc_dcmd
);
482 void brcmf_proto_bcdc_detach(struct brcmf_pub
*drvr
)
484 struct brcmf_bcdc
*bcdc
= drvr
->proto
->pd
;
486 drvr
->proto
->pd
= NULL
;
487 brcmf_fws_detach(bcdc
->fws
);