xref: /freebsd/sys/contrib/dev/broadcom/brcm80211/brcmfmac/msgbuf.c (revision 902136e0fe112383ec64d2ef43a446063b5e6417)
1b4c3e9b5SBjoern A. Zeeb // SPDX-License-Identifier: ISC
2b4c3e9b5SBjoern A. Zeeb /*
3b4c3e9b5SBjoern A. Zeeb  * Copyright (c) 2014 Broadcom Corporation
4b4c3e9b5SBjoern A. Zeeb  */
5b4c3e9b5SBjoern A. Zeeb 
6b4c3e9b5SBjoern A. Zeeb /*******************************************************************************
7b4c3e9b5SBjoern A. Zeeb  * Communicates with the dongle by using dcmd codes.
8b4c3e9b5SBjoern A. Zeeb  * For certain dcmd codes, the dongle interprets string data from the host.
9b4c3e9b5SBjoern A. Zeeb  ******************************************************************************/
10b4c3e9b5SBjoern A. Zeeb 
11b4c3e9b5SBjoern A. Zeeb #include <linux/types.h>
12b4c3e9b5SBjoern A. Zeeb #include <linux/netdevice.h>
13b4c3e9b5SBjoern A. Zeeb #include <linux/etherdevice.h>
14*902136e0SBjoern A. Zeeb #if defined(__FreeBSD__)
15*902136e0SBjoern A. Zeeb #include <linux/delay.h>
16*902136e0SBjoern A. Zeeb #ifdef DEBUG
17*902136e0SBjoern A. Zeeb #include <linux/seq_file.h>
18*902136e0SBjoern A. Zeeb #endif
19*902136e0SBjoern A. Zeeb #endif
20b4c3e9b5SBjoern A. Zeeb 
21b4c3e9b5SBjoern A. Zeeb #include <brcmu_utils.h>
22b4c3e9b5SBjoern A. Zeeb #include <brcmu_wifi.h>
23b4c3e9b5SBjoern A. Zeeb 
24b4c3e9b5SBjoern A. Zeeb #include "core.h"
25b4c3e9b5SBjoern A. Zeeb #include "debug.h"
26b4c3e9b5SBjoern A. Zeeb #include "proto.h"
27b4c3e9b5SBjoern A. Zeeb #include "msgbuf.h"
28b4c3e9b5SBjoern A. Zeeb #include "commonring.h"
29b4c3e9b5SBjoern A. Zeeb #include "flowring.h"
30b4c3e9b5SBjoern A. Zeeb #include "bus.h"
31b4c3e9b5SBjoern A. Zeeb #include "tracepoint.h"
32b4c3e9b5SBjoern A. Zeeb 
33b4c3e9b5SBjoern A. Zeeb 
34b4c3e9b5SBjoern A. Zeeb #define MSGBUF_IOCTL_RESP_TIMEOUT		msecs_to_jiffies(2000)
35b4c3e9b5SBjoern A. Zeeb 
36b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_GEN_STATUS			0x1
37b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_RING_STATUS			0x2
38b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_FLOW_RING_CREATE		0x3
39b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_FLOW_RING_CREATE_CMPLT	0x4
40b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_FLOW_RING_DELETE		0x5
41b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_FLOW_RING_DELETE_CMPLT	0x6
42b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_FLOW_RING_FLUSH		0x7
43b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_FLOW_RING_FLUSH_CMPLT	0x8
44b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_IOCTLPTR_REQ		0x9
45b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_IOCTLPTR_REQ_ACK		0xA
46b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_IOCTLRESP_BUF_POST		0xB
47b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_IOCTL_CMPLT			0xC
48b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_EVENT_BUF_POST		0xD
49b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_WL_EVENT			0xE
50b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_TX_POST			0xF
51b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_TX_STATUS			0x10
52b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_RXBUF_POST			0x11
53b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_RX_CMPLT			0x12
54b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_LPBK_DMAXFER		0x13
55b4c3e9b5SBjoern A. Zeeb #define MSGBUF_TYPE_LPBK_DMAXFER_CMPLT		0x14
56b4c3e9b5SBjoern A. Zeeb 
57b4c3e9b5SBjoern A. Zeeb #define NR_TX_PKTIDS				2048
58b4c3e9b5SBjoern A. Zeeb #define NR_RX_PKTIDS				1024
59b4c3e9b5SBjoern A. Zeeb 
60b4c3e9b5SBjoern A. Zeeb #define BRCMF_IOCTL_REQ_PKTID			0xFFFE
61b4c3e9b5SBjoern A. Zeeb 
62b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_MAX_PKT_SIZE		2048
63b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_MAX_CTL_PKT_SIZE           8192
64b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_RXBUFPOST_THRESHOLD	32
65b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_MAX_IOCTLRESPBUF_POST	8
66b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_MAX_EVENTBUF_POST		8
67b4c3e9b5SBjoern A. Zeeb 
68b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_PKT_FLAGS_FRAME_802_3	0x01
69b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_PKT_FLAGS_FRAME_802_11	0x02
70b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_PKT_FLAGS_FRAME_MASK	0x07
71b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_PKT_FLAGS_PRIO_SHIFT	5
72b4c3e9b5SBjoern A. Zeeb 
73b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_TX_FLUSH_CNT1		32
74b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_TX_FLUSH_CNT2		96
75b4c3e9b5SBjoern A. Zeeb 
76b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_DELAY_TXWORKER_THRS	96
77b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_TRICKLE_TXWORKER_THRS	32
78b4c3e9b5SBjoern A. Zeeb #define BRCMF_MSGBUF_UPDATE_RX_PTR_THRS		48
79b4c3e9b5SBjoern A. Zeeb 
80b4c3e9b5SBjoern A. Zeeb #define BRCMF_MAX_TXSTATUS_WAIT_RETRIES		10
81b4c3e9b5SBjoern A. Zeeb 
82b4c3e9b5SBjoern A. Zeeb struct msgbuf_common_hdr {
83b4c3e9b5SBjoern A. Zeeb 	u8				msgtype;
84b4c3e9b5SBjoern A. Zeeb 	u8				ifidx;
85b4c3e9b5SBjoern A. Zeeb 	u8				flags;
86b4c3e9b5SBjoern A. Zeeb 	u8				rsvd0;
87b4c3e9b5SBjoern A. Zeeb 	__le32				request_id;
88b4c3e9b5SBjoern A. Zeeb };
89b4c3e9b5SBjoern A. Zeeb 
90b4c3e9b5SBjoern A. Zeeb struct msgbuf_ioctl_req_hdr {
91b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
92b4c3e9b5SBjoern A. Zeeb 	__le32				cmd;
93b4c3e9b5SBjoern A. Zeeb 	__le16				trans_id;
94b4c3e9b5SBjoern A. Zeeb 	__le16				input_buf_len;
95b4c3e9b5SBjoern A. Zeeb 	__le16				output_buf_len;
96b4c3e9b5SBjoern A. Zeeb 	__le16				rsvd0[3];
97b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_buf_addr		req_buf_addr;
98b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd1[2];
99b4c3e9b5SBjoern A. Zeeb };
100b4c3e9b5SBjoern A. Zeeb 
101b4c3e9b5SBjoern A. Zeeb struct msgbuf_tx_msghdr {
102b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
103b4c3e9b5SBjoern A. Zeeb 	u8				txhdr[ETH_HLEN];
104b4c3e9b5SBjoern A. Zeeb 	u8				flags;
105b4c3e9b5SBjoern A. Zeeb 	u8				seg_cnt;
106b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_buf_addr		metadata_buf_addr;
107b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_buf_addr		data_buf_addr;
108b4c3e9b5SBjoern A. Zeeb 	__le16				metadata_buf_len;
109b4c3e9b5SBjoern A. Zeeb 	__le16				data_len;
110b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd0;
111b4c3e9b5SBjoern A. Zeeb };
112b4c3e9b5SBjoern A. Zeeb 
113b4c3e9b5SBjoern A. Zeeb struct msgbuf_rx_bufpost {
114b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
115b4c3e9b5SBjoern A. Zeeb 	__le16				metadata_buf_len;
116b4c3e9b5SBjoern A. Zeeb 	__le16				data_buf_len;
117b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd0;
118b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_buf_addr		metadata_buf_addr;
119b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_buf_addr		data_buf_addr;
120b4c3e9b5SBjoern A. Zeeb };
121b4c3e9b5SBjoern A. Zeeb 
122b4c3e9b5SBjoern A. Zeeb struct msgbuf_rx_ioctl_resp_or_event {
123b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
124b4c3e9b5SBjoern A. Zeeb 	__le16				host_buf_len;
125b4c3e9b5SBjoern A. Zeeb 	__le16				rsvd0[3];
126b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_buf_addr		host_buf_addr;
127b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd1[4];
128b4c3e9b5SBjoern A. Zeeb };
129b4c3e9b5SBjoern A. Zeeb 
130b4c3e9b5SBjoern A. Zeeb struct msgbuf_completion_hdr {
131b4c3e9b5SBjoern A. Zeeb 	__le16				status;
132b4c3e9b5SBjoern A. Zeeb 	__le16				flow_ring_id;
133b4c3e9b5SBjoern A. Zeeb };
134b4c3e9b5SBjoern A. Zeeb 
135b4c3e9b5SBjoern A. Zeeb /* Data struct for the MSGBUF_TYPE_GEN_STATUS */
136b4c3e9b5SBjoern A. Zeeb struct msgbuf_gen_status {
137b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
138b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_completion_hdr	compl_hdr;
139b4c3e9b5SBjoern A. Zeeb 	__le16				write_idx;
140b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd0[3];
141b4c3e9b5SBjoern A. Zeeb };
142b4c3e9b5SBjoern A. Zeeb 
143b4c3e9b5SBjoern A. Zeeb /* Data struct for the MSGBUF_TYPE_RING_STATUS */
144b4c3e9b5SBjoern A. Zeeb struct msgbuf_ring_status {
145b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
146b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_completion_hdr	compl_hdr;
147b4c3e9b5SBjoern A. Zeeb 	__le16				write_idx;
148b4c3e9b5SBjoern A. Zeeb 	__le16				rsvd0[5];
149b4c3e9b5SBjoern A. Zeeb };
150b4c3e9b5SBjoern A. Zeeb 
151b4c3e9b5SBjoern A. Zeeb struct msgbuf_rx_event {
152b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
153b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_completion_hdr	compl_hdr;
154b4c3e9b5SBjoern A. Zeeb 	__le16				event_data_len;
155b4c3e9b5SBjoern A. Zeeb 	__le16				seqnum;
156b4c3e9b5SBjoern A. Zeeb 	__le16				rsvd0[4];
157b4c3e9b5SBjoern A. Zeeb };
158b4c3e9b5SBjoern A. Zeeb 
159b4c3e9b5SBjoern A. Zeeb struct msgbuf_ioctl_resp_hdr {
160b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
161b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_completion_hdr	compl_hdr;
162b4c3e9b5SBjoern A. Zeeb 	__le16				resp_len;
163b4c3e9b5SBjoern A. Zeeb 	__le16				trans_id;
164b4c3e9b5SBjoern A. Zeeb 	__le32				cmd;
165b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd0;
166b4c3e9b5SBjoern A. Zeeb };
167b4c3e9b5SBjoern A. Zeeb 
168b4c3e9b5SBjoern A. Zeeb struct msgbuf_tx_status {
169b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
170b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_completion_hdr	compl_hdr;
171b4c3e9b5SBjoern A. Zeeb 	__le16				metadata_len;
172b4c3e9b5SBjoern A. Zeeb 	__le16				tx_status;
173b4c3e9b5SBjoern A. Zeeb };
174b4c3e9b5SBjoern A. Zeeb 
175b4c3e9b5SBjoern A. Zeeb struct msgbuf_rx_complete {
176b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
177b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_completion_hdr	compl_hdr;
178b4c3e9b5SBjoern A. Zeeb 	__le16				metadata_len;
179b4c3e9b5SBjoern A. Zeeb 	__le16				data_len;
180b4c3e9b5SBjoern A. Zeeb 	__le16				data_offset;
181b4c3e9b5SBjoern A. Zeeb 	__le16				flags;
182b4c3e9b5SBjoern A. Zeeb 	__le32				rx_status_0;
183b4c3e9b5SBjoern A. Zeeb 	__le32				rx_status_1;
184b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd0;
185b4c3e9b5SBjoern A. Zeeb };
186b4c3e9b5SBjoern A. Zeeb 
187b4c3e9b5SBjoern A. Zeeb struct msgbuf_tx_flowring_create_req {
188b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
189b4c3e9b5SBjoern A. Zeeb 	u8				da[ETH_ALEN];
190b4c3e9b5SBjoern A. Zeeb 	u8				sa[ETH_ALEN];
191b4c3e9b5SBjoern A. Zeeb 	u8				tid;
192b4c3e9b5SBjoern A. Zeeb 	u8				if_flags;
193b4c3e9b5SBjoern A. Zeeb 	__le16				flow_ring_id;
194b4c3e9b5SBjoern A. Zeeb 	u8				tc;
195b4c3e9b5SBjoern A. Zeeb 	u8				priority;
196b4c3e9b5SBjoern A. Zeeb 	__le16				int_vector;
197b4c3e9b5SBjoern A. Zeeb 	__le16				max_items;
198b4c3e9b5SBjoern A. Zeeb 	__le16				len_item;
199b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_buf_addr		flow_ring_addr;
200b4c3e9b5SBjoern A. Zeeb };
201b4c3e9b5SBjoern A. Zeeb 
202b4c3e9b5SBjoern A. Zeeb struct msgbuf_tx_flowring_delete_req {
203b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
204b4c3e9b5SBjoern A. Zeeb 	__le16				flow_ring_id;
205b4c3e9b5SBjoern A. Zeeb 	__le16				reason;
206b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd0[7];
207b4c3e9b5SBjoern A. Zeeb };
208b4c3e9b5SBjoern A. Zeeb 
209b4c3e9b5SBjoern A. Zeeb struct msgbuf_flowring_create_resp {
210b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
211b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_completion_hdr	compl_hdr;
212b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd0[3];
213b4c3e9b5SBjoern A. Zeeb };
214b4c3e9b5SBjoern A. Zeeb 
215b4c3e9b5SBjoern A. Zeeb struct msgbuf_flowring_delete_resp {
216b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
217b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_completion_hdr	compl_hdr;
218b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd0[3];
219b4c3e9b5SBjoern A. Zeeb };
220b4c3e9b5SBjoern A. Zeeb 
221b4c3e9b5SBjoern A. Zeeb struct msgbuf_flowring_flush_resp {
222b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr	msg;
223b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_completion_hdr	compl_hdr;
224b4c3e9b5SBjoern A. Zeeb 	__le32				rsvd0[3];
225b4c3e9b5SBjoern A. Zeeb };
226b4c3e9b5SBjoern A. Zeeb 
227b4c3e9b5SBjoern A. Zeeb struct brcmf_msgbuf_work_item {
228b4c3e9b5SBjoern A. Zeeb 	struct list_head queue;
229b4c3e9b5SBjoern A. Zeeb 	u32 flowid;
230b4c3e9b5SBjoern A. Zeeb 	int ifidx;
231b4c3e9b5SBjoern A. Zeeb 	u8 sa[ETH_ALEN];
232b4c3e9b5SBjoern A. Zeeb 	u8 da[ETH_ALEN];
233b4c3e9b5SBjoern A. Zeeb };
234b4c3e9b5SBjoern A. Zeeb 
235b4c3e9b5SBjoern A. Zeeb struct brcmf_msgbuf {
236b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr;
237b4c3e9b5SBjoern A. Zeeb 
238b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring **commonrings;
239b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring **flowrings;
240b4c3e9b5SBjoern A. Zeeb 	dma_addr_t *flowring_dma_handle;
241b4c3e9b5SBjoern A. Zeeb 
242b4c3e9b5SBjoern A. Zeeb 	u16 max_flowrings;
243b4c3e9b5SBjoern A. Zeeb 	u16 max_submissionrings;
244b4c3e9b5SBjoern A. Zeeb 	u16 max_completionrings;
245b4c3e9b5SBjoern A. Zeeb 
246b4c3e9b5SBjoern A. Zeeb 	u16 rx_dataoffset;
247b4c3e9b5SBjoern A. Zeeb 	u32 max_rxbufpost;
248b4c3e9b5SBjoern A. Zeeb 	u16 rx_metadata_offset;
249b4c3e9b5SBjoern A. Zeeb 	u32 rxbufpost;
250b4c3e9b5SBjoern A. Zeeb 
251b4c3e9b5SBjoern A. Zeeb 	u32 max_ioctlrespbuf;
252b4c3e9b5SBjoern A. Zeeb 	u32 cur_ioctlrespbuf;
253b4c3e9b5SBjoern A. Zeeb 	u32 max_eventbuf;
254b4c3e9b5SBjoern A. Zeeb 	u32 cur_eventbuf;
255b4c3e9b5SBjoern A. Zeeb 
256b4c3e9b5SBjoern A. Zeeb 	void *ioctbuf;
257b4c3e9b5SBjoern A. Zeeb 	dma_addr_t ioctbuf_handle;
258b4c3e9b5SBjoern A. Zeeb 	u32 ioctbuf_phys_hi;
259b4c3e9b5SBjoern A. Zeeb 	u32 ioctbuf_phys_lo;
260b4c3e9b5SBjoern A. Zeeb 	int ioctl_resp_status;
261b4c3e9b5SBjoern A. Zeeb 	u32 ioctl_resp_ret_len;
262b4c3e9b5SBjoern A. Zeeb 	u32 ioctl_resp_pktid;
263b4c3e9b5SBjoern A. Zeeb 
264b4c3e9b5SBjoern A. Zeeb 	u16 data_seq_no;
265b4c3e9b5SBjoern A. Zeeb 	u16 ioctl_seq_no;
266b4c3e9b5SBjoern A. Zeeb 	u32 reqid;
267b4c3e9b5SBjoern A. Zeeb 	wait_queue_head_t ioctl_resp_wait;
268b4c3e9b5SBjoern A. Zeeb 	bool ctl_completed;
269b4c3e9b5SBjoern A. Zeeb 
270b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_pktids *tx_pktids;
271b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_pktids *rx_pktids;
272b4c3e9b5SBjoern A. Zeeb 	struct brcmf_flowring *flow;
273b4c3e9b5SBjoern A. Zeeb 
274b4c3e9b5SBjoern A. Zeeb 	struct workqueue_struct *txflow_wq;
275b4c3e9b5SBjoern A. Zeeb 	struct work_struct txflow_work;
276b4c3e9b5SBjoern A. Zeeb 	unsigned long *flow_map;
277b4c3e9b5SBjoern A. Zeeb 	unsigned long *txstatus_done_map;
278b4c3e9b5SBjoern A. Zeeb 
279b4c3e9b5SBjoern A. Zeeb 	struct work_struct flowring_work;
280b4c3e9b5SBjoern A. Zeeb 	spinlock_t flowring_work_lock;
281b4c3e9b5SBjoern A. Zeeb 	struct list_head work_queue;
282b4c3e9b5SBjoern A. Zeeb };
283b4c3e9b5SBjoern A. Zeeb 
284b4c3e9b5SBjoern A. Zeeb struct brcmf_msgbuf_pktid {
285b4c3e9b5SBjoern A. Zeeb 	atomic_t  allocated;
286b4c3e9b5SBjoern A. Zeeb 	u16 data_offset;
287b4c3e9b5SBjoern A. Zeeb 	struct sk_buff *skb;
288b4c3e9b5SBjoern A. Zeeb 	dma_addr_t physaddr;
289b4c3e9b5SBjoern A. Zeeb };
290b4c3e9b5SBjoern A. Zeeb 
291b4c3e9b5SBjoern A. Zeeb struct brcmf_msgbuf_pktids {
292b4c3e9b5SBjoern A. Zeeb 	u32 array_size;
293b4c3e9b5SBjoern A. Zeeb 	u32 last_allocated_idx;
294b4c3e9b5SBjoern A. Zeeb 	enum dma_data_direction direction;
295b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_pktid *array;
296b4c3e9b5SBjoern A. Zeeb };
297b4c3e9b5SBjoern A. Zeeb 
298b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_rxbuf_ioctlresp_post(struct brcmf_msgbuf *msgbuf);
299b4c3e9b5SBjoern A. Zeeb 
300b4c3e9b5SBjoern A. Zeeb 
301b4c3e9b5SBjoern A. Zeeb static struct brcmf_msgbuf_pktids *
brcmf_msgbuf_init_pktids(u32 nr_array_entries,enum dma_data_direction direction)302b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_init_pktids(u32 nr_array_entries,
303b4c3e9b5SBjoern A. Zeeb 			 enum dma_data_direction direction)
304b4c3e9b5SBjoern A. Zeeb {
305b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_pktid *array;
306b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_pktids *pktids;
307b4c3e9b5SBjoern A. Zeeb 
308b4c3e9b5SBjoern A. Zeeb 	array = kcalloc(nr_array_entries, sizeof(*array), GFP_KERNEL);
309b4c3e9b5SBjoern A. Zeeb 	if (!array)
310b4c3e9b5SBjoern A. Zeeb 		return NULL;
311b4c3e9b5SBjoern A. Zeeb 
312b4c3e9b5SBjoern A. Zeeb 	pktids = kzalloc(sizeof(*pktids), GFP_KERNEL);
313b4c3e9b5SBjoern A. Zeeb 	if (!pktids) {
314b4c3e9b5SBjoern A. Zeeb 		kfree(array);
315b4c3e9b5SBjoern A. Zeeb 		return NULL;
316b4c3e9b5SBjoern A. Zeeb 	}
317b4c3e9b5SBjoern A. Zeeb 	pktids->array = array;
318b4c3e9b5SBjoern A. Zeeb 	pktids->array_size = nr_array_entries;
319b4c3e9b5SBjoern A. Zeeb 
320b4c3e9b5SBjoern A. Zeeb 	return pktids;
321b4c3e9b5SBjoern A. Zeeb }
322b4c3e9b5SBjoern A. Zeeb 
323b4c3e9b5SBjoern A. Zeeb 
324b4c3e9b5SBjoern A. Zeeb static int
brcmf_msgbuf_alloc_pktid(struct device * dev,struct brcmf_msgbuf_pktids * pktids,struct sk_buff * skb,u16 data_offset,dma_addr_t * physaddr,u32 * idx)325b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_alloc_pktid(struct device *dev,
326b4c3e9b5SBjoern A. Zeeb 			 struct brcmf_msgbuf_pktids *pktids,
327b4c3e9b5SBjoern A. Zeeb 			 struct sk_buff *skb, u16 data_offset,
328b4c3e9b5SBjoern A. Zeeb 			 dma_addr_t *physaddr, u32 *idx)
329b4c3e9b5SBjoern A. Zeeb {
330b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_pktid *array;
331b4c3e9b5SBjoern A. Zeeb 	u32 count;
332b4c3e9b5SBjoern A. Zeeb 
333b4c3e9b5SBjoern A. Zeeb 	array = pktids->array;
334b4c3e9b5SBjoern A. Zeeb 
335b4c3e9b5SBjoern A. Zeeb 	*physaddr = dma_map_single(dev, skb->data + data_offset,
336b4c3e9b5SBjoern A. Zeeb 				   skb->len - data_offset, pktids->direction);
337b4c3e9b5SBjoern A. Zeeb 
338b4c3e9b5SBjoern A. Zeeb 	if (dma_mapping_error(dev, *physaddr)) {
339b4c3e9b5SBjoern A. Zeeb 		brcmf_err("dma_map_single failed !!\n");
340b4c3e9b5SBjoern A. Zeeb 		return -ENOMEM;
341b4c3e9b5SBjoern A. Zeeb 	}
342b4c3e9b5SBjoern A. Zeeb 
343b4c3e9b5SBjoern A. Zeeb 	*idx = pktids->last_allocated_idx;
344b4c3e9b5SBjoern A. Zeeb 
345b4c3e9b5SBjoern A. Zeeb 	count = 0;
346b4c3e9b5SBjoern A. Zeeb 	do {
347b4c3e9b5SBjoern A. Zeeb 		(*idx)++;
348b4c3e9b5SBjoern A. Zeeb 		if (*idx == pktids->array_size)
349b4c3e9b5SBjoern A. Zeeb 			*idx = 0;
350b4c3e9b5SBjoern A. Zeeb 		if (array[*idx].allocated.counter == 0)
351b4c3e9b5SBjoern A. Zeeb 			if (atomic_cmpxchg(&array[*idx].allocated, 0, 1) == 0)
352b4c3e9b5SBjoern A. Zeeb 				break;
353b4c3e9b5SBjoern A. Zeeb 		count++;
354b4c3e9b5SBjoern A. Zeeb 	} while (count < pktids->array_size);
355b4c3e9b5SBjoern A. Zeeb 
356b4c3e9b5SBjoern A. Zeeb 	if (count == pktids->array_size) {
357b4c3e9b5SBjoern A. Zeeb 		dma_unmap_single(dev, *physaddr, skb->len - data_offset,
358b4c3e9b5SBjoern A. Zeeb 				 pktids->direction);
359b4c3e9b5SBjoern A. Zeeb 		return -ENOMEM;
360b4c3e9b5SBjoern A. Zeeb 	}
361b4c3e9b5SBjoern A. Zeeb 
362b4c3e9b5SBjoern A. Zeeb 	array[*idx].data_offset = data_offset;
363b4c3e9b5SBjoern A. Zeeb 	array[*idx].physaddr = *physaddr;
364b4c3e9b5SBjoern A. Zeeb 	array[*idx].skb = skb;
365b4c3e9b5SBjoern A. Zeeb 
366b4c3e9b5SBjoern A. Zeeb 	pktids->last_allocated_idx = *idx;
367b4c3e9b5SBjoern A. Zeeb 
368b4c3e9b5SBjoern A. Zeeb 	return 0;
369b4c3e9b5SBjoern A. Zeeb }
370b4c3e9b5SBjoern A. Zeeb 
371b4c3e9b5SBjoern A. Zeeb 
372b4c3e9b5SBjoern A. Zeeb static struct sk_buff *
brcmf_msgbuf_get_pktid(struct device * dev,struct brcmf_msgbuf_pktids * pktids,u32 idx)373b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_get_pktid(struct device *dev, struct brcmf_msgbuf_pktids *pktids,
374b4c3e9b5SBjoern A. Zeeb 		       u32 idx)
375b4c3e9b5SBjoern A. Zeeb {
376b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_pktid *pktid;
377b4c3e9b5SBjoern A. Zeeb 	struct sk_buff *skb;
378b4c3e9b5SBjoern A. Zeeb 
379b4c3e9b5SBjoern A. Zeeb 	if (idx >= pktids->array_size) {
380b4c3e9b5SBjoern A. Zeeb 		brcmf_err("Invalid packet id %d (max %d)\n", idx,
381b4c3e9b5SBjoern A. Zeeb 			  pktids->array_size);
382b4c3e9b5SBjoern A. Zeeb 		return NULL;
383b4c3e9b5SBjoern A. Zeeb 	}
384b4c3e9b5SBjoern A. Zeeb 	if (pktids->array[idx].allocated.counter) {
385b4c3e9b5SBjoern A. Zeeb 		pktid = &pktids->array[idx];
386b4c3e9b5SBjoern A. Zeeb 		dma_unmap_single(dev, pktid->physaddr,
387b4c3e9b5SBjoern A. Zeeb 				 pktid->skb->len - pktid->data_offset,
388b4c3e9b5SBjoern A. Zeeb 				 pktids->direction);
389b4c3e9b5SBjoern A. Zeeb 		skb = pktid->skb;
390b4c3e9b5SBjoern A. Zeeb 		pktid->allocated.counter = 0;
391b4c3e9b5SBjoern A. Zeeb 		return skb;
392b4c3e9b5SBjoern A. Zeeb 	} else {
393b4c3e9b5SBjoern A. Zeeb 		brcmf_err("Invalid packet id %d (not in use)\n", idx);
394b4c3e9b5SBjoern A. Zeeb 	}
395b4c3e9b5SBjoern A. Zeeb 
396b4c3e9b5SBjoern A. Zeeb 	return NULL;
397b4c3e9b5SBjoern A. Zeeb }
398b4c3e9b5SBjoern A. Zeeb 
399b4c3e9b5SBjoern A. Zeeb 
400b4c3e9b5SBjoern A. Zeeb static void
brcmf_msgbuf_release_array(struct device * dev,struct brcmf_msgbuf_pktids * pktids)401b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_release_array(struct device *dev,
402b4c3e9b5SBjoern A. Zeeb 			   struct brcmf_msgbuf_pktids *pktids)
403b4c3e9b5SBjoern A. Zeeb {
404b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_pktid *array;
405b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_pktid *pktid;
406b4c3e9b5SBjoern A. Zeeb 	u32 count;
407b4c3e9b5SBjoern A. Zeeb 
408b4c3e9b5SBjoern A. Zeeb 	array = pktids->array;
409b4c3e9b5SBjoern A. Zeeb 	count = 0;
410b4c3e9b5SBjoern A. Zeeb 	do {
411b4c3e9b5SBjoern A. Zeeb 		if (array[count].allocated.counter) {
412b4c3e9b5SBjoern A. Zeeb 			pktid = &array[count];
413b4c3e9b5SBjoern A. Zeeb 			dma_unmap_single(dev, pktid->physaddr,
414b4c3e9b5SBjoern A. Zeeb 					 pktid->skb->len - pktid->data_offset,
415b4c3e9b5SBjoern A. Zeeb 					 pktids->direction);
416b4c3e9b5SBjoern A. Zeeb 			brcmu_pkt_buf_free_skb(pktid->skb);
417b4c3e9b5SBjoern A. Zeeb 		}
418b4c3e9b5SBjoern A. Zeeb 		count++;
419b4c3e9b5SBjoern A. Zeeb 	} while (count < pktids->array_size);
420b4c3e9b5SBjoern A. Zeeb 
421b4c3e9b5SBjoern A. Zeeb 	kfree(array);
422b4c3e9b5SBjoern A. Zeeb 	kfree(pktids);
423b4c3e9b5SBjoern A. Zeeb }
424b4c3e9b5SBjoern A. Zeeb 
425b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_release_pktids(struct brcmf_msgbuf * msgbuf)426b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_release_pktids(struct brcmf_msgbuf *msgbuf)
427b4c3e9b5SBjoern A. Zeeb {
428b4c3e9b5SBjoern A. Zeeb 	if (msgbuf->rx_pktids)
429b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_release_array(msgbuf->drvr->bus_if->dev,
430b4c3e9b5SBjoern A. Zeeb 					   msgbuf->rx_pktids);
431b4c3e9b5SBjoern A. Zeeb 	if (msgbuf->tx_pktids)
432b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_release_array(msgbuf->drvr->bus_if->dev,
433b4c3e9b5SBjoern A. Zeeb 					   msgbuf->tx_pktids);
434b4c3e9b5SBjoern A. Zeeb }
435b4c3e9b5SBjoern A. Zeeb 
436b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_tx_ioctl(struct brcmf_pub * drvr,int ifidx,uint cmd,void * buf,uint len)437b4c3e9b5SBjoern A. Zeeb static int brcmf_msgbuf_tx_ioctl(struct brcmf_pub *drvr, int ifidx,
438*902136e0SBjoern A. Zeeb #if defined(__linux__)
439b4c3e9b5SBjoern A. Zeeb 				 uint cmd, void *buf, uint len)
440*902136e0SBjoern A. Zeeb #elif defined(__FreeBSD__)
441*902136e0SBjoern A. Zeeb 				 uint cmd, const void *buf, uint len)
442*902136e0SBjoern A. Zeeb #endif
443b4c3e9b5SBjoern A. Zeeb {
444b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
445b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring;
446b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_ioctl_req_hdr *request;
447b4c3e9b5SBjoern A. Zeeb 	u16 buf_len;
448b4c3e9b5SBjoern A. Zeeb 	void *ret_ptr;
449b4c3e9b5SBjoern A. Zeeb 	int err;
450b4c3e9b5SBjoern A. Zeeb 
451b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT];
452b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_lock(commonring);
453b4c3e9b5SBjoern A. Zeeb 	ret_ptr = brcmf_commonring_reserve_for_write(commonring);
454b4c3e9b5SBjoern A. Zeeb 	if (!ret_ptr) {
455b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Failed to reserve space in commonring\n");
456b4c3e9b5SBjoern A. Zeeb 		brcmf_commonring_unlock(commonring);
457b4c3e9b5SBjoern A. Zeeb 		return -ENOMEM;
458b4c3e9b5SBjoern A. Zeeb 	}
459b4c3e9b5SBjoern A. Zeeb 
460b4c3e9b5SBjoern A. Zeeb 	msgbuf->reqid++;
461b4c3e9b5SBjoern A. Zeeb 
462b4c3e9b5SBjoern A. Zeeb 	request = (struct msgbuf_ioctl_req_hdr *)ret_ptr;
463b4c3e9b5SBjoern A. Zeeb 	request->msg.msgtype = MSGBUF_TYPE_IOCTLPTR_REQ;
464b4c3e9b5SBjoern A. Zeeb 	request->msg.ifidx = (u8)ifidx;
465b4c3e9b5SBjoern A. Zeeb 	request->msg.flags = 0;
466b4c3e9b5SBjoern A. Zeeb 	request->msg.request_id = cpu_to_le32(BRCMF_IOCTL_REQ_PKTID);
467b4c3e9b5SBjoern A. Zeeb 	request->cmd = cpu_to_le32(cmd);
468b4c3e9b5SBjoern A. Zeeb 	request->output_buf_len = cpu_to_le16(len);
469b4c3e9b5SBjoern A. Zeeb 	request->trans_id = cpu_to_le16(msgbuf->reqid);
470b4c3e9b5SBjoern A. Zeeb 
471b4c3e9b5SBjoern A. Zeeb 	buf_len = min_t(u16, len, BRCMF_TX_IOCTL_MAX_MSG_SIZE);
472b4c3e9b5SBjoern A. Zeeb 	request->input_buf_len = cpu_to_le16(buf_len);
473b4c3e9b5SBjoern A. Zeeb 	request->req_buf_addr.high_addr = cpu_to_le32(msgbuf->ioctbuf_phys_hi);
474b4c3e9b5SBjoern A. Zeeb 	request->req_buf_addr.low_addr = cpu_to_le32(msgbuf->ioctbuf_phys_lo);
475b4c3e9b5SBjoern A. Zeeb 	if (buf)
476b4c3e9b5SBjoern A. Zeeb 		memcpy(msgbuf->ioctbuf, buf, buf_len);
477b4c3e9b5SBjoern A. Zeeb 	else
478b4c3e9b5SBjoern A. Zeeb 		memset(msgbuf->ioctbuf, 0, buf_len);
479b4c3e9b5SBjoern A. Zeeb 
480b4c3e9b5SBjoern A. Zeeb 	err = brcmf_commonring_write_complete(commonring);
481b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_unlock(commonring);
482b4c3e9b5SBjoern A. Zeeb 
483b4c3e9b5SBjoern A. Zeeb 	return err;
484b4c3e9b5SBjoern A. Zeeb }
485b4c3e9b5SBjoern A. Zeeb 
486b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_ioctl_resp_wait(struct brcmf_msgbuf * msgbuf)487b4c3e9b5SBjoern A. Zeeb static int brcmf_msgbuf_ioctl_resp_wait(struct brcmf_msgbuf *msgbuf)
488b4c3e9b5SBjoern A. Zeeb {
489b4c3e9b5SBjoern A. Zeeb 	return wait_event_timeout(msgbuf->ioctl_resp_wait,
490b4c3e9b5SBjoern A. Zeeb 				  msgbuf->ctl_completed,
491b4c3e9b5SBjoern A. Zeeb 				  MSGBUF_IOCTL_RESP_TIMEOUT);
492b4c3e9b5SBjoern A. Zeeb }
493b4c3e9b5SBjoern A. Zeeb 
494b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_ioctl_resp_wake(struct brcmf_msgbuf * msgbuf)495b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_ioctl_resp_wake(struct brcmf_msgbuf *msgbuf)
496b4c3e9b5SBjoern A. Zeeb {
497b4c3e9b5SBjoern A. Zeeb 	msgbuf->ctl_completed = true;
498b4c3e9b5SBjoern A. Zeeb 	wake_up(&msgbuf->ioctl_resp_wait);
499b4c3e9b5SBjoern A. Zeeb }
500b4c3e9b5SBjoern A. Zeeb 
501b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_query_dcmd(struct brcmf_pub * drvr,int ifidx,uint cmd,void * buf,uint len,int * fwerr)502b4c3e9b5SBjoern A. Zeeb static int brcmf_msgbuf_query_dcmd(struct brcmf_pub *drvr, int ifidx,
503b4c3e9b5SBjoern A. Zeeb 				   uint cmd, void *buf, uint len, int *fwerr)
504b4c3e9b5SBjoern A. Zeeb {
505b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
506b4c3e9b5SBjoern A. Zeeb 	struct sk_buff *skb = NULL;
507b4c3e9b5SBjoern A. Zeeb 	int timeout;
508b4c3e9b5SBjoern A. Zeeb 	int err;
509b4c3e9b5SBjoern A. Zeeb 
510b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(MSGBUF, "ifidx=%d, cmd=%d, len=%d\n", ifidx, cmd, len);
511b4c3e9b5SBjoern A. Zeeb 	*fwerr = 0;
512b4c3e9b5SBjoern A. Zeeb 	msgbuf->ctl_completed = false;
513b4c3e9b5SBjoern A. Zeeb 	err = brcmf_msgbuf_tx_ioctl(drvr, ifidx, cmd, buf, len);
514b4c3e9b5SBjoern A. Zeeb 	if (err)
515b4c3e9b5SBjoern A. Zeeb 		return err;
516b4c3e9b5SBjoern A. Zeeb 
517b4c3e9b5SBjoern A. Zeeb 	timeout = brcmf_msgbuf_ioctl_resp_wait(msgbuf);
518b4c3e9b5SBjoern A. Zeeb 	if (!timeout) {
519b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Timeout on response for query command\n");
520b4c3e9b5SBjoern A. Zeeb 		return -EIO;
521b4c3e9b5SBjoern A. Zeeb 	}
522b4c3e9b5SBjoern A. Zeeb 
523b4c3e9b5SBjoern A. Zeeb 	skb = brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev,
524b4c3e9b5SBjoern A. Zeeb 				     msgbuf->rx_pktids,
525b4c3e9b5SBjoern A. Zeeb 				     msgbuf->ioctl_resp_pktid);
526b4c3e9b5SBjoern A. Zeeb 	if (msgbuf->ioctl_resp_ret_len != 0) {
527b4c3e9b5SBjoern A. Zeeb 		if (!skb)
528b4c3e9b5SBjoern A. Zeeb 			return -EBADF;
529b4c3e9b5SBjoern A. Zeeb 
530b4c3e9b5SBjoern A. Zeeb 		memcpy(buf, skb->data, (len < msgbuf->ioctl_resp_ret_len) ?
531b4c3e9b5SBjoern A. Zeeb 				       len : msgbuf->ioctl_resp_ret_len);
532b4c3e9b5SBjoern A. Zeeb 	}
533b4c3e9b5SBjoern A. Zeeb 	brcmu_pkt_buf_free_skb(skb);
534b4c3e9b5SBjoern A. Zeeb 
535b4c3e9b5SBjoern A. Zeeb 	*fwerr = msgbuf->ioctl_resp_status;
536b4c3e9b5SBjoern A. Zeeb 	return 0;
537b4c3e9b5SBjoern A. Zeeb }
538b4c3e9b5SBjoern A. Zeeb 
539b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_set_dcmd(struct brcmf_pub * drvr,int ifidx,uint cmd,void * buf,uint len,int * fwerr)540b4c3e9b5SBjoern A. Zeeb static int brcmf_msgbuf_set_dcmd(struct brcmf_pub *drvr, int ifidx,
541b4c3e9b5SBjoern A. Zeeb 				 uint cmd, void *buf, uint len, int *fwerr)
542b4c3e9b5SBjoern A. Zeeb {
543b4c3e9b5SBjoern A. Zeeb 	return brcmf_msgbuf_query_dcmd(drvr, ifidx, cmd, buf, len, fwerr);
544b4c3e9b5SBjoern A. Zeeb }
545b4c3e9b5SBjoern A. Zeeb 
546b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_hdrpull(struct brcmf_pub * drvr,bool do_fws,struct sk_buff * skb,struct brcmf_if ** ifp)547b4c3e9b5SBjoern A. Zeeb static int brcmf_msgbuf_hdrpull(struct brcmf_pub *drvr, bool do_fws,
548b4c3e9b5SBjoern A. Zeeb 				struct sk_buff *skb, struct brcmf_if **ifp)
549b4c3e9b5SBjoern A. Zeeb {
550b4c3e9b5SBjoern A. Zeeb 	return -ENODEV;
551b4c3e9b5SBjoern A. Zeeb }
552b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_rxreorder(struct brcmf_if * ifp,struct sk_buff * skb)553b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_rxreorder(struct brcmf_if *ifp, struct sk_buff *skb)
554b4c3e9b5SBjoern A. Zeeb {
555b4c3e9b5SBjoern A. Zeeb }
556b4c3e9b5SBjoern A. Zeeb 
557b4c3e9b5SBjoern A. Zeeb static void
brcmf_msgbuf_remove_flowring(struct brcmf_msgbuf * msgbuf,u16 flowid)558b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_remove_flowring(struct brcmf_msgbuf *msgbuf, u16 flowid)
559b4c3e9b5SBjoern A. Zeeb {
560b4c3e9b5SBjoern A. Zeeb 	u32 dma_sz;
561b4c3e9b5SBjoern A. Zeeb 	void *dma_buf;
562b4c3e9b5SBjoern A. Zeeb 
563b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(MSGBUF, "Removing flowring %d\n", flowid);
564b4c3e9b5SBjoern A. Zeeb 
565b4c3e9b5SBjoern A. Zeeb 	dma_sz = BRCMF_H2D_TXFLOWRING_MAX_ITEM * BRCMF_H2D_TXFLOWRING_ITEMSIZE;
566b4c3e9b5SBjoern A. Zeeb 	dma_buf = msgbuf->flowrings[flowid]->buf_addr;
567b4c3e9b5SBjoern A. Zeeb 	dma_free_coherent(msgbuf->drvr->bus_if->dev, dma_sz, dma_buf,
568b4c3e9b5SBjoern A. Zeeb 			  msgbuf->flowring_dma_handle[flowid]);
569b4c3e9b5SBjoern A. Zeeb 
570b4c3e9b5SBjoern A. Zeeb 	brcmf_flowring_delete(msgbuf->flow, flowid);
571b4c3e9b5SBjoern A. Zeeb }
572b4c3e9b5SBjoern A. Zeeb 
573b4c3e9b5SBjoern A. Zeeb 
574b4c3e9b5SBjoern A. Zeeb static struct brcmf_msgbuf_work_item *
brcmf_msgbuf_dequeue_work(struct brcmf_msgbuf * msgbuf)575b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_dequeue_work(struct brcmf_msgbuf *msgbuf)
576b4c3e9b5SBjoern A. Zeeb {
577b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_work_item *work = NULL;
578b4c3e9b5SBjoern A. Zeeb 	ulong flags;
579b4c3e9b5SBjoern A. Zeeb 
580b4c3e9b5SBjoern A. Zeeb 	spin_lock_irqsave(&msgbuf->flowring_work_lock, flags);
581b4c3e9b5SBjoern A. Zeeb 	if (!list_empty(&msgbuf->work_queue)) {
582b4c3e9b5SBjoern A. Zeeb 		work = list_first_entry(&msgbuf->work_queue,
583b4c3e9b5SBjoern A. Zeeb 					struct brcmf_msgbuf_work_item, queue);
584b4c3e9b5SBjoern A. Zeeb 		list_del(&work->queue);
585b4c3e9b5SBjoern A. Zeeb 	}
586b4c3e9b5SBjoern A. Zeeb 	spin_unlock_irqrestore(&msgbuf->flowring_work_lock, flags);
587b4c3e9b5SBjoern A. Zeeb 
588b4c3e9b5SBjoern A. Zeeb 	return work;
589b4c3e9b5SBjoern A. Zeeb }
590b4c3e9b5SBjoern A. Zeeb 
591b4c3e9b5SBjoern A. Zeeb 
592b4c3e9b5SBjoern A. Zeeb static u32
brcmf_msgbuf_flowring_create_worker(struct brcmf_msgbuf * msgbuf,struct brcmf_msgbuf_work_item * work)593b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_flowring_create_worker(struct brcmf_msgbuf *msgbuf,
594b4c3e9b5SBjoern A. Zeeb 				    struct brcmf_msgbuf_work_item *work)
595b4c3e9b5SBjoern A. Zeeb {
596b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
597b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_tx_flowring_create_req *create;
598b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring;
599b4c3e9b5SBjoern A. Zeeb 	void *ret_ptr;
600b4c3e9b5SBjoern A. Zeeb 	u32 flowid;
601b4c3e9b5SBjoern A. Zeeb 	void *dma_buf;
602b4c3e9b5SBjoern A. Zeeb 	u32 dma_sz;
603b4c3e9b5SBjoern A. Zeeb 	u64 address;
604b4c3e9b5SBjoern A. Zeeb 	int err;
605b4c3e9b5SBjoern A. Zeeb 
606b4c3e9b5SBjoern A. Zeeb 	flowid = work->flowid;
607b4c3e9b5SBjoern A. Zeeb 	dma_sz = BRCMF_H2D_TXFLOWRING_MAX_ITEM * BRCMF_H2D_TXFLOWRING_ITEMSIZE;
608b4c3e9b5SBjoern A. Zeeb 	dma_buf = dma_alloc_coherent(msgbuf->drvr->bus_if->dev, dma_sz,
609b4c3e9b5SBjoern A. Zeeb 				     &msgbuf->flowring_dma_handle[flowid],
610b4c3e9b5SBjoern A. Zeeb 				     GFP_KERNEL);
611b4c3e9b5SBjoern A. Zeeb 	if (!dma_buf) {
612b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "dma_alloc_coherent failed\n");
613b4c3e9b5SBjoern A. Zeeb 		brcmf_flowring_delete(msgbuf->flow, flowid);
614b4c3e9b5SBjoern A. Zeeb 		return BRCMF_FLOWRING_INVALID_ID;
615b4c3e9b5SBjoern A. Zeeb 	}
616b4c3e9b5SBjoern A. Zeeb 
617b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_config(msgbuf->flowrings[flowid],
618b4c3e9b5SBjoern A. Zeeb 				BRCMF_H2D_TXFLOWRING_MAX_ITEM,
619b4c3e9b5SBjoern A. Zeeb 				BRCMF_H2D_TXFLOWRING_ITEMSIZE, dma_buf);
620b4c3e9b5SBjoern A. Zeeb 
621b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT];
622b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_lock(commonring);
623b4c3e9b5SBjoern A. Zeeb 	ret_ptr = brcmf_commonring_reserve_for_write(commonring);
624b4c3e9b5SBjoern A. Zeeb 	if (!ret_ptr) {
625b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Failed to reserve space in commonring\n");
626b4c3e9b5SBjoern A. Zeeb 		brcmf_commonring_unlock(commonring);
627b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_remove_flowring(msgbuf, flowid);
628b4c3e9b5SBjoern A. Zeeb 		return BRCMF_FLOWRING_INVALID_ID;
629b4c3e9b5SBjoern A. Zeeb 	}
630b4c3e9b5SBjoern A. Zeeb 
631b4c3e9b5SBjoern A. Zeeb 	create = (struct msgbuf_tx_flowring_create_req *)ret_ptr;
632b4c3e9b5SBjoern A. Zeeb 	create->msg.msgtype = MSGBUF_TYPE_FLOW_RING_CREATE;
633b4c3e9b5SBjoern A. Zeeb 	create->msg.ifidx = work->ifidx;
634b4c3e9b5SBjoern A. Zeeb 	create->msg.request_id = 0;
635b4c3e9b5SBjoern A. Zeeb 	create->tid = brcmf_flowring_tid(msgbuf->flow, flowid);
636b4c3e9b5SBjoern A. Zeeb 	create->flow_ring_id = cpu_to_le16(flowid +
637b4c3e9b5SBjoern A. Zeeb 					   BRCMF_H2D_MSGRING_FLOWRING_IDSTART);
638b4c3e9b5SBjoern A. Zeeb 	memcpy(create->sa, work->sa, ETH_ALEN);
639b4c3e9b5SBjoern A. Zeeb 	memcpy(create->da, work->da, ETH_ALEN);
640b4c3e9b5SBjoern A. Zeeb 	address = (u64)msgbuf->flowring_dma_handle[flowid];
641b4c3e9b5SBjoern A. Zeeb 	create->flow_ring_addr.high_addr = cpu_to_le32(address >> 32);
642b4c3e9b5SBjoern A. Zeeb 	create->flow_ring_addr.low_addr = cpu_to_le32(address & 0xffffffff);
643b4c3e9b5SBjoern A. Zeeb 	create->max_items = cpu_to_le16(BRCMF_H2D_TXFLOWRING_MAX_ITEM);
644b4c3e9b5SBjoern A. Zeeb 	create->len_item = cpu_to_le16(BRCMF_H2D_TXFLOWRING_ITEMSIZE);
645b4c3e9b5SBjoern A. Zeeb 
646b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(MSGBUF, "Send Flow Create Req flow ID %d for peer %pM prio %d ifindex %d\n",
647b4c3e9b5SBjoern A. Zeeb 		  flowid, work->da, create->tid, work->ifidx);
648b4c3e9b5SBjoern A. Zeeb 
649b4c3e9b5SBjoern A. Zeeb 	err = brcmf_commonring_write_complete(commonring);
650b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_unlock(commonring);
651b4c3e9b5SBjoern A. Zeeb 	if (err) {
652b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Failed to write commonring\n");
653b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_remove_flowring(msgbuf, flowid);
654b4c3e9b5SBjoern A. Zeeb 		return BRCMF_FLOWRING_INVALID_ID;
655b4c3e9b5SBjoern A. Zeeb 	}
656b4c3e9b5SBjoern A. Zeeb 
657b4c3e9b5SBjoern A. Zeeb 	return flowid;
658b4c3e9b5SBjoern A. Zeeb }
659b4c3e9b5SBjoern A. Zeeb 
660b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_flowring_worker(struct work_struct * work)661b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_flowring_worker(struct work_struct *work)
662b4c3e9b5SBjoern A. Zeeb {
663b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf;
664b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_work_item *create;
665b4c3e9b5SBjoern A. Zeeb 
666b4c3e9b5SBjoern A. Zeeb 	msgbuf = container_of(work, struct brcmf_msgbuf, flowring_work);
667b4c3e9b5SBjoern A. Zeeb 
668b4c3e9b5SBjoern A. Zeeb 	while ((create = brcmf_msgbuf_dequeue_work(msgbuf))) {
669b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_flowring_create_worker(msgbuf, create);
670b4c3e9b5SBjoern A. Zeeb 		kfree(create);
671b4c3e9b5SBjoern A. Zeeb 	}
672b4c3e9b5SBjoern A. Zeeb }
673b4c3e9b5SBjoern A. Zeeb 
674b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_flowring_create(struct brcmf_msgbuf * msgbuf,int ifidx,struct sk_buff * skb)675b4c3e9b5SBjoern A. Zeeb static u32 brcmf_msgbuf_flowring_create(struct brcmf_msgbuf *msgbuf, int ifidx,
676b4c3e9b5SBjoern A. Zeeb 					struct sk_buff *skb)
677b4c3e9b5SBjoern A. Zeeb {
678b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_work_item *create;
679b4c3e9b5SBjoern A. Zeeb 	struct ethhdr *eh = (struct ethhdr *)(skb->data);
680b4c3e9b5SBjoern A. Zeeb 	u32 flowid;
681b4c3e9b5SBjoern A. Zeeb 	ulong flags;
682b4c3e9b5SBjoern A. Zeeb 
683b4c3e9b5SBjoern A. Zeeb 	create = kzalloc(sizeof(*create), GFP_ATOMIC);
684b4c3e9b5SBjoern A. Zeeb 	if (create == NULL)
685b4c3e9b5SBjoern A. Zeeb 		return BRCMF_FLOWRING_INVALID_ID;
686b4c3e9b5SBjoern A. Zeeb 
687b4c3e9b5SBjoern A. Zeeb 	flowid = brcmf_flowring_create(msgbuf->flow, eh->h_dest,
688b4c3e9b5SBjoern A. Zeeb 				       skb->priority, ifidx);
689b4c3e9b5SBjoern A. Zeeb 	if (flowid == BRCMF_FLOWRING_INVALID_ID) {
690b4c3e9b5SBjoern A. Zeeb 		kfree(create);
691b4c3e9b5SBjoern A. Zeeb 		return flowid;
692b4c3e9b5SBjoern A. Zeeb 	}
693b4c3e9b5SBjoern A. Zeeb 
694b4c3e9b5SBjoern A. Zeeb 	create->flowid = flowid;
695b4c3e9b5SBjoern A. Zeeb 	create->ifidx = ifidx;
696b4c3e9b5SBjoern A. Zeeb 	memcpy(create->sa, eh->h_source, ETH_ALEN);
697b4c3e9b5SBjoern A. Zeeb 	memcpy(create->da, eh->h_dest, ETH_ALEN);
698b4c3e9b5SBjoern A. Zeeb 
699b4c3e9b5SBjoern A. Zeeb 	spin_lock_irqsave(&msgbuf->flowring_work_lock, flags);
700b4c3e9b5SBjoern A. Zeeb 	list_add_tail(&create->queue, &msgbuf->work_queue);
701b4c3e9b5SBjoern A. Zeeb 	spin_unlock_irqrestore(&msgbuf->flowring_work_lock, flags);
702b4c3e9b5SBjoern A. Zeeb 	schedule_work(&msgbuf->flowring_work);
703b4c3e9b5SBjoern A. Zeeb 
704b4c3e9b5SBjoern A. Zeeb 	return flowid;
705b4c3e9b5SBjoern A. Zeeb }
706b4c3e9b5SBjoern A. Zeeb 
707b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_txflow(struct brcmf_msgbuf * msgbuf,u16 flowid)708b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_txflow(struct brcmf_msgbuf *msgbuf, u16 flowid)
709b4c3e9b5SBjoern A. Zeeb {
710b4c3e9b5SBjoern A. Zeeb 	struct brcmf_flowring *flow = msgbuf->flow;
711b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
712b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring;
713b4c3e9b5SBjoern A. Zeeb 	void *ret_ptr;
714b4c3e9b5SBjoern A. Zeeb 	u32 count;
715b4c3e9b5SBjoern A. Zeeb 	struct sk_buff *skb;
716b4c3e9b5SBjoern A. Zeeb 	dma_addr_t physaddr;
717b4c3e9b5SBjoern A. Zeeb 	u32 pktid;
718b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_tx_msghdr *tx_msghdr;
719b4c3e9b5SBjoern A. Zeeb 	u64 address;
720b4c3e9b5SBjoern A. Zeeb 
721b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->flowrings[flowid];
722b4c3e9b5SBjoern A. Zeeb 	if (!brcmf_commonring_write_available(commonring))
723b4c3e9b5SBjoern A. Zeeb 		return;
724b4c3e9b5SBjoern A. Zeeb 
725b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_lock(commonring);
726b4c3e9b5SBjoern A. Zeeb 
727b4c3e9b5SBjoern A. Zeeb 	count = BRCMF_MSGBUF_TX_FLUSH_CNT2 - BRCMF_MSGBUF_TX_FLUSH_CNT1;
728b4c3e9b5SBjoern A. Zeeb 	while (brcmf_flowring_qlen(flow, flowid)) {
729b4c3e9b5SBjoern A. Zeeb 		skb = brcmf_flowring_dequeue(flow, flowid);
730b4c3e9b5SBjoern A. Zeeb 		if (skb == NULL) {
731b4c3e9b5SBjoern A. Zeeb 			bphy_err(drvr, "No SKB, but qlen %d\n",
732b4c3e9b5SBjoern A. Zeeb 				 brcmf_flowring_qlen(flow, flowid));
733b4c3e9b5SBjoern A. Zeeb 			break;
734b4c3e9b5SBjoern A. Zeeb 		}
735b4c3e9b5SBjoern A. Zeeb 		skb_orphan(skb);
736b4c3e9b5SBjoern A. Zeeb 		if (brcmf_msgbuf_alloc_pktid(msgbuf->drvr->bus_if->dev,
737b4c3e9b5SBjoern A. Zeeb 					     msgbuf->tx_pktids, skb, ETH_HLEN,
738b4c3e9b5SBjoern A. Zeeb 					     &physaddr, &pktid)) {
739b4c3e9b5SBjoern A. Zeeb 			brcmf_flowring_reinsert(flow, flowid, skb);
740b4c3e9b5SBjoern A. Zeeb 			bphy_err(drvr, "No PKTID available !!\n");
741b4c3e9b5SBjoern A. Zeeb 			break;
742b4c3e9b5SBjoern A. Zeeb 		}
743b4c3e9b5SBjoern A. Zeeb 		ret_ptr = brcmf_commonring_reserve_for_write(commonring);
744b4c3e9b5SBjoern A. Zeeb 		if (!ret_ptr) {
745b4c3e9b5SBjoern A. Zeeb 			brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev,
746b4c3e9b5SBjoern A. Zeeb 					       msgbuf->tx_pktids, pktid);
747b4c3e9b5SBjoern A. Zeeb 			brcmf_flowring_reinsert(flow, flowid, skb);
748b4c3e9b5SBjoern A. Zeeb 			break;
749b4c3e9b5SBjoern A. Zeeb 		}
750b4c3e9b5SBjoern A. Zeeb 		count++;
751b4c3e9b5SBjoern A. Zeeb 
752b4c3e9b5SBjoern A. Zeeb 		tx_msghdr = (struct msgbuf_tx_msghdr *)ret_ptr;
753b4c3e9b5SBjoern A. Zeeb 
754b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->msg.msgtype = MSGBUF_TYPE_TX_POST;
755b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->msg.request_id = cpu_to_le32(pktid + 1);
756b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->msg.ifidx = brcmf_flowring_ifidx_get(flow, flowid);
757b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->flags = BRCMF_MSGBUF_PKT_FLAGS_FRAME_802_3;
758b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->flags |= (skb->priority & 0x07) <<
759b4c3e9b5SBjoern A. Zeeb 				    BRCMF_MSGBUF_PKT_FLAGS_PRIO_SHIFT;
760b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->seg_cnt = 1;
761b4c3e9b5SBjoern A. Zeeb 		memcpy(tx_msghdr->txhdr, skb->data, ETH_HLEN);
762b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->data_len = cpu_to_le16(skb->len - ETH_HLEN);
763b4c3e9b5SBjoern A. Zeeb 		address = (u64)physaddr;
764b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->data_buf_addr.high_addr = cpu_to_le32(address >> 32);
765b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->data_buf_addr.low_addr =
766b4c3e9b5SBjoern A. Zeeb 			cpu_to_le32(address & 0xffffffff);
767b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->metadata_buf_len = 0;
768b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->metadata_buf_addr.high_addr = 0;
769b4c3e9b5SBjoern A. Zeeb 		tx_msghdr->metadata_buf_addr.low_addr = 0;
770b4c3e9b5SBjoern A. Zeeb 		atomic_inc(&commonring->outstanding_tx);
771b4c3e9b5SBjoern A. Zeeb 		if (count >= BRCMF_MSGBUF_TX_FLUSH_CNT2) {
772b4c3e9b5SBjoern A. Zeeb 			brcmf_commonring_write_complete(commonring);
773b4c3e9b5SBjoern A. Zeeb 			count = 0;
774b4c3e9b5SBjoern A. Zeeb 		}
775b4c3e9b5SBjoern A. Zeeb 	}
776b4c3e9b5SBjoern A. Zeeb 	if (count)
777b4c3e9b5SBjoern A. Zeeb 		brcmf_commonring_write_complete(commonring);
778b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_unlock(commonring);
779b4c3e9b5SBjoern A. Zeeb }
780b4c3e9b5SBjoern A. Zeeb 
781b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_txflow_worker(struct work_struct * worker)782b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_txflow_worker(struct work_struct *worker)
783b4c3e9b5SBjoern A. Zeeb {
784b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf;
785b4c3e9b5SBjoern A. Zeeb 	u32 flowid;
786b4c3e9b5SBjoern A. Zeeb 
787b4c3e9b5SBjoern A. Zeeb 	msgbuf = container_of(worker, struct brcmf_msgbuf, txflow_work);
788b4c3e9b5SBjoern A. Zeeb 	for_each_set_bit(flowid, msgbuf->flow_map, msgbuf->max_flowrings) {
789b4c3e9b5SBjoern A. Zeeb 		clear_bit(flowid, msgbuf->flow_map);
790b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_txflow(msgbuf, flowid);
791b4c3e9b5SBjoern A. Zeeb 	}
792b4c3e9b5SBjoern A. Zeeb }
793b4c3e9b5SBjoern A. Zeeb 
794b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_schedule_txdata(struct brcmf_msgbuf * msgbuf,u32 flowid,bool force)795b4c3e9b5SBjoern A. Zeeb static int brcmf_msgbuf_schedule_txdata(struct brcmf_msgbuf *msgbuf, u32 flowid,
796b4c3e9b5SBjoern A. Zeeb 					bool force)
797b4c3e9b5SBjoern A. Zeeb {
798b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring;
799b4c3e9b5SBjoern A. Zeeb 
800b4c3e9b5SBjoern A. Zeeb 	set_bit(flowid, msgbuf->flow_map);
801b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->flowrings[flowid];
802b4c3e9b5SBjoern A. Zeeb 	if ((force) || (atomic_read(&commonring->outstanding_tx) <
803b4c3e9b5SBjoern A. Zeeb 			BRCMF_MSGBUF_DELAY_TXWORKER_THRS))
804b4c3e9b5SBjoern A. Zeeb 		queue_work(msgbuf->txflow_wq, &msgbuf->txflow_work);
805b4c3e9b5SBjoern A. Zeeb 
806b4c3e9b5SBjoern A. Zeeb 	return 0;
807b4c3e9b5SBjoern A. Zeeb }
808b4c3e9b5SBjoern A. Zeeb 
809b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_tx_queue_data(struct brcmf_pub * drvr,int ifidx,struct sk_buff * skb)810b4c3e9b5SBjoern A. Zeeb static int brcmf_msgbuf_tx_queue_data(struct brcmf_pub *drvr, int ifidx,
811b4c3e9b5SBjoern A. Zeeb 				      struct sk_buff *skb)
812b4c3e9b5SBjoern A. Zeeb {
813b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
814b4c3e9b5SBjoern A. Zeeb 	struct brcmf_flowring *flow = msgbuf->flow;
815b4c3e9b5SBjoern A. Zeeb 	struct ethhdr *eh = (struct ethhdr *)(skb->data);
816b4c3e9b5SBjoern A. Zeeb 	u32 flowid;
817b4c3e9b5SBjoern A. Zeeb 	u32 queue_count;
818b4c3e9b5SBjoern A. Zeeb 	bool force;
819b4c3e9b5SBjoern A. Zeeb 
820b4c3e9b5SBjoern A. Zeeb 	flowid = brcmf_flowring_lookup(flow, eh->h_dest, skb->priority, ifidx);
821b4c3e9b5SBjoern A. Zeeb 	if (flowid == BRCMF_FLOWRING_INVALID_ID) {
822b4c3e9b5SBjoern A. Zeeb 		flowid = brcmf_msgbuf_flowring_create(msgbuf, ifidx, skb);
823b4c3e9b5SBjoern A. Zeeb 		if (flowid == BRCMF_FLOWRING_INVALID_ID) {
824b4c3e9b5SBjoern A. Zeeb 			return -ENOMEM;
825b4c3e9b5SBjoern A. Zeeb 		} else {
826b4c3e9b5SBjoern A. Zeeb 			brcmf_flowring_enqueue(flow, flowid, skb);
827b4c3e9b5SBjoern A. Zeeb 			return 0;
828b4c3e9b5SBjoern A. Zeeb 		}
829b4c3e9b5SBjoern A. Zeeb 	}
830b4c3e9b5SBjoern A. Zeeb 	queue_count = brcmf_flowring_enqueue(flow, flowid, skb);
831b4c3e9b5SBjoern A. Zeeb 	force = ((queue_count % BRCMF_MSGBUF_TRICKLE_TXWORKER_THRS) == 0);
832b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_schedule_txdata(msgbuf, flowid, force);
833b4c3e9b5SBjoern A. Zeeb 
834b4c3e9b5SBjoern A. Zeeb 	return 0;
835b4c3e9b5SBjoern A. Zeeb }
836b4c3e9b5SBjoern A. Zeeb 
837b4c3e9b5SBjoern A. Zeeb 
838b4c3e9b5SBjoern A. Zeeb static void
brcmf_msgbuf_configure_addr_mode(struct brcmf_pub * drvr,int ifidx,enum proto_addr_mode addr_mode)839b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_configure_addr_mode(struct brcmf_pub *drvr, int ifidx,
840b4c3e9b5SBjoern A. Zeeb 				 enum proto_addr_mode addr_mode)
841b4c3e9b5SBjoern A. Zeeb {
842b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
843b4c3e9b5SBjoern A. Zeeb 
844b4c3e9b5SBjoern A. Zeeb 	brcmf_flowring_configure_addr_mode(msgbuf->flow, ifidx, addr_mode);
845b4c3e9b5SBjoern A. Zeeb }
846b4c3e9b5SBjoern A. Zeeb 
847b4c3e9b5SBjoern A. Zeeb 
848b4c3e9b5SBjoern A. Zeeb static void
849*902136e0SBjoern A. Zeeb #if defined(__linux__)
brcmf_msgbuf_delete_peer(struct brcmf_pub * drvr,int ifidx,u8 peer[ETH_ALEN])850b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_delete_peer(struct brcmf_pub *drvr, int ifidx, u8 peer[ETH_ALEN])
851*902136e0SBjoern A. Zeeb #elif defined(__FreeBSD__)
852*902136e0SBjoern A. Zeeb brcmf_msgbuf_delete_peer(struct brcmf_pub *drvr, int ifidx, const u8 peer[ETH_ALEN])
853*902136e0SBjoern A. Zeeb #endif
854b4c3e9b5SBjoern A. Zeeb {
855b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
856b4c3e9b5SBjoern A. Zeeb 
857b4c3e9b5SBjoern A. Zeeb 	brcmf_flowring_delete_peer(msgbuf->flow, ifidx, peer);
858b4c3e9b5SBjoern A. Zeeb }
859b4c3e9b5SBjoern A. Zeeb 
860b4c3e9b5SBjoern A. Zeeb 
861b4c3e9b5SBjoern A. Zeeb static void
862*902136e0SBjoern A. Zeeb #if defined(__linux__)
brcmf_msgbuf_add_tdls_peer(struct brcmf_pub * drvr,int ifidx,u8 peer[ETH_ALEN])863b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_add_tdls_peer(struct brcmf_pub *drvr, int ifidx, u8 peer[ETH_ALEN])
864*902136e0SBjoern A. Zeeb #elif defined(__FreeBSD__)
865*902136e0SBjoern A. Zeeb brcmf_msgbuf_add_tdls_peer(struct brcmf_pub *drvr, int ifidx, const u8 peer[ETH_ALEN])
866*902136e0SBjoern A. Zeeb #endif
867b4c3e9b5SBjoern A. Zeeb {
868b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
869b4c3e9b5SBjoern A. Zeeb 
870b4c3e9b5SBjoern A. Zeeb 	brcmf_flowring_add_tdls_peer(msgbuf->flow, ifidx, peer);
871b4c3e9b5SBjoern A. Zeeb }
872b4c3e9b5SBjoern A. Zeeb 
873b4c3e9b5SBjoern A. Zeeb 
874b4c3e9b5SBjoern A. Zeeb static void
brcmf_msgbuf_process_ioctl_complete(struct brcmf_msgbuf * msgbuf,void * buf)875b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_process_ioctl_complete(struct brcmf_msgbuf *msgbuf, void *buf)
876b4c3e9b5SBjoern A. Zeeb {
877b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_ioctl_resp_hdr *ioctl_resp;
878b4c3e9b5SBjoern A. Zeeb 
879b4c3e9b5SBjoern A. Zeeb 	ioctl_resp = (struct msgbuf_ioctl_resp_hdr *)buf;
880b4c3e9b5SBjoern A. Zeeb 
881b4c3e9b5SBjoern A. Zeeb 	msgbuf->ioctl_resp_status =
882b4c3e9b5SBjoern A. Zeeb 			(s16)le16_to_cpu(ioctl_resp->compl_hdr.status);
883b4c3e9b5SBjoern A. Zeeb 	msgbuf->ioctl_resp_ret_len = le16_to_cpu(ioctl_resp->resp_len);
884b4c3e9b5SBjoern A. Zeeb 	msgbuf->ioctl_resp_pktid = le32_to_cpu(ioctl_resp->msg.request_id);
885b4c3e9b5SBjoern A. Zeeb 
886b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_ioctl_resp_wake(msgbuf);
887b4c3e9b5SBjoern A. Zeeb 
888b4c3e9b5SBjoern A. Zeeb 	if (msgbuf->cur_ioctlrespbuf)
889b4c3e9b5SBjoern A. Zeeb 		msgbuf->cur_ioctlrespbuf--;
890b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_rxbuf_ioctlresp_post(msgbuf);
891b4c3e9b5SBjoern A. Zeeb }
892b4c3e9b5SBjoern A. Zeeb 
893b4c3e9b5SBjoern A. Zeeb 
894b4c3e9b5SBjoern A. Zeeb static void
brcmf_msgbuf_process_txstatus(struct brcmf_msgbuf * msgbuf,void * buf)895b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_process_txstatus(struct brcmf_msgbuf *msgbuf, void *buf)
896b4c3e9b5SBjoern A. Zeeb {
897b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring;
898b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_tx_status *tx_status;
899b4c3e9b5SBjoern A. Zeeb 	u32 idx;
900b4c3e9b5SBjoern A. Zeeb 	struct sk_buff *skb;
901b4c3e9b5SBjoern A. Zeeb 	u16 flowid;
902b4c3e9b5SBjoern A. Zeeb 
903b4c3e9b5SBjoern A. Zeeb 	tx_status = (struct msgbuf_tx_status *)buf;
904b4c3e9b5SBjoern A. Zeeb 	idx = le32_to_cpu(tx_status->msg.request_id) - 1;
905b4c3e9b5SBjoern A. Zeeb 	flowid = le16_to_cpu(tx_status->compl_hdr.flow_ring_id);
906b4c3e9b5SBjoern A. Zeeb 	flowid -= BRCMF_H2D_MSGRING_FLOWRING_IDSTART;
907b4c3e9b5SBjoern A. Zeeb 	skb = brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev,
908b4c3e9b5SBjoern A. Zeeb 				     msgbuf->tx_pktids, idx);
909b4c3e9b5SBjoern A. Zeeb 	if (!skb)
910b4c3e9b5SBjoern A. Zeeb 		return;
911b4c3e9b5SBjoern A. Zeeb 
912b4c3e9b5SBjoern A. Zeeb 	set_bit(flowid, msgbuf->txstatus_done_map);
913b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->flowrings[flowid];
914b4c3e9b5SBjoern A. Zeeb 	atomic_dec(&commonring->outstanding_tx);
915b4c3e9b5SBjoern A. Zeeb 
916b4c3e9b5SBjoern A. Zeeb 	brcmf_txfinalize(brcmf_get_ifp(msgbuf->drvr, tx_status->msg.ifidx),
917b4c3e9b5SBjoern A. Zeeb 			 skb, true);
918b4c3e9b5SBjoern A. Zeeb }
919b4c3e9b5SBjoern A. Zeeb 
920b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_rxbuf_data_post(struct brcmf_msgbuf * msgbuf,u32 count)921b4c3e9b5SBjoern A. Zeeb static u32 brcmf_msgbuf_rxbuf_data_post(struct brcmf_msgbuf *msgbuf, u32 count)
922b4c3e9b5SBjoern A. Zeeb {
923b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
924b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring;
925*902136e0SBjoern A. Zeeb #if defined(__linux__)
926b4c3e9b5SBjoern A. Zeeb 	void *ret_ptr;
927*902136e0SBjoern A. Zeeb #elif defined(__FreeBSD__)
928*902136e0SBjoern A. Zeeb 	u8 *ret_ptr;
929*902136e0SBjoern A. Zeeb #endif
930b4c3e9b5SBjoern A. Zeeb 	struct sk_buff *skb;
931b4c3e9b5SBjoern A. Zeeb 	u16 alloced;
932b4c3e9b5SBjoern A. Zeeb 	u32 pktlen;
933b4c3e9b5SBjoern A. Zeeb 	dma_addr_t physaddr;
934b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_rx_bufpost *rx_bufpost;
935b4c3e9b5SBjoern A. Zeeb 	u64 address;
936b4c3e9b5SBjoern A. Zeeb 	u32 pktid;
937b4c3e9b5SBjoern A. Zeeb 	u32 i;
938b4c3e9b5SBjoern A. Zeeb 
939b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_RXPOST_SUBMIT];
940b4c3e9b5SBjoern A. Zeeb 	ret_ptr = brcmf_commonring_reserve_for_write_multiple(commonring,
941b4c3e9b5SBjoern A. Zeeb 							      count,
942b4c3e9b5SBjoern A. Zeeb 							      &alloced);
943b4c3e9b5SBjoern A. Zeeb 	if (!ret_ptr) {
944b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "Failed to reserve space in commonring\n");
945b4c3e9b5SBjoern A. Zeeb 		return 0;
946b4c3e9b5SBjoern A. Zeeb 	}
947b4c3e9b5SBjoern A. Zeeb 
948b4c3e9b5SBjoern A. Zeeb 	for (i = 0; i < alloced; i++) {
949b4c3e9b5SBjoern A. Zeeb 		rx_bufpost = (struct msgbuf_rx_bufpost *)ret_ptr;
950b4c3e9b5SBjoern A. Zeeb 		memset(rx_bufpost, 0, sizeof(*rx_bufpost));
951b4c3e9b5SBjoern A. Zeeb 
952b4c3e9b5SBjoern A. Zeeb 		skb = brcmu_pkt_buf_get_skb(BRCMF_MSGBUF_MAX_PKT_SIZE);
953b4c3e9b5SBjoern A. Zeeb 
954b4c3e9b5SBjoern A. Zeeb 		if (skb == NULL) {
955b4c3e9b5SBjoern A. Zeeb 			bphy_err(drvr, "Failed to alloc SKB\n");
956b4c3e9b5SBjoern A. Zeeb 			brcmf_commonring_write_cancel(commonring, alloced - i);
957b4c3e9b5SBjoern A. Zeeb 			break;
958b4c3e9b5SBjoern A. Zeeb 		}
959b4c3e9b5SBjoern A. Zeeb 
960b4c3e9b5SBjoern A. Zeeb 		pktlen = skb->len;
961b4c3e9b5SBjoern A. Zeeb 		if (brcmf_msgbuf_alloc_pktid(msgbuf->drvr->bus_if->dev,
962b4c3e9b5SBjoern A. Zeeb 					     msgbuf->rx_pktids, skb, 0,
963b4c3e9b5SBjoern A. Zeeb 					     &physaddr, &pktid)) {
964b4c3e9b5SBjoern A. Zeeb 			dev_kfree_skb_any(skb);
965b4c3e9b5SBjoern A. Zeeb 			bphy_err(drvr, "No PKTID available !!\n");
966b4c3e9b5SBjoern A. Zeeb 			brcmf_commonring_write_cancel(commonring, alloced - i);
967b4c3e9b5SBjoern A. Zeeb 			break;
968b4c3e9b5SBjoern A. Zeeb 		}
969b4c3e9b5SBjoern A. Zeeb 
970b4c3e9b5SBjoern A. Zeeb 		if (msgbuf->rx_metadata_offset) {
971b4c3e9b5SBjoern A. Zeeb 			address = (u64)physaddr;
972b4c3e9b5SBjoern A. Zeeb 			rx_bufpost->metadata_buf_len =
973b4c3e9b5SBjoern A. Zeeb 				cpu_to_le16(msgbuf->rx_metadata_offset);
974b4c3e9b5SBjoern A. Zeeb 			rx_bufpost->metadata_buf_addr.high_addr =
975b4c3e9b5SBjoern A. Zeeb 				cpu_to_le32(address >> 32);
976b4c3e9b5SBjoern A. Zeeb 			rx_bufpost->metadata_buf_addr.low_addr =
977b4c3e9b5SBjoern A. Zeeb 				cpu_to_le32(address & 0xffffffff);
978b4c3e9b5SBjoern A. Zeeb 
979b4c3e9b5SBjoern A. Zeeb 			skb_pull(skb, msgbuf->rx_metadata_offset);
980b4c3e9b5SBjoern A. Zeeb 			pktlen = skb->len;
981b4c3e9b5SBjoern A. Zeeb 			physaddr += msgbuf->rx_metadata_offset;
982b4c3e9b5SBjoern A. Zeeb 		}
983b4c3e9b5SBjoern A. Zeeb 		rx_bufpost->msg.msgtype = MSGBUF_TYPE_RXBUF_POST;
984b4c3e9b5SBjoern A. Zeeb 		rx_bufpost->msg.request_id = cpu_to_le32(pktid);
985b4c3e9b5SBjoern A. Zeeb 
986b4c3e9b5SBjoern A. Zeeb 		address = (u64)physaddr;
987b4c3e9b5SBjoern A. Zeeb 		rx_bufpost->data_buf_len = cpu_to_le16((u16)pktlen);
988b4c3e9b5SBjoern A. Zeeb 		rx_bufpost->data_buf_addr.high_addr =
989b4c3e9b5SBjoern A. Zeeb 			cpu_to_le32(address >> 32);
990b4c3e9b5SBjoern A. Zeeb 		rx_bufpost->data_buf_addr.low_addr =
991b4c3e9b5SBjoern A. Zeeb 			cpu_to_le32(address & 0xffffffff);
992b4c3e9b5SBjoern A. Zeeb 
993b4c3e9b5SBjoern A. Zeeb 		ret_ptr += brcmf_commonring_len_item(commonring);
994b4c3e9b5SBjoern A. Zeeb 	}
995b4c3e9b5SBjoern A. Zeeb 
996b4c3e9b5SBjoern A. Zeeb 	if (i)
997b4c3e9b5SBjoern A. Zeeb 		brcmf_commonring_write_complete(commonring);
998b4c3e9b5SBjoern A. Zeeb 
999b4c3e9b5SBjoern A. Zeeb 	return i;
1000b4c3e9b5SBjoern A. Zeeb }
1001b4c3e9b5SBjoern A. Zeeb 
1002b4c3e9b5SBjoern A. Zeeb 
1003b4c3e9b5SBjoern A. Zeeb static void
brcmf_msgbuf_rxbuf_data_fill(struct brcmf_msgbuf * msgbuf)1004b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_rxbuf_data_fill(struct brcmf_msgbuf *msgbuf)
1005b4c3e9b5SBjoern A. Zeeb {
1006b4c3e9b5SBjoern A. Zeeb 	u32 fillbufs;
1007b4c3e9b5SBjoern A. Zeeb 	u32 retcount;
1008b4c3e9b5SBjoern A. Zeeb 
1009b4c3e9b5SBjoern A. Zeeb 	fillbufs = msgbuf->max_rxbufpost - msgbuf->rxbufpost;
1010b4c3e9b5SBjoern A. Zeeb 
1011b4c3e9b5SBjoern A. Zeeb 	while (fillbufs) {
1012b4c3e9b5SBjoern A. Zeeb 		retcount = brcmf_msgbuf_rxbuf_data_post(msgbuf, fillbufs);
1013b4c3e9b5SBjoern A. Zeeb 		if (!retcount)
1014b4c3e9b5SBjoern A. Zeeb 			break;
1015b4c3e9b5SBjoern A. Zeeb 		msgbuf->rxbufpost += retcount;
1016b4c3e9b5SBjoern A. Zeeb 		fillbufs -= retcount;
1017b4c3e9b5SBjoern A. Zeeb 	}
1018b4c3e9b5SBjoern A. Zeeb }
1019b4c3e9b5SBjoern A. Zeeb 
1020b4c3e9b5SBjoern A. Zeeb 
1021b4c3e9b5SBjoern A. Zeeb static void
brcmf_msgbuf_update_rxbufpost_count(struct brcmf_msgbuf * msgbuf,u16 rxcnt)1022b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_update_rxbufpost_count(struct brcmf_msgbuf *msgbuf, u16 rxcnt)
1023b4c3e9b5SBjoern A. Zeeb {
1024b4c3e9b5SBjoern A. Zeeb 	msgbuf->rxbufpost -= rxcnt;
1025b4c3e9b5SBjoern A. Zeeb 	if (msgbuf->rxbufpost <= (msgbuf->max_rxbufpost -
1026b4c3e9b5SBjoern A. Zeeb 				  BRCMF_MSGBUF_RXBUFPOST_THRESHOLD))
1027b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_rxbuf_data_fill(msgbuf);
1028b4c3e9b5SBjoern A. Zeeb }
1029b4c3e9b5SBjoern A. Zeeb 
1030b4c3e9b5SBjoern A. Zeeb 
1031b4c3e9b5SBjoern A. Zeeb static u32
brcmf_msgbuf_rxbuf_ctrl_post(struct brcmf_msgbuf * msgbuf,bool event_buf,u32 count)1032b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_rxbuf_ctrl_post(struct brcmf_msgbuf *msgbuf, bool event_buf,
1033b4c3e9b5SBjoern A. Zeeb 			     u32 count)
1034b4c3e9b5SBjoern A. Zeeb {
1035b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
1036b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring;
1037*902136e0SBjoern A. Zeeb #if defined(__linux__)
1038b4c3e9b5SBjoern A. Zeeb 	void *ret_ptr;
1039*902136e0SBjoern A. Zeeb #elif defined(__FreeBSD__)
1040*902136e0SBjoern A. Zeeb 	u8 *ret_ptr;
1041*902136e0SBjoern A. Zeeb #endif
1042b4c3e9b5SBjoern A. Zeeb 	struct sk_buff *skb;
1043b4c3e9b5SBjoern A. Zeeb 	u16 alloced;
1044b4c3e9b5SBjoern A. Zeeb 	u32 pktlen;
1045b4c3e9b5SBjoern A. Zeeb 	dma_addr_t physaddr;
1046b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_rx_ioctl_resp_or_event *rx_bufpost;
1047b4c3e9b5SBjoern A. Zeeb 	u64 address;
1048b4c3e9b5SBjoern A. Zeeb 	u32 pktid;
1049b4c3e9b5SBjoern A. Zeeb 	u32 i;
1050b4c3e9b5SBjoern A. Zeeb 
1051b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT];
1052b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_lock(commonring);
1053b4c3e9b5SBjoern A. Zeeb 	ret_ptr = brcmf_commonring_reserve_for_write_multiple(commonring,
1054b4c3e9b5SBjoern A. Zeeb 							      count,
1055b4c3e9b5SBjoern A. Zeeb 							      &alloced);
1056b4c3e9b5SBjoern A. Zeeb 	if (!ret_ptr) {
1057b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Failed to reserve space in commonring\n");
1058b4c3e9b5SBjoern A. Zeeb 		brcmf_commonring_unlock(commonring);
1059b4c3e9b5SBjoern A. Zeeb 		return 0;
1060b4c3e9b5SBjoern A. Zeeb 	}
1061b4c3e9b5SBjoern A. Zeeb 
1062b4c3e9b5SBjoern A. Zeeb 	for (i = 0; i < alloced; i++) {
1063b4c3e9b5SBjoern A. Zeeb 		rx_bufpost = (struct msgbuf_rx_ioctl_resp_or_event *)ret_ptr;
1064b4c3e9b5SBjoern A. Zeeb 		memset(rx_bufpost, 0, sizeof(*rx_bufpost));
1065b4c3e9b5SBjoern A. Zeeb 
1066b4c3e9b5SBjoern A. Zeeb 		skb = brcmu_pkt_buf_get_skb(BRCMF_MSGBUF_MAX_CTL_PKT_SIZE);
1067b4c3e9b5SBjoern A. Zeeb 
1068b4c3e9b5SBjoern A. Zeeb 		if (skb == NULL) {
1069b4c3e9b5SBjoern A. Zeeb 			bphy_err(drvr, "Failed to alloc SKB\n");
1070b4c3e9b5SBjoern A. Zeeb 			brcmf_commonring_write_cancel(commonring, alloced - i);
1071b4c3e9b5SBjoern A. Zeeb 			break;
1072b4c3e9b5SBjoern A. Zeeb 		}
1073b4c3e9b5SBjoern A. Zeeb 
1074b4c3e9b5SBjoern A. Zeeb 		pktlen = skb->len;
1075b4c3e9b5SBjoern A. Zeeb 		if (brcmf_msgbuf_alloc_pktid(msgbuf->drvr->bus_if->dev,
1076b4c3e9b5SBjoern A. Zeeb 					     msgbuf->rx_pktids, skb, 0,
1077b4c3e9b5SBjoern A. Zeeb 					     &physaddr, &pktid)) {
1078b4c3e9b5SBjoern A. Zeeb 			dev_kfree_skb_any(skb);
1079b4c3e9b5SBjoern A. Zeeb 			bphy_err(drvr, "No PKTID available !!\n");
1080b4c3e9b5SBjoern A. Zeeb 			brcmf_commonring_write_cancel(commonring, alloced - i);
1081b4c3e9b5SBjoern A. Zeeb 			break;
1082b4c3e9b5SBjoern A. Zeeb 		}
1083b4c3e9b5SBjoern A. Zeeb 		if (event_buf)
1084b4c3e9b5SBjoern A. Zeeb 			rx_bufpost->msg.msgtype = MSGBUF_TYPE_EVENT_BUF_POST;
1085b4c3e9b5SBjoern A. Zeeb 		else
1086b4c3e9b5SBjoern A. Zeeb 			rx_bufpost->msg.msgtype =
1087b4c3e9b5SBjoern A. Zeeb 				MSGBUF_TYPE_IOCTLRESP_BUF_POST;
1088b4c3e9b5SBjoern A. Zeeb 		rx_bufpost->msg.request_id = cpu_to_le32(pktid);
1089b4c3e9b5SBjoern A. Zeeb 
1090b4c3e9b5SBjoern A. Zeeb 		address = (u64)physaddr;
1091b4c3e9b5SBjoern A. Zeeb 		rx_bufpost->host_buf_len = cpu_to_le16((u16)pktlen);
1092b4c3e9b5SBjoern A. Zeeb 		rx_bufpost->host_buf_addr.high_addr =
1093b4c3e9b5SBjoern A. Zeeb 			cpu_to_le32(address >> 32);
1094b4c3e9b5SBjoern A. Zeeb 		rx_bufpost->host_buf_addr.low_addr =
1095b4c3e9b5SBjoern A. Zeeb 			cpu_to_le32(address & 0xffffffff);
1096b4c3e9b5SBjoern A. Zeeb 
1097b4c3e9b5SBjoern A. Zeeb 		ret_ptr += brcmf_commonring_len_item(commonring);
1098b4c3e9b5SBjoern A. Zeeb 	}
1099b4c3e9b5SBjoern A. Zeeb 
1100b4c3e9b5SBjoern A. Zeeb 	if (i)
1101b4c3e9b5SBjoern A. Zeeb 		brcmf_commonring_write_complete(commonring);
1102b4c3e9b5SBjoern A. Zeeb 
1103b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_unlock(commonring);
1104b4c3e9b5SBjoern A. Zeeb 
1105b4c3e9b5SBjoern A. Zeeb 	return i;
1106b4c3e9b5SBjoern A. Zeeb }
1107b4c3e9b5SBjoern A. Zeeb 
1108b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_rxbuf_ioctlresp_post(struct brcmf_msgbuf * msgbuf)1109b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_rxbuf_ioctlresp_post(struct brcmf_msgbuf *msgbuf)
1110b4c3e9b5SBjoern A. Zeeb {
1111b4c3e9b5SBjoern A. Zeeb 	u32 count;
1112b4c3e9b5SBjoern A. Zeeb 
1113b4c3e9b5SBjoern A. Zeeb 	count = msgbuf->max_ioctlrespbuf - msgbuf->cur_ioctlrespbuf;
1114b4c3e9b5SBjoern A. Zeeb 	count = brcmf_msgbuf_rxbuf_ctrl_post(msgbuf, false, count);
1115b4c3e9b5SBjoern A. Zeeb 	msgbuf->cur_ioctlrespbuf += count;
1116b4c3e9b5SBjoern A. Zeeb }
1117b4c3e9b5SBjoern A. Zeeb 
1118b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_rxbuf_event_post(struct brcmf_msgbuf * msgbuf)1119b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_rxbuf_event_post(struct brcmf_msgbuf *msgbuf)
1120b4c3e9b5SBjoern A. Zeeb {
1121b4c3e9b5SBjoern A. Zeeb 	u32 count;
1122b4c3e9b5SBjoern A. Zeeb 
1123b4c3e9b5SBjoern A. Zeeb 	count = msgbuf->max_eventbuf - msgbuf->cur_eventbuf;
1124b4c3e9b5SBjoern A. Zeeb 	count = brcmf_msgbuf_rxbuf_ctrl_post(msgbuf, true, count);
1125b4c3e9b5SBjoern A. Zeeb 	msgbuf->cur_eventbuf += count;
1126b4c3e9b5SBjoern A. Zeeb }
1127b4c3e9b5SBjoern A. Zeeb 
1128b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_process_event(struct brcmf_msgbuf * msgbuf,void * buf)1129b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_process_event(struct brcmf_msgbuf *msgbuf, void *buf)
1130b4c3e9b5SBjoern A. Zeeb {
1131b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
1132b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_rx_event *event;
1133b4c3e9b5SBjoern A. Zeeb 	u32 idx;
1134b4c3e9b5SBjoern A. Zeeb 	u16 buflen;
1135b4c3e9b5SBjoern A. Zeeb 	struct sk_buff *skb;
1136b4c3e9b5SBjoern A. Zeeb 	struct brcmf_if *ifp;
1137b4c3e9b5SBjoern A. Zeeb 
1138b4c3e9b5SBjoern A. Zeeb 	event = (struct msgbuf_rx_event *)buf;
1139b4c3e9b5SBjoern A. Zeeb 	idx = le32_to_cpu(event->msg.request_id);
1140b4c3e9b5SBjoern A. Zeeb 	buflen = le16_to_cpu(event->event_data_len);
1141b4c3e9b5SBjoern A. Zeeb 
1142b4c3e9b5SBjoern A. Zeeb 	if (msgbuf->cur_eventbuf)
1143b4c3e9b5SBjoern A. Zeeb 		msgbuf->cur_eventbuf--;
1144b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_rxbuf_event_post(msgbuf);
1145b4c3e9b5SBjoern A. Zeeb 
1146b4c3e9b5SBjoern A. Zeeb 	skb = brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev,
1147b4c3e9b5SBjoern A. Zeeb 				     msgbuf->rx_pktids, idx);
1148b4c3e9b5SBjoern A. Zeeb 	if (!skb)
1149b4c3e9b5SBjoern A. Zeeb 		return;
1150b4c3e9b5SBjoern A. Zeeb 
1151b4c3e9b5SBjoern A. Zeeb 	if (msgbuf->rx_dataoffset)
1152b4c3e9b5SBjoern A. Zeeb 		skb_pull(skb, msgbuf->rx_dataoffset);
1153b4c3e9b5SBjoern A. Zeeb 
1154b4c3e9b5SBjoern A. Zeeb 	skb_trim(skb, buflen);
1155b4c3e9b5SBjoern A. Zeeb 
1156b4c3e9b5SBjoern A. Zeeb 	ifp = brcmf_get_ifp(msgbuf->drvr, event->msg.ifidx);
1157b4c3e9b5SBjoern A. Zeeb 	if (!ifp || !ifp->ndev) {
1158b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Received pkt for invalid ifidx %d\n",
1159b4c3e9b5SBjoern A. Zeeb 			 event->msg.ifidx);
1160b4c3e9b5SBjoern A. Zeeb 		goto exit;
1161b4c3e9b5SBjoern A. Zeeb 	}
1162b4c3e9b5SBjoern A. Zeeb 
1163b4c3e9b5SBjoern A. Zeeb 	skb->protocol = eth_type_trans(skb, ifp->ndev);
1164b4c3e9b5SBjoern A. Zeeb 
1165b4c3e9b5SBjoern A. Zeeb 	brcmf_fweh_process_skb(ifp->drvr, skb, 0, GFP_KERNEL);
1166b4c3e9b5SBjoern A. Zeeb 
1167b4c3e9b5SBjoern A. Zeeb exit:
1168b4c3e9b5SBjoern A. Zeeb 	brcmu_pkt_buf_free_skb(skb);
1169b4c3e9b5SBjoern A. Zeeb }
1170b4c3e9b5SBjoern A. Zeeb 
1171b4c3e9b5SBjoern A. Zeeb 
1172b4c3e9b5SBjoern A. Zeeb static void
brcmf_msgbuf_process_rx_complete(struct brcmf_msgbuf * msgbuf,void * buf)1173b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_process_rx_complete(struct brcmf_msgbuf *msgbuf, void *buf)
1174b4c3e9b5SBjoern A. Zeeb {
1175b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
1176b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_rx_complete *rx_complete;
1177b4c3e9b5SBjoern A. Zeeb 	struct sk_buff *skb;
1178b4c3e9b5SBjoern A. Zeeb 	u16 data_offset;
1179b4c3e9b5SBjoern A. Zeeb 	u16 buflen;
1180b4c3e9b5SBjoern A. Zeeb 	u16 flags;
1181b4c3e9b5SBjoern A. Zeeb 	u32 idx;
1182b4c3e9b5SBjoern A. Zeeb 	struct brcmf_if *ifp;
1183b4c3e9b5SBjoern A. Zeeb 
1184b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_update_rxbufpost_count(msgbuf, 1);
1185b4c3e9b5SBjoern A. Zeeb 
1186b4c3e9b5SBjoern A. Zeeb 	rx_complete = (struct msgbuf_rx_complete *)buf;
1187b4c3e9b5SBjoern A. Zeeb 	data_offset = le16_to_cpu(rx_complete->data_offset);
1188b4c3e9b5SBjoern A. Zeeb 	buflen = le16_to_cpu(rx_complete->data_len);
1189b4c3e9b5SBjoern A. Zeeb 	idx = le32_to_cpu(rx_complete->msg.request_id);
1190b4c3e9b5SBjoern A. Zeeb 	flags = le16_to_cpu(rx_complete->flags);
1191b4c3e9b5SBjoern A. Zeeb 
1192b4c3e9b5SBjoern A. Zeeb 	skb = brcmf_msgbuf_get_pktid(msgbuf->drvr->bus_if->dev,
1193b4c3e9b5SBjoern A. Zeeb 				     msgbuf->rx_pktids, idx);
1194b4c3e9b5SBjoern A. Zeeb 	if (!skb)
1195b4c3e9b5SBjoern A. Zeeb 		return;
1196b4c3e9b5SBjoern A. Zeeb 
1197b4c3e9b5SBjoern A. Zeeb 	if (data_offset)
1198b4c3e9b5SBjoern A. Zeeb 		skb_pull(skb, data_offset);
1199b4c3e9b5SBjoern A. Zeeb 	else if (msgbuf->rx_dataoffset)
1200b4c3e9b5SBjoern A. Zeeb 		skb_pull(skb, msgbuf->rx_dataoffset);
1201b4c3e9b5SBjoern A. Zeeb 
1202b4c3e9b5SBjoern A. Zeeb 	skb_trim(skb, buflen);
1203b4c3e9b5SBjoern A. Zeeb 
1204b4c3e9b5SBjoern A. Zeeb 	if ((flags & BRCMF_MSGBUF_PKT_FLAGS_FRAME_MASK) ==
1205b4c3e9b5SBjoern A. Zeeb 	    BRCMF_MSGBUF_PKT_FLAGS_FRAME_802_11) {
1206b4c3e9b5SBjoern A. Zeeb 		ifp = msgbuf->drvr->mon_if;
1207b4c3e9b5SBjoern A. Zeeb 
1208b4c3e9b5SBjoern A. Zeeb 		if (!ifp) {
1209b4c3e9b5SBjoern A. Zeeb 			bphy_err(drvr, "Received unexpected monitor pkt\n");
1210b4c3e9b5SBjoern A. Zeeb 			brcmu_pkt_buf_free_skb(skb);
1211b4c3e9b5SBjoern A. Zeeb 			return;
1212b4c3e9b5SBjoern A. Zeeb 		}
1213b4c3e9b5SBjoern A. Zeeb 
1214b4c3e9b5SBjoern A. Zeeb 		brcmf_netif_mon_rx(ifp, skb);
1215b4c3e9b5SBjoern A. Zeeb 		return;
1216b4c3e9b5SBjoern A. Zeeb 	}
1217b4c3e9b5SBjoern A. Zeeb 
1218b4c3e9b5SBjoern A. Zeeb 	ifp = brcmf_get_ifp(msgbuf->drvr, rx_complete->msg.ifidx);
1219b4c3e9b5SBjoern A. Zeeb 	if (!ifp || !ifp->ndev) {
1220b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Received pkt for invalid ifidx %d\n",
1221b4c3e9b5SBjoern A. Zeeb 			 rx_complete->msg.ifidx);
1222b4c3e9b5SBjoern A. Zeeb 		brcmu_pkt_buf_free_skb(skb);
1223b4c3e9b5SBjoern A. Zeeb 		return;
1224b4c3e9b5SBjoern A. Zeeb 	}
1225b4c3e9b5SBjoern A. Zeeb 
1226b4c3e9b5SBjoern A. Zeeb 	skb->protocol = eth_type_trans(skb, ifp->ndev);
1227b4c3e9b5SBjoern A. Zeeb 	brcmf_netif_rx(ifp, skb);
1228b4c3e9b5SBjoern A. Zeeb }
1229b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_process_gen_status(struct brcmf_msgbuf * msgbuf,void * buf)1230b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_process_gen_status(struct brcmf_msgbuf *msgbuf,
1231b4c3e9b5SBjoern A. Zeeb 					    void *buf)
1232b4c3e9b5SBjoern A. Zeeb {
1233b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_gen_status *gen_status = buf;
1234b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
1235b4c3e9b5SBjoern A. Zeeb 	int err;
1236b4c3e9b5SBjoern A. Zeeb 
1237b4c3e9b5SBjoern A. Zeeb 	err = le16_to_cpu(gen_status->compl_hdr.status);
1238b4c3e9b5SBjoern A. Zeeb 	if (err)
1239b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Firmware reported general error: %d\n", err);
1240b4c3e9b5SBjoern A. Zeeb }
1241b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_process_ring_status(struct brcmf_msgbuf * msgbuf,void * buf)1242b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_process_ring_status(struct brcmf_msgbuf *msgbuf,
1243b4c3e9b5SBjoern A. Zeeb 					     void *buf)
1244b4c3e9b5SBjoern A. Zeeb {
1245b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_ring_status *ring_status = buf;
1246b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
1247b4c3e9b5SBjoern A. Zeeb 	int err;
1248b4c3e9b5SBjoern A. Zeeb 
1249b4c3e9b5SBjoern A. Zeeb 	err = le16_to_cpu(ring_status->compl_hdr.status);
1250b4c3e9b5SBjoern A. Zeeb 	if (err) {
1251b4c3e9b5SBjoern A. Zeeb 		int ring = le16_to_cpu(ring_status->compl_hdr.flow_ring_id);
1252b4c3e9b5SBjoern A. Zeeb 
1253b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Firmware reported ring %d error: %d\n", ring,
1254b4c3e9b5SBjoern A. Zeeb 			 err);
1255b4c3e9b5SBjoern A. Zeeb 	}
1256b4c3e9b5SBjoern A. Zeeb }
1257b4c3e9b5SBjoern A. Zeeb 
1258b4c3e9b5SBjoern A. Zeeb static void
brcmf_msgbuf_process_flow_ring_create_response(struct brcmf_msgbuf * msgbuf,void * buf)1259b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_process_flow_ring_create_response(struct brcmf_msgbuf *msgbuf,
1260b4c3e9b5SBjoern A. Zeeb 					       void *buf)
1261b4c3e9b5SBjoern A. Zeeb {
1262b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
1263b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_flowring_create_resp *flowring_create_resp;
1264b4c3e9b5SBjoern A. Zeeb 	u16 status;
1265b4c3e9b5SBjoern A. Zeeb 	u16 flowid;
1266b4c3e9b5SBjoern A. Zeeb 
1267b4c3e9b5SBjoern A. Zeeb 	flowring_create_resp = (struct msgbuf_flowring_create_resp *)buf;
1268b4c3e9b5SBjoern A. Zeeb 
1269b4c3e9b5SBjoern A. Zeeb 	flowid = le16_to_cpu(flowring_create_resp->compl_hdr.flow_ring_id);
1270b4c3e9b5SBjoern A. Zeeb 	flowid -= BRCMF_H2D_MSGRING_FLOWRING_IDSTART;
1271b4c3e9b5SBjoern A. Zeeb 	status =  le16_to_cpu(flowring_create_resp->compl_hdr.status);
1272b4c3e9b5SBjoern A. Zeeb 
1273b4c3e9b5SBjoern A. Zeeb 	if (status) {
1274b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Flowring creation failed, code %d\n", status);
1275b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_remove_flowring(msgbuf, flowid);
1276b4c3e9b5SBjoern A. Zeeb 		return;
1277b4c3e9b5SBjoern A. Zeeb 	}
1278b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(MSGBUF, "Flowring %d Create response status %d\n", flowid,
1279b4c3e9b5SBjoern A. Zeeb 		  status);
1280b4c3e9b5SBjoern A. Zeeb 
1281b4c3e9b5SBjoern A. Zeeb 	brcmf_flowring_open(msgbuf->flow, flowid);
1282b4c3e9b5SBjoern A. Zeeb 
1283b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_schedule_txdata(msgbuf, flowid, true);
1284b4c3e9b5SBjoern A. Zeeb }
1285b4c3e9b5SBjoern A. Zeeb 
1286b4c3e9b5SBjoern A. Zeeb 
1287b4c3e9b5SBjoern A. Zeeb static void
brcmf_msgbuf_process_flow_ring_delete_response(struct brcmf_msgbuf * msgbuf,void * buf)1288b4c3e9b5SBjoern A. Zeeb brcmf_msgbuf_process_flow_ring_delete_response(struct brcmf_msgbuf *msgbuf,
1289b4c3e9b5SBjoern A. Zeeb 					       void *buf)
1290b4c3e9b5SBjoern A. Zeeb {
1291b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
1292b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_flowring_delete_resp *flowring_delete_resp;
1293b4c3e9b5SBjoern A. Zeeb 	u16 status;
1294b4c3e9b5SBjoern A. Zeeb 	u16 flowid;
1295b4c3e9b5SBjoern A. Zeeb 
1296b4c3e9b5SBjoern A. Zeeb 	flowring_delete_resp = (struct msgbuf_flowring_delete_resp *)buf;
1297b4c3e9b5SBjoern A. Zeeb 
1298b4c3e9b5SBjoern A. Zeeb 	flowid = le16_to_cpu(flowring_delete_resp->compl_hdr.flow_ring_id);
1299b4c3e9b5SBjoern A. Zeeb 	flowid -= BRCMF_H2D_MSGRING_FLOWRING_IDSTART;
1300b4c3e9b5SBjoern A. Zeeb 	status =  le16_to_cpu(flowring_delete_resp->compl_hdr.status);
1301b4c3e9b5SBjoern A. Zeeb 
1302b4c3e9b5SBjoern A. Zeeb 	if (status) {
1303b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Flowring deletion failed, code %d\n", status);
1304b4c3e9b5SBjoern A. Zeeb 		brcmf_flowring_delete(msgbuf->flow, flowid);
1305b4c3e9b5SBjoern A. Zeeb 		return;
1306b4c3e9b5SBjoern A. Zeeb 	}
1307b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(MSGBUF, "Flowring %d Delete response status %d\n", flowid,
1308b4c3e9b5SBjoern A. Zeeb 		  status);
1309b4c3e9b5SBjoern A. Zeeb 
1310b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_remove_flowring(msgbuf, flowid);
1311b4c3e9b5SBjoern A. Zeeb }
1312b4c3e9b5SBjoern A. Zeeb 
1313b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_process_msgtype(struct brcmf_msgbuf * msgbuf,void * buf)1314b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_process_msgtype(struct brcmf_msgbuf *msgbuf, void *buf)
1315b4c3e9b5SBjoern A. Zeeb {
1316b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = msgbuf->drvr;
1317b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_common_hdr *msg;
1318b4c3e9b5SBjoern A. Zeeb 
1319b4c3e9b5SBjoern A. Zeeb 	msg = (struct msgbuf_common_hdr *)buf;
1320b4c3e9b5SBjoern A. Zeeb 	switch (msg->msgtype) {
1321b4c3e9b5SBjoern A. Zeeb 	case MSGBUF_TYPE_GEN_STATUS:
1322b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "MSGBUF_TYPE_GEN_STATUS\n");
1323b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_process_gen_status(msgbuf, buf);
1324b4c3e9b5SBjoern A. Zeeb 		break;
1325b4c3e9b5SBjoern A. Zeeb 	case MSGBUF_TYPE_RING_STATUS:
1326b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "MSGBUF_TYPE_RING_STATUS\n");
1327b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_process_ring_status(msgbuf, buf);
1328b4c3e9b5SBjoern A. Zeeb 		break;
1329b4c3e9b5SBjoern A. Zeeb 	case MSGBUF_TYPE_FLOW_RING_CREATE_CMPLT:
1330b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "MSGBUF_TYPE_FLOW_RING_CREATE_CMPLT\n");
1331b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_process_flow_ring_create_response(msgbuf, buf);
1332b4c3e9b5SBjoern A. Zeeb 		break;
1333b4c3e9b5SBjoern A. Zeeb 	case MSGBUF_TYPE_FLOW_RING_DELETE_CMPLT:
1334b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "MSGBUF_TYPE_FLOW_RING_DELETE_CMPLT\n");
1335b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_process_flow_ring_delete_response(msgbuf, buf);
1336b4c3e9b5SBjoern A. Zeeb 		break;
1337b4c3e9b5SBjoern A. Zeeb 	case MSGBUF_TYPE_IOCTLPTR_REQ_ACK:
1338b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "MSGBUF_TYPE_IOCTLPTR_REQ_ACK\n");
1339b4c3e9b5SBjoern A. Zeeb 		break;
1340b4c3e9b5SBjoern A. Zeeb 	case MSGBUF_TYPE_IOCTL_CMPLT:
1341b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "MSGBUF_TYPE_IOCTL_CMPLT\n");
1342b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_process_ioctl_complete(msgbuf, buf);
1343b4c3e9b5SBjoern A. Zeeb 		break;
1344b4c3e9b5SBjoern A. Zeeb 	case MSGBUF_TYPE_WL_EVENT:
1345b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "MSGBUF_TYPE_WL_EVENT\n");
1346b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_process_event(msgbuf, buf);
1347b4c3e9b5SBjoern A. Zeeb 		break;
1348b4c3e9b5SBjoern A. Zeeb 	case MSGBUF_TYPE_TX_STATUS:
1349b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "MSGBUF_TYPE_TX_STATUS\n");
1350b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_process_txstatus(msgbuf, buf);
1351b4c3e9b5SBjoern A. Zeeb 		break;
1352b4c3e9b5SBjoern A. Zeeb 	case MSGBUF_TYPE_RX_CMPLT:
1353b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "MSGBUF_TYPE_RX_CMPLT\n");
1354b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_process_rx_complete(msgbuf, buf);
1355b4c3e9b5SBjoern A. Zeeb 		break;
1356b4c3e9b5SBjoern A. Zeeb 	default:
1357b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Unsupported msgtype %d\n", msg->msgtype);
1358b4c3e9b5SBjoern A. Zeeb 		break;
1359b4c3e9b5SBjoern A. Zeeb 	}
1360b4c3e9b5SBjoern A. Zeeb }
1361b4c3e9b5SBjoern A. Zeeb 
1362b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_process_rx(struct brcmf_msgbuf * msgbuf,struct brcmf_commonring * commonring)1363b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_process_rx(struct brcmf_msgbuf *msgbuf,
1364b4c3e9b5SBjoern A. Zeeb 				    struct brcmf_commonring *commonring)
1365b4c3e9b5SBjoern A. Zeeb {
1366*902136e0SBjoern A. Zeeb #if defined(__linux__)
1367b4c3e9b5SBjoern A. Zeeb 	void *buf;
1368*902136e0SBjoern A. Zeeb #elif defined(__FreeBSD__)
1369*902136e0SBjoern A. Zeeb 	u8 *buf;
1370*902136e0SBjoern A. Zeeb #endif
1371b4c3e9b5SBjoern A. Zeeb 	u16 count;
1372b4c3e9b5SBjoern A. Zeeb 	u16 processed;
1373b4c3e9b5SBjoern A. Zeeb 
1374b4c3e9b5SBjoern A. Zeeb again:
1375b4c3e9b5SBjoern A. Zeeb 	buf = brcmf_commonring_get_read_ptr(commonring, &count);
1376b4c3e9b5SBjoern A. Zeeb 	if (buf == NULL)
1377b4c3e9b5SBjoern A. Zeeb 		return;
1378b4c3e9b5SBjoern A. Zeeb 
1379b4c3e9b5SBjoern A. Zeeb 	processed = 0;
1380b4c3e9b5SBjoern A. Zeeb 	while (count) {
1381b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_process_msgtype(msgbuf,
1382b4c3e9b5SBjoern A. Zeeb 					     buf + msgbuf->rx_dataoffset);
1383b4c3e9b5SBjoern A. Zeeb 		buf += brcmf_commonring_len_item(commonring);
1384b4c3e9b5SBjoern A. Zeeb 		processed++;
1385b4c3e9b5SBjoern A. Zeeb 		if (processed == BRCMF_MSGBUF_UPDATE_RX_PTR_THRS) {
1386b4c3e9b5SBjoern A. Zeeb 			brcmf_commonring_read_complete(commonring, processed);
1387b4c3e9b5SBjoern A. Zeeb 			processed = 0;
1388b4c3e9b5SBjoern A. Zeeb 		}
1389b4c3e9b5SBjoern A. Zeeb 		count--;
1390b4c3e9b5SBjoern A. Zeeb 	}
1391b4c3e9b5SBjoern A. Zeeb 	if (processed)
1392b4c3e9b5SBjoern A. Zeeb 		brcmf_commonring_read_complete(commonring, processed);
1393b4c3e9b5SBjoern A. Zeeb 
1394b4c3e9b5SBjoern A. Zeeb 	if (commonring->r_ptr == 0)
1395b4c3e9b5SBjoern A. Zeeb 		goto again;
1396b4c3e9b5SBjoern A. Zeeb }
1397b4c3e9b5SBjoern A. Zeeb 
1398b4c3e9b5SBjoern A. Zeeb 
brcmf_proto_msgbuf_rx_trigger(struct device * dev)1399b4c3e9b5SBjoern A. Zeeb int brcmf_proto_msgbuf_rx_trigger(struct device *dev)
1400b4c3e9b5SBjoern A. Zeeb {
1401b4c3e9b5SBjoern A. Zeeb 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1402b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = bus_if->drvr;
1403b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
1404b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring;
1405b4c3e9b5SBjoern A. Zeeb 	void *buf;
1406b4c3e9b5SBjoern A. Zeeb 	u32 flowid;
1407b4c3e9b5SBjoern A. Zeeb 	int qlen;
1408b4c3e9b5SBjoern A. Zeeb 
1409b4c3e9b5SBjoern A. Zeeb 	buf = msgbuf->commonrings[BRCMF_D2H_MSGRING_RX_COMPLETE];
1410b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_process_rx(msgbuf, buf);
1411b4c3e9b5SBjoern A. Zeeb 	buf = msgbuf->commonrings[BRCMF_D2H_MSGRING_TX_COMPLETE];
1412b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_process_rx(msgbuf, buf);
1413b4c3e9b5SBjoern A. Zeeb 	buf = msgbuf->commonrings[BRCMF_D2H_MSGRING_CONTROL_COMPLETE];
1414b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_process_rx(msgbuf, buf);
1415b4c3e9b5SBjoern A. Zeeb 
1416b4c3e9b5SBjoern A. Zeeb 	for_each_set_bit(flowid, msgbuf->txstatus_done_map,
1417b4c3e9b5SBjoern A. Zeeb 			 msgbuf->max_flowrings) {
1418b4c3e9b5SBjoern A. Zeeb 		clear_bit(flowid, msgbuf->txstatus_done_map);
1419b4c3e9b5SBjoern A. Zeeb 		commonring = msgbuf->flowrings[flowid];
1420b4c3e9b5SBjoern A. Zeeb 		qlen = brcmf_flowring_qlen(msgbuf->flow, flowid);
1421b4c3e9b5SBjoern A. Zeeb 		if ((qlen > BRCMF_MSGBUF_TRICKLE_TXWORKER_THRS) ||
1422b4c3e9b5SBjoern A. Zeeb 		    ((qlen) && (atomic_read(&commonring->outstanding_tx) <
1423b4c3e9b5SBjoern A. Zeeb 				BRCMF_MSGBUF_TRICKLE_TXWORKER_THRS)))
1424b4c3e9b5SBjoern A. Zeeb 			brcmf_msgbuf_schedule_txdata(msgbuf, flowid, true);
1425b4c3e9b5SBjoern A. Zeeb 	}
1426b4c3e9b5SBjoern A. Zeeb 
1427b4c3e9b5SBjoern A. Zeeb 	return 0;
1428b4c3e9b5SBjoern A. Zeeb }
1429b4c3e9b5SBjoern A. Zeeb 
1430b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_delete_flowring(struct brcmf_pub * drvr,u16 flowid)1431b4c3e9b5SBjoern A. Zeeb void brcmf_msgbuf_delete_flowring(struct brcmf_pub *drvr, u16 flowid)
1432b4c3e9b5SBjoern A. Zeeb {
1433b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
1434b4c3e9b5SBjoern A. Zeeb 	struct msgbuf_tx_flowring_delete_req *delete;
1435b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring;
1436b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring_del = msgbuf->flowrings[flowid];
1437b4c3e9b5SBjoern A. Zeeb 	struct brcmf_flowring *flow = msgbuf->flow;
1438b4c3e9b5SBjoern A. Zeeb 	void *ret_ptr;
1439b4c3e9b5SBjoern A. Zeeb 	u8 ifidx;
1440b4c3e9b5SBjoern A. Zeeb 	int err;
1441b4c3e9b5SBjoern A. Zeeb 	int retry = BRCMF_MAX_TXSTATUS_WAIT_RETRIES;
1442b4c3e9b5SBjoern A. Zeeb 
1443b4c3e9b5SBjoern A. Zeeb 	/* make sure it is not in txflow */
1444b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_lock(commonring_del);
1445b4c3e9b5SBjoern A. Zeeb 	flow->rings[flowid]->status = RING_CLOSING;
1446b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_unlock(commonring_del);
1447b4c3e9b5SBjoern A. Zeeb 
1448b4c3e9b5SBjoern A. Zeeb 	/* wait for commonring txflow finished */
1449b4c3e9b5SBjoern A. Zeeb 	while (retry && atomic_read(&commonring_del->outstanding_tx)) {
1450b4c3e9b5SBjoern A. Zeeb 		usleep_range(5000, 10000);
1451b4c3e9b5SBjoern A. Zeeb 		retry--;
1452b4c3e9b5SBjoern A. Zeeb 	}
1453b4c3e9b5SBjoern A. Zeeb 	if (!retry) {
1454b4c3e9b5SBjoern A. Zeeb 		brcmf_err("timed out waiting for txstatus\n");
1455b4c3e9b5SBjoern A. Zeeb 		atomic_set(&commonring_del->outstanding_tx, 0);
1456b4c3e9b5SBjoern A. Zeeb 	}
1457b4c3e9b5SBjoern A. Zeeb 
1458b4c3e9b5SBjoern A. Zeeb 	/* no need to submit if firmware can not be reached */
1459b4c3e9b5SBjoern A. Zeeb 	if (drvr->bus_if->state != BRCMF_BUS_UP) {
1460b4c3e9b5SBjoern A. Zeeb 		brcmf_dbg(MSGBUF, "bus down, flowring will be removed\n");
1461b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_remove_flowring(msgbuf, flowid);
1462b4c3e9b5SBjoern A. Zeeb 		return;
1463b4c3e9b5SBjoern A. Zeeb 	}
1464b4c3e9b5SBjoern A. Zeeb 
1465b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT];
1466b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_lock(commonring);
1467b4c3e9b5SBjoern A. Zeeb 	ret_ptr = brcmf_commonring_reserve_for_write(commonring);
1468b4c3e9b5SBjoern A. Zeeb 	if (!ret_ptr) {
1469b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "FW unaware, flowring will be removed !!\n");
1470b4c3e9b5SBjoern A. Zeeb 		brcmf_commonring_unlock(commonring);
1471b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_remove_flowring(msgbuf, flowid);
1472b4c3e9b5SBjoern A. Zeeb 		return;
1473b4c3e9b5SBjoern A. Zeeb 	}
1474b4c3e9b5SBjoern A. Zeeb 
1475b4c3e9b5SBjoern A. Zeeb 	delete = (struct msgbuf_tx_flowring_delete_req *)ret_ptr;
1476b4c3e9b5SBjoern A. Zeeb 
1477b4c3e9b5SBjoern A. Zeeb 	ifidx = brcmf_flowring_ifidx_get(msgbuf->flow, flowid);
1478b4c3e9b5SBjoern A. Zeeb 
1479b4c3e9b5SBjoern A. Zeeb 	delete->msg.msgtype = MSGBUF_TYPE_FLOW_RING_DELETE;
1480b4c3e9b5SBjoern A. Zeeb 	delete->msg.ifidx = ifidx;
1481b4c3e9b5SBjoern A. Zeeb 	delete->msg.request_id = 0;
1482b4c3e9b5SBjoern A. Zeeb 
1483b4c3e9b5SBjoern A. Zeeb 	delete->flow_ring_id = cpu_to_le16(flowid +
1484b4c3e9b5SBjoern A. Zeeb 					   BRCMF_H2D_MSGRING_FLOWRING_IDSTART);
1485b4c3e9b5SBjoern A. Zeeb 	delete->reason = 0;
1486b4c3e9b5SBjoern A. Zeeb 
1487b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(MSGBUF, "Send Flow Delete Req flow ID %d, ifindex %d\n",
1488b4c3e9b5SBjoern A. Zeeb 		  flowid, ifidx);
1489b4c3e9b5SBjoern A. Zeeb 
1490b4c3e9b5SBjoern A. Zeeb 	err = brcmf_commonring_write_complete(commonring);
1491b4c3e9b5SBjoern A. Zeeb 	brcmf_commonring_unlock(commonring);
1492b4c3e9b5SBjoern A. Zeeb 	if (err) {
1493b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "Failed to submit RING_DELETE, flowring will be removed\n");
1494b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_remove_flowring(msgbuf, flowid);
1495b4c3e9b5SBjoern A. Zeeb 	}
1496b4c3e9b5SBjoern A. Zeeb }
1497b4c3e9b5SBjoern A. Zeeb 
1498b4c3e9b5SBjoern A. Zeeb #ifdef DEBUG
brcmf_msgbuf_stats_read(struct seq_file * seq,void * data)1499b4c3e9b5SBjoern A. Zeeb static int brcmf_msgbuf_stats_read(struct seq_file *seq, void *data)
1500b4c3e9b5SBjoern A. Zeeb {
1501b4c3e9b5SBjoern A. Zeeb 	struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
1502b4c3e9b5SBjoern A. Zeeb 	struct brcmf_pub *drvr = bus_if->drvr;
1503b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
1504b4c3e9b5SBjoern A. Zeeb 	struct brcmf_commonring *commonring;
1505b4c3e9b5SBjoern A. Zeeb 	u16 i;
1506b4c3e9b5SBjoern A. Zeeb 	struct brcmf_flowring_ring *ring;
1507b4c3e9b5SBjoern A. Zeeb 	struct brcmf_flowring_hash *hash;
1508b4c3e9b5SBjoern A. Zeeb 
1509b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_CONTROL_SUBMIT];
1510b4c3e9b5SBjoern A. Zeeb 	seq_printf(seq, "h2d_ctl_submit: rp %4u, wp %4u, depth %4u\n",
1511b4c3e9b5SBjoern A. Zeeb 		   commonring->r_ptr, commonring->w_ptr, commonring->depth);
1512b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->commonrings[BRCMF_H2D_MSGRING_RXPOST_SUBMIT];
1513b4c3e9b5SBjoern A. Zeeb 	seq_printf(seq, "h2d_rx_submit:  rp %4u, wp %4u, depth %4u\n",
1514b4c3e9b5SBjoern A. Zeeb 		   commonring->r_ptr, commonring->w_ptr, commonring->depth);
1515b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->commonrings[BRCMF_D2H_MSGRING_CONTROL_COMPLETE];
1516b4c3e9b5SBjoern A. Zeeb 	seq_printf(seq, "d2h_ctl_cmplt:  rp %4u, wp %4u, depth %4u\n",
1517b4c3e9b5SBjoern A. Zeeb 		   commonring->r_ptr, commonring->w_ptr, commonring->depth);
1518b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->commonrings[BRCMF_D2H_MSGRING_TX_COMPLETE];
1519b4c3e9b5SBjoern A. Zeeb 	seq_printf(seq, "d2h_tx_cmplt:   rp %4u, wp %4u, depth %4u\n",
1520b4c3e9b5SBjoern A. Zeeb 		   commonring->r_ptr, commonring->w_ptr, commonring->depth);
1521b4c3e9b5SBjoern A. Zeeb 	commonring = msgbuf->commonrings[BRCMF_D2H_MSGRING_RX_COMPLETE];
1522b4c3e9b5SBjoern A. Zeeb 	seq_printf(seq, "d2h_rx_cmplt:   rp %4u, wp %4u, depth %4u\n",
1523b4c3e9b5SBjoern A. Zeeb 		   commonring->r_ptr, commonring->w_ptr, commonring->depth);
1524b4c3e9b5SBjoern A. Zeeb 
1525b4c3e9b5SBjoern A. Zeeb 	seq_printf(seq, "\nh2d_flowrings: depth %u\n",
1526b4c3e9b5SBjoern A. Zeeb 		   BRCMF_H2D_TXFLOWRING_MAX_ITEM);
1527b4c3e9b5SBjoern A. Zeeb 	seq_puts(seq, "Active flowrings:\n");
1528b4c3e9b5SBjoern A. Zeeb 	for (i = 0; i < msgbuf->flow->nrofrings; i++) {
1529b4c3e9b5SBjoern A. Zeeb 		if (!msgbuf->flow->rings[i])
1530b4c3e9b5SBjoern A. Zeeb 			continue;
1531b4c3e9b5SBjoern A. Zeeb 		ring = msgbuf->flow->rings[i];
1532b4c3e9b5SBjoern A. Zeeb 		if (ring->status != RING_OPEN)
1533b4c3e9b5SBjoern A. Zeeb 			continue;
1534b4c3e9b5SBjoern A. Zeeb 		commonring = msgbuf->flowrings[i];
1535b4c3e9b5SBjoern A. Zeeb 		hash = &msgbuf->flow->hash[ring->hash_id];
1536b4c3e9b5SBjoern A. Zeeb 		seq_printf(seq, "id %3u: rp %4u, wp %4u, qlen %4u, blocked %u\n"
1537b4c3e9b5SBjoern A. Zeeb 				"        ifidx %u, fifo %u, da %pM\n",
1538b4c3e9b5SBjoern A. Zeeb 				i, commonring->r_ptr, commonring->w_ptr,
1539b4c3e9b5SBjoern A. Zeeb 				skb_queue_len(&ring->skblist), ring->blocked,
1540b4c3e9b5SBjoern A. Zeeb 				hash->ifidx, hash->fifo, hash->mac);
1541b4c3e9b5SBjoern A. Zeeb 	}
1542b4c3e9b5SBjoern A. Zeeb 
1543b4c3e9b5SBjoern A. Zeeb 	return 0;
1544b4c3e9b5SBjoern A. Zeeb }
1545b4c3e9b5SBjoern A. Zeeb #else
brcmf_msgbuf_stats_read(struct seq_file * seq,void * data)1546b4c3e9b5SBjoern A. Zeeb static int brcmf_msgbuf_stats_read(struct seq_file *seq, void *data)
1547b4c3e9b5SBjoern A. Zeeb {
1548b4c3e9b5SBjoern A. Zeeb 	return 0;
1549b4c3e9b5SBjoern A. Zeeb }
1550b4c3e9b5SBjoern A. Zeeb #endif
1551b4c3e9b5SBjoern A. Zeeb 
brcmf_msgbuf_debugfs_create(struct brcmf_pub * drvr)1552b4c3e9b5SBjoern A. Zeeb static void brcmf_msgbuf_debugfs_create(struct brcmf_pub *drvr)
1553b4c3e9b5SBjoern A. Zeeb {
1554b4c3e9b5SBjoern A. Zeeb 	brcmf_debugfs_add_entry(drvr, "msgbuf_stats", brcmf_msgbuf_stats_read);
1555b4c3e9b5SBjoern A. Zeeb }
1556b4c3e9b5SBjoern A. Zeeb 
brcmf_proto_msgbuf_attach(struct brcmf_pub * drvr)1557b4c3e9b5SBjoern A. Zeeb int brcmf_proto_msgbuf_attach(struct brcmf_pub *drvr)
1558b4c3e9b5SBjoern A. Zeeb {
1559b4c3e9b5SBjoern A. Zeeb 	struct brcmf_bus_msgbuf *if_msgbuf;
1560b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf;
1561b4c3e9b5SBjoern A. Zeeb 	u64 address;
1562b4c3e9b5SBjoern A. Zeeb 	u32 count;
1563b4c3e9b5SBjoern A. Zeeb 
1564b4c3e9b5SBjoern A. Zeeb 	if_msgbuf = drvr->bus_if->msgbuf;
1565b4c3e9b5SBjoern A. Zeeb 
1566b4c3e9b5SBjoern A. Zeeb 	if (if_msgbuf->max_flowrings >= BRCMF_FLOWRING_HASHSIZE) {
1567b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "driver not configured for this many flowrings %d\n",
1568b4c3e9b5SBjoern A. Zeeb 			 if_msgbuf->max_flowrings);
1569b4c3e9b5SBjoern A. Zeeb 		if_msgbuf->max_flowrings = BRCMF_FLOWRING_HASHSIZE - 1;
1570b4c3e9b5SBjoern A. Zeeb 	}
1571b4c3e9b5SBjoern A. Zeeb 
1572b4c3e9b5SBjoern A. Zeeb 	msgbuf = kzalloc(sizeof(*msgbuf), GFP_KERNEL);
1573b4c3e9b5SBjoern A. Zeeb 	if (!msgbuf)
1574b4c3e9b5SBjoern A. Zeeb 		goto fail;
1575b4c3e9b5SBjoern A. Zeeb 
1576b4c3e9b5SBjoern A. Zeeb 	msgbuf->txflow_wq = create_singlethread_workqueue("msgbuf_txflow");
1577b4c3e9b5SBjoern A. Zeeb 	if (msgbuf->txflow_wq == NULL) {
1578b4c3e9b5SBjoern A. Zeeb 		bphy_err(drvr, "workqueue creation failed\n");
1579b4c3e9b5SBjoern A. Zeeb 		goto fail;
1580b4c3e9b5SBjoern A. Zeeb 	}
1581b4c3e9b5SBjoern A. Zeeb 	INIT_WORK(&msgbuf->txflow_work, brcmf_msgbuf_txflow_worker);
1582b4c3e9b5SBjoern A. Zeeb 	count = BITS_TO_LONGS(if_msgbuf->max_flowrings);
1583b4c3e9b5SBjoern A. Zeeb 	count = count * sizeof(unsigned long);
1584b4c3e9b5SBjoern A. Zeeb 	msgbuf->flow_map = kzalloc(count, GFP_KERNEL);
1585b4c3e9b5SBjoern A. Zeeb 	if (!msgbuf->flow_map)
1586b4c3e9b5SBjoern A. Zeeb 		goto fail;
1587b4c3e9b5SBjoern A. Zeeb 
1588b4c3e9b5SBjoern A. Zeeb 	msgbuf->txstatus_done_map = kzalloc(count, GFP_KERNEL);
1589b4c3e9b5SBjoern A. Zeeb 	if (!msgbuf->txstatus_done_map)
1590b4c3e9b5SBjoern A. Zeeb 		goto fail;
1591b4c3e9b5SBjoern A. Zeeb 
1592b4c3e9b5SBjoern A. Zeeb 	msgbuf->drvr = drvr;
1593b4c3e9b5SBjoern A. Zeeb 	msgbuf->ioctbuf = dma_alloc_coherent(drvr->bus_if->dev,
1594b4c3e9b5SBjoern A. Zeeb 					     BRCMF_TX_IOCTL_MAX_MSG_SIZE,
1595b4c3e9b5SBjoern A. Zeeb 					     &msgbuf->ioctbuf_handle,
1596b4c3e9b5SBjoern A. Zeeb 					     GFP_KERNEL);
1597b4c3e9b5SBjoern A. Zeeb 	if (!msgbuf->ioctbuf)
1598b4c3e9b5SBjoern A. Zeeb 		goto fail;
1599b4c3e9b5SBjoern A. Zeeb 	address = (u64)msgbuf->ioctbuf_handle;
1600b4c3e9b5SBjoern A. Zeeb 	msgbuf->ioctbuf_phys_hi = address >> 32;
1601b4c3e9b5SBjoern A. Zeeb 	msgbuf->ioctbuf_phys_lo = address & 0xffffffff;
1602b4c3e9b5SBjoern A. Zeeb 
1603b4c3e9b5SBjoern A. Zeeb 	drvr->proto->hdrpull = brcmf_msgbuf_hdrpull;
1604b4c3e9b5SBjoern A. Zeeb 	drvr->proto->query_dcmd = brcmf_msgbuf_query_dcmd;
1605b4c3e9b5SBjoern A. Zeeb 	drvr->proto->set_dcmd = brcmf_msgbuf_set_dcmd;
1606b4c3e9b5SBjoern A. Zeeb 	drvr->proto->tx_queue_data = brcmf_msgbuf_tx_queue_data;
1607b4c3e9b5SBjoern A. Zeeb 	drvr->proto->configure_addr_mode = brcmf_msgbuf_configure_addr_mode;
1608b4c3e9b5SBjoern A. Zeeb 	drvr->proto->delete_peer = brcmf_msgbuf_delete_peer;
1609b4c3e9b5SBjoern A. Zeeb 	drvr->proto->add_tdls_peer = brcmf_msgbuf_add_tdls_peer;
1610b4c3e9b5SBjoern A. Zeeb 	drvr->proto->rxreorder = brcmf_msgbuf_rxreorder;
1611b4c3e9b5SBjoern A. Zeeb 	drvr->proto->debugfs_create = brcmf_msgbuf_debugfs_create;
1612b4c3e9b5SBjoern A. Zeeb 	drvr->proto->pd = msgbuf;
1613b4c3e9b5SBjoern A. Zeeb 
1614b4c3e9b5SBjoern A. Zeeb 	init_waitqueue_head(&msgbuf->ioctl_resp_wait);
1615b4c3e9b5SBjoern A. Zeeb 
1616b4c3e9b5SBjoern A. Zeeb 	msgbuf->commonrings =
1617b4c3e9b5SBjoern A. Zeeb 		(struct brcmf_commonring **)if_msgbuf->commonrings;
1618b4c3e9b5SBjoern A. Zeeb 	msgbuf->flowrings = (struct brcmf_commonring **)if_msgbuf->flowrings;
1619b4c3e9b5SBjoern A. Zeeb 	msgbuf->max_flowrings = if_msgbuf->max_flowrings;
1620b4c3e9b5SBjoern A. Zeeb 	msgbuf->flowring_dma_handle =
1621b4c3e9b5SBjoern A. Zeeb 		kcalloc(msgbuf->max_flowrings,
1622b4c3e9b5SBjoern A. Zeeb 			sizeof(*msgbuf->flowring_dma_handle), GFP_KERNEL);
1623b4c3e9b5SBjoern A. Zeeb 	if (!msgbuf->flowring_dma_handle)
1624b4c3e9b5SBjoern A. Zeeb 		goto fail;
1625b4c3e9b5SBjoern A. Zeeb 
1626b4c3e9b5SBjoern A. Zeeb 	msgbuf->rx_dataoffset = if_msgbuf->rx_dataoffset;
1627b4c3e9b5SBjoern A. Zeeb 	msgbuf->max_rxbufpost = if_msgbuf->max_rxbufpost;
1628b4c3e9b5SBjoern A. Zeeb 
1629b4c3e9b5SBjoern A. Zeeb 	msgbuf->max_ioctlrespbuf = BRCMF_MSGBUF_MAX_IOCTLRESPBUF_POST;
1630b4c3e9b5SBjoern A. Zeeb 	msgbuf->max_eventbuf = BRCMF_MSGBUF_MAX_EVENTBUF_POST;
1631b4c3e9b5SBjoern A. Zeeb 
1632b4c3e9b5SBjoern A. Zeeb 	msgbuf->tx_pktids = brcmf_msgbuf_init_pktids(NR_TX_PKTIDS,
1633b4c3e9b5SBjoern A. Zeeb 						     DMA_TO_DEVICE);
1634b4c3e9b5SBjoern A. Zeeb 	if (!msgbuf->tx_pktids)
1635b4c3e9b5SBjoern A. Zeeb 		goto fail;
1636b4c3e9b5SBjoern A. Zeeb 	msgbuf->rx_pktids = brcmf_msgbuf_init_pktids(NR_RX_PKTIDS,
1637b4c3e9b5SBjoern A. Zeeb 						     DMA_FROM_DEVICE);
1638b4c3e9b5SBjoern A. Zeeb 	if (!msgbuf->rx_pktids)
1639b4c3e9b5SBjoern A. Zeeb 		goto fail;
1640b4c3e9b5SBjoern A. Zeeb 
1641b4c3e9b5SBjoern A. Zeeb 	msgbuf->flow = brcmf_flowring_attach(drvr->bus_if->dev,
1642b4c3e9b5SBjoern A. Zeeb 					     if_msgbuf->max_flowrings);
1643b4c3e9b5SBjoern A. Zeeb 	if (!msgbuf->flow)
1644b4c3e9b5SBjoern A. Zeeb 		goto fail;
1645b4c3e9b5SBjoern A. Zeeb 
1646b4c3e9b5SBjoern A. Zeeb 
1647b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(MSGBUF, "Feeding buffers, rx data %d, rx event %d, rx ioctl resp %d\n",
1648b4c3e9b5SBjoern A. Zeeb 		  msgbuf->max_rxbufpost, msgbuf->max_eventbuf,
1649b4c3e9b5SBjoern A. Zeeb 		  msgbuf->max_ioctlrespbuf);
1650b4c3e9b5SBjoern A. Zeeb 	count = 0;
1651b4c3e9b5SBjoern A. Zeeb 	do {
1652b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_rxbuf_data_fill(msgbuf);
1653b4c3e9b5SBjoern A. Zeeb 		if (msgbuf->max_rxbufpost != msgbuf->rxbufpost)
1654*902136e0SBjoern A. Zeeb #if defined(__linux__)
1655b4c3e9b5SBjoern A. Zeeb 			msleep(10);
1656*902136e0SBjoern A. Zeeb #elif defined(__FreeBSD__)
1657*902136e0SBjoern A. Zeeb 			linux_msleep(10);
1658*902136e0SBjoern A. Zeeb #endif
1659b4c3e9b5SBjoern A. Zeeb 		else
1660b4c3e9b5SBjoern A. Zeeb 			break;
1661b4c3e9b5SBjoern A. Zeeb 		count++;
1662b4c3e9b5SBjoern A. Zeeb 	} while (count < 10);
1663b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_rxbuf_event_post(msgbuf);
1664b4c3e9b5SBjoern A. Zeeb 	brcmf_msgbuf_rxbuf_ioctlresp_post(msgbuf);
1665b4c3e9b5SBjoern A. Zeeb 
1666b4c3e9b5SBjoern A. Zeeb 	INIT_WORK(&msgbuf->flowring_work, brcmf_msgbuf_flowring_worker);
1667b4c3e9b5SBjoern A. Zeeb 	spin_lock_init(&msgbuf->flowring_work_lock);
1668b4c3e9b5SBjoern A. Zeeb 	INIT_LIST_HEAD(&msgbuf->work_queue);
1669b4c3e9b5SBjoern A. Zeeb 
1670b4c3e9b5SBjoern A. Zeeb 	return 0;
1671b4c3e9b5SBjoern A. Zeeb 
1672b4c3e9b5SBjoern A. Zeeb fail:
1673b4c3e9b5SBjoern A. Zeeb 	if (msgbuf) {
1674b4c3e9b5SBjoern A. Zeeb 		kfree(msgbuf->flow_map);
1675b4c3e9b5SBjoern A. Zeeb 		kfree(msgbuf->txstatus_done_map);
1676b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_release_pktids(msgbuf);
1677b4c3e9b5SBjoern A. Zeeb 		kfree(msgbuf->flowring_dma_handle);
1678b4c3e9b5SBjoern A. Zeeb 		if (msgbuf->ioctbuf)
1679b4c3e9b5SBjoern A. Zeeb 			dma_free_coherent(drvr->bus_if->dev,
1680b4c3e9b5SBjoern A. Zeeb 					  BRCMF_TX_IOCTL_MAX_MSG_SIZE,
1681b4c3e9b5SBjoern A. Zeeb 					  msgbuf->ioctbuf,
1682b4c3e9b5SBjoern A. Zeeb 					  msgbuf->ioctbuf_handle);
1683b4c3e9b5SBjoern A. Zeeb 		if (msgbuf->txflow_wq)
1684b4c3e9b5SBjoern A. Zeeb 			destroy_workqueue(msgbuf->txflow_wq);
1685b4c3e9b5SBjoern A. Zeeb 		kfree(msgbuf);
1686b4c3e9b5SBjoern A. Zeeb 	}
1687b4c3e9b5SBjoern A. Zeeb 	return -ENOMEM;
1688b4c3e9b5SBjoern A. Zeeb }
1689b4c3e9b5SBjoern A. Zeeb 
1690b4c3e9b5SBjoern A. Zeeb 
brcmf_proto_msgbuf_detach(struct brcmf_pub * drvr)1691b4c3e9b5SBjoern A. Zeeb void brcmf_proto_msgbuf_detach(struct brcmf_pub *drvr)
1692b4c3e9b5SBjoern A. Zeeb {
1693b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf *msgbuf;
1694b4c3e9b5SBjoern A. Zeeb 	struct brcmf_msgbuf_work_item *work;
1695b4c3e9b5SBjoern A. Zeeb 
1696b4c3e9b5SBjoern A. Zeeb 	brcmf_dbg(TRACE, "Enter\n");
1697b4c3e9b5SBjoern A. Zeeb 	if (drvr->proto->pd) {
1698b4c3e9b5SBjoern A. Zeeb 		msgbuf = (struct brcmf_msgbuf *)drvr->proto->pd;
1699b4c3e9b5SBjoern A. Zeeb 		cancel_work_sync(&msgbuf->flowring_work);
1700b4c3e9b5SBjoern A. Zeeb 		while (!list_empty(&msgbuf->work_queue)) {
1701b4c3e9b5SBjoern A. Zeeb 			work = list_first_entry(&msgbuf->work_queue,
1702b4c3e9b5SBjoern A. Zeeb 						struct brcmf_msgbuf_work_item,
1703b4c3e9b5SBjoern A. Zeeb 						queue);
1704b4c3e9b5SBjoern A. Zeeb 			list_del(&work->queue);
1705b4c3e9b5SBjoern A. Zeeb 			kfree(work);
1706b4c3e9b5SBjoern A. Zeeb 		}
1707b4c3e9b5SBjoern A. Zeeb 		kfree(msgbuf->flow_map);
1708b4c3e9b5SBjoern A. Zeeb 		kfree(msgbuf->txstatus_done_map);
1709b4c3e9b5SBjoern A. Zeeb 		if (msgbuf->txflow_wq)
1710b4c3e9b5SBjoern A. Zeeb 			destroy_workqueue(msgbuf->txflow_wq);
1711b4c3e9b5SBjoern A. Zeeb 
1712b4c3e9b5SBjoern A. Zeeb 		brcmf_flowring_detach(msgbuf->flow);
1713b4c3e9b5SBjoern A. Zeeb 		dma_free_coherent(drvr->bus_if->dev,
1714b4c3e9b5SBjoern A. Zeeb 				  BRCMF_TX_IOCTL_MAX_MSG_SIZE,
1715b4c3e9b5SBjoern A. Zeeb 				  msgbuf->ioctbuf, msgbuf->ioctbuf_handle);
1716b4c3e9b5SBjoern A. Zeeb 		brcmf_msgbuf_release_pktids(msgbuf);
1717b4c3e9b5SBjoern A. Zeeb 		kfree(msgbuf->flowring_dma_handle);
1718b4c3e9b5SBjoern A. Zeeb 		kfree(msgbuf);
1719b4c3e9b5SBjoern A. Zeeb 		drvr->proto->pd = NULL;
1720b4c3e9b5SBjoern A. Zeeb 	}
1721b4c3e9b5SBjoern A. Zeeb }
1722