xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/dhd_cdc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * DHD Protocol Module for CDC and BDC.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 1999-2017, Broadcom Corporation
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *      Unless you and Broadcom execute a separate written software license
9*4882a593Smuzhiyun  * agreement governing use of this software, this software is licensed to you
10*4882a593Smuzhiyun  * under the terms of the GNU General Public License version 2 (the "GPL"),
11*4882a593Smuzhiyun  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
12*4882a593Smuzhiyun  * following added to such license:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *      As a special exception, the copyright holders of this software give you
15*4882a593Smuzhiyun  * permission to link this software with independent modules, and to copy and
16*4882a593Smuzhiyun  * distribute the resulting executable under terms of your choice, provided that
17*4882a593Smuzhiyun  * you also meet, for each linked independent module, the terms and conditions of
18*4882a593Smuzhiyun  * the license of that module.  An independent module is a module which is not
19*4882a593Smuzhiyun  * derived from this software.  The special exception does not apply to any
20*4882a593Smuzhiyun  * modifications of the software.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  *      Notwithstanding the above, under no circumstances may you combine this
23*4882a593Smuzhiyun  * software in any way with any other Broadcom software provided under a license
24*4882a593Smuzhiyun  * other than the GPL, without Broadcom's express prior written consent.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * <<Broadcom-WL-IPTag/Open:>>
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * $Id: dhd_cdc.c 692135 2017-03-26 17:19:39Z $
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * BDC is like CDC, except it includes a header for data packets to convey
32*4882a593Smuzhiyun  * packet priority over the bus, and flags (e.g. to indicate checksum status
33*4882a593Smuzhiyun  * for dongle offload.)
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include <typedefs.h>
37*4882a593Smuzhiyun #include <osl.h>
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include <bcmutils.h>
40*4882a593Smuzhiyun #include <bcmcdc.h>
41*4882a593Smuzhiyun #include <bcmendian.h>
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #include <dngl_stats.h>
44*4882a593Smuzhiyun #include <dhd.h>
45*4882a593Smuzhiyun #include <dhd_proto.h>
46*4882a593Smuzhiyun #include <dhd_bus.h>
47*4882a593Smuzhiyun #include <dhd_dbg.h>
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun #ifdef PROP_TXSTATUS
50*4882a593Smuzhiyun #include <wlfc_proto.h>
51*4882a593Smuzhiyun #include <dhd_wlfc.h>
52*4882a593Smuzhiyun #endif // endif
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #ifdef DHD_ULP
55*4882a593Smuzhiyun #include <dhd_ulp.h>
56*4882a593Smuzhiyun #endif /* DHD_ULP */
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #define RETRIES 2		/* # of retries to retrieve matching ioctl response */
59*4882a593Smuzhiyun #define BUS_HEADER_LEN	(24+DHD_SDALIGN)	/* Must be at least SDPCM_RESERVE
60*4882a593Smuzhiyun 				 * defined in dhd_sdio.c (amount of header tha might be added)
61*4882a593Smuzhiyun 				 * plus any space that might be needed for alignment padding.
62*4882a593Smuzhiyun 				 */
63*4882a593Smuzhiyun #define ROUND_UP_MARGIN	2048	/* Biggest SDIO block size possible for
64*4882a593Smuzhiyun 				 * round off at the end of buffer
65*4882a593Smuzhiyun 				 */
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun typedef struct dhd_prot {
68*4882a593Smuzhiyun 	uint16 reqid;
69*4882a593Smuzhiyun 	uint8 pending;
70*4882a593Smuzhiyun 	uint32 lastcmd;
71*4882a593Smuzhiyun 	uint8 bus_header[BUS_HEADER_LEN];
72*4882a593Smuzhiyun 	cdc_ioctl_t msg;
73*4882a593Smuzhiyun 	unsigned char buf[WLC_IOCTL_MAXLEN + ROUND_UP_MARGIN];
74*4882a593Smuzhiyun } dhd_prot_t;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun uint16
dhd_prot_get_ioctl_trans_id(dhd_pub_t * dhdp)77*4882a593Smuzhiyun dhd_prot_get_ioctl_trans_id(dhd_pub_t *dhdp)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	/* SDIO does not have ioctl_trans_id yet, so return -1 */
80*4882a593Smuzhiyun 	return -1;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun static int
dhdcdc_msg(dhd_pub_t * dhd)84*4882a593Smuzhiyun dhdcdc_msg(dhd_pub_t *dhd)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	int err = 0;
87*4882a593Smuzhiyun 	dhd_prot_t *prot = dhd->prot;
88*4882a593Smuzhiyun 	int len = ltoh32(prot->msg.len) + sizeof(cdc_ioctl_t);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	DHD_OS_WAKE_LOCK(dhd);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	/* NOTE : cdc->msg.len holds the desired length of the buffer to be
95*4882a593Smuzhiyun 	 *        returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
96*4882a593Smuzhiyun 	 *	  is actually sent to the dongle
97*4882a593Smuzhiyun 	 */
98*4882a593Smuzhiyun 	if (len > CDC_MAX_MSG_SIZE)
99*4882a593Smuzhiyun 		len = CDC_MAX_MSG_SIZE;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	/* Send request */
102*4882a593Smuzhiyun 	err = dhd_bus_txctl(dhd->bus, (uchar*)&prot->msg, len);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	DHD_OS_WAKE_UNLOCK(dhd);
105*4882a593Smuzhiyun 	return err;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun static int
dhdcdc_cmplt(dhd_pub_t * dhd,uint32 id,uint32 len)109*4882a593Smuzhiyun dhdcdc_cmplt(dhd_pub_t *dhd, uint32 id, uint32 len)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	int ret;
112*4882a593Smuzhiyun 	int cdc_len = len + sizeof(cdc_ioctl_t);
113*4882a593Smuzhiyun 	dhd_prot_t *prot = dhd->prot;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	do {
118*4882a593Smuzhiyun 		ret = dhd_bus_rxctl(dhd->bus, (uchar*)&prot->msg, cdc_len);
119*4882a593Smuzhiyun 		if (ret < 0)
120*4882a593Smuzhiyun 			break;
121*4882a593Smuzhiyun 	} while (CDC_IOC_ID(ltoh32(prot->msg.flags)) != id);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	return ret;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun static int
dhdcdc_query_ioctl(dhd_pub_t * dhd,int ifidx,uint cmd,void * buf,uint len,uint8 action)127*4882a593Smuzhiyun dhdcdc_query_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf, uint len, uint8 action)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	dhd_prot_t *prot = dhd->prot;
130*4882a593Smuzhiyun 	cdc_ioctl_t *msg = &prot->msg;
131*4882a593Smuzhiyun 	int ret = 0, retries = 0;
132*4882a593Smuzhiyun 	uint32 id, flags = 0;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
135*4882a593Smuzhiyun 	DHD_CTL(("%s: cmd %d len %d\n", __FUNCTION__, cmd, len));
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/* Respond "bcmerror" and "bcmerrorstr" with local cache */
138*4882a593Smuzhiyun 	if (cmd == WLC_GET_VAR && buf)
139*4882a593Smuzhiyun 	{
140*4882a593Smuzhiyun 		if (!strcmp((char *)buf, "bcmerrorstr"))
141*4882a593Smuzhiyun 		{
142*4882a593Smuzhiyun 			strncpy((char *)buf, bcmerrorstr(dhd->dongle_error), BCME_STRLEN);
143*4882a593Smuzhiyun 			goto done;
144*4882a593Smuzhiyun 		}
145*4882a593Smuzhiyun 		else if (!strcmp((char *)buf, "bcmerror"))
146*4882a593Smuzhiyun 		{
147*4882a593Smuzhiyun 			*(int *)buf = dhd->dongle_error;
148*4882a593Smuzhiyun 			goto done;
149*4882a593Smuzhiyun 		}
150*4882a593Smuzhiyun 	}
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	memset(msg, 0, sizeof(cdc_ioctl_t));
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun #ifdef BCMSPI
155*4882a593Smuzhiyun 	/* 11bit gSPI bus allows 2048bytes of max-data.  We restrict 'len'
156*4882a593Smuzhiyun 	 * value which is 8Kbytes for various 'get' commands to 2000.  48 bytes are
157*4882a593Smuzhiyun 	 * left for sw headers and misc.
158*4882a593Smuzhiyun 	 */
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	if (len > 2000) {
161*4882a593Smuzhiyun 		DHD_ERROR(("dhdcdc_query_ioctl: len is truncated to 2000 bytes\n"));
162*4882a593Smuzhiyun 		len = 2000;
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun #ifdef BCMQT
165*4882a593Smuzhiyun 	/* WAR: packet length limited for SPI host issue in FIFO mode on Zebu */
166*4882a593Smuzhiyun 	if (len > 460) {
167*4882a593Smuzhiyun 		DHD_ERROR(("len is truncated to 460 bytes on Zebu\n"));
168*4882a593Smuzhiyun 		len = 460;
169*4882a593Smuzhiyun 	}
170*4882a593Smuzhiyun #endif /* BCMQT */
171*4882a593Smuzhiyun #endif /* BCMSPI */
172*4882a593Smuzhiyun 	msg->cmd = htol32(cmd);
173*4882a593Smuzhiyun 	msg->len = htol32(len);
174*4882a593Smuzhiyun 	msg->flags = (++prot->reqid << CDCF_IOC_ID_SHIFT);
175*4882a593Smuzhiyun 	CDC_SET_IF_IDX(msg, ifidx);
176*4882a593Smuzhiyun 	/* add additional action bits */
177*4882a593Smuzhiyun 	action &= WL_IOCTL_ACTION_MASK;
178*4882a593Smuzhiyun 	msg->flags |= (action << CDCF_IOC_ACTION_SHIFT);
179*4882a593Smuzhiyun 	msg->flags = htol32(msg->flags);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	if (buf)
182*4882a593Smuzhiyun 		memcpy(prot->buf, buf, len);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	if ((ret = dhdcdc_msg(dhd)) < 0) {
185*4882a593Smuzhiyun 		if (!dhd->hang_was_sent)
186*4882a593Smuzhiyun 		DHD_ERROR(("dhdcdc_query_ioctl: dhdcdc_msg failed w/status %d\n", ret));
187*4882a593Smuzhiyun 		goto done;
188*4882a593Smuzhiyun 	}
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun retry:
191*4882a593Smuzhiyun 	/* wait for interrupt and get first fragment */
192*4882a593Smuzhiyun 	if ((ret = dhdcdc_cmplt(dhd, prot->reqid, len)) < 0)
193*4882a593Smuzhiyun 		goto done;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	flags = ltoh32(msg->flags);
196*4882a593Smuzhiyun 	id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	if ((id < prot->reqid) && (++retries < RETRIES))
199*4882a593Smuzhiyun 		goto retry;
200*4882a593Smuzhiyun 	if (id != prot->reqid) {
201*4882a593Smuzhiyun 		DHD_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
202*4882a593Smuzhiyun 		           dhd_ifname(dhd, ifidx), __FUNCTION__, id, prot->reqid));
203*4882a593Smuzhiyun 		ret = -EINVAL;
204*4882a593Smuzhiyun 		goto done;
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	/* Copy info buffer */
208*4882a593Smuzhiyun 	if (buf)
209*4882a593Smuzhiyun 	{
210*4882a593Smuzhiyun 		if (ret < (int)len)
211*4882a593Smuzhiyun 			len = ret;
212*4882a593Smuzhiyun 		memcpy(buf, (void*) prot->buf, len);
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	/* Check the ERROR flag */
216*4882a593Smuzhiyun 	if (flags & CDCF_IOC_ERROR)
217*4882a593Smuzhiyun 	{
218*4882a593Smuzhiyun 		ret = ltoh32(msg->status);
219*4882a593Smuzhiyun 		/* Cache error from dongle */
220*4882a593Smuzhiyun 		dhd->dongle_error = ret;
221*4882a593Smuzhiyun 	}
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun done:
224*4882a593Smuzhiyun 	return ret;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun #ifdef DHD_PM_CONTROL_FROM_FILE
228*4882a593Smuzhiyun extern bool g_pm_control;
229*4882a593Smuzhiyun #endif /* DHD_PM_CONTROL_FROM_FILE */
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun static int
dhdcdc_set_ioctl(dhd_pub_t * dhd,int ifidx,uint cmd,void * buf,uint len,uint8 action)232*4882a593Smuzhiyun dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf, uint len, uint8 action)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun 	dhd_prot_t *prot = dhd->prot;
235*4882a593Smuzhiyun 	cdc_ioctl_t *msg = &prot->msg;
236*4882a593Smuzhiyun 	int ret = 0;
237*4882a593Smuzhiyun 	uint32 flags, id;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
240*4882a593Smuzhiyun 	DHD_CTL(("%s: cmd %d len %d\n", __FUNCTION__, cmd, len));
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	if (dhd->busstate == DHD_BUS_DOWN) {
243*4882a593Smuzhiyun 		DHD_ERROR(("%s : bus is down. we have nothing to do\n", __FUNCTION__));
244*4882a593Smuzhiyun 		return -EIO;
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/* don't talk to the dongle if fw is about to be reloaded */
248*4882a593Smuzhiyun 	if (dhd->hang_was_sent) {
249*4882a593Smuzhiyun 		DHD_ERROR(("%s: HANG was sent up earlier. Not talking to the chip\n",
250*4882a593Smuzhiyun 			__FUNCTION__));
251*4882a593Smuzhiyun 		return -EIO;
252*4882a593Smuzhiyun 	}
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	if (cmd == WLC_SET_PM) {
255*4882a593Smuzhiyun #ifdef DHD_PM_CONTROL_FROM_FILE
256*4882a593Smuzhiyun 		if (g_pm_control == TRUE) {
257*4882a593Smuzhiyun 			DHD_ERROR(("%s: SET PM ignored!(Requested:%d)\n",
258*4882a593Smuzhiyun 				__FUNCTION__, buf ? *(char *)buf : 0));
259*4882a593Smuzhiyun 			goto done;
260*4882a593Smuzhiyun 		}
261*4882a593Smuzhiyun #endif /* DHD_PM_CONTROL_FROM_FILE */
262*4882a593Smuzhiyun #if defined(WLAIBSS)
263*4882a593Smuzhiyun 		if (dhd->op_mode == DHD_FLAG_IBSS_MODE) {
264*4882a593Smuzhiyun 			DHD_ERROR(("%s: SET PM ignored for IBSS!(Requested:%d)\n",
265*4882a593Smuzhiyun 				__FUNCTION__, buf ? *(char *)buf : 0));
266*4882a593Smuzhiyun 			goto done;
267*4882a593Smuzhiyun 		}
268*4882a593Smuzhiyun #endif /* WLAIBSS */
269*4882a593Smuzhiyun 		DHD_TRACE_HW4(("%s: SET PM to %d\n", __FUNCTION__, buf ? *(char *)buf : 0));
270*4882a593Smuzhiyun 	}
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	memset(msg, 0, sizeof(cdc_ioctl_t));
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	msg->cmd = htol32(cmd);
275*4882a593Smuzhiyun 	msg->len = htol32(len);
276*4882a593Smuzhiyun 	msg->flags = (++prot->reqid << CDCF_IOC_ID_SHIFT);
277*4882a593Smuzhiyun 	CDC_SET_IF_IDX(msg, ifidx);
278*4882a593Smuzhiyun 	/* add additional action bits */
279*4882a593Smuzhiyun 	action &= WL_IOCTL_ACTION_MASK;
280*4882a593Smuzhiyun 	msg->flags |= (action << CDCF_IOC_ACTION_SHIFT) | CDCF_IOC_SET;
281*4882a593Smuzhiyun 	msg->flags = htol32(msg->flags);
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	if (buf)
284*4882a593Smuzhiyun 		memcpy(prot->buf, buf, len);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun #ifdef DHD_ULP
287*4882a593Smuzhiyun 	if (buf && (!strncmp(buf, "ulp", sizeof("ulp")))) {
288*4882a593Smuzhiyun 		/* force all the writes after this point to NOT to use cached sbwad value */
289*4882a593Smuzhiyun 		dhd_ulp_disable_cached_sbwad(dhd);
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun #endif /* DHD_ULP */
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	if ((ret = dhdcdc_msg(dhd)) < 0) {
294*4882a593Smuzhiyun 		DHD_ERROR(("%s: dhdcdc_msg failed w/status %d\n", __FUNCTION__, ret));
295*4882a593Smuzhiyun 		goto done;
296*4882a593Smuzhiyun 	}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if ((ret = dhdcdc_cmplt(dhd, prot->reqid, len)) < 0)
299*4882a593Smuzhiyun 		goto done;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	flags = ltoh32(msg->flags);
302*4882a593Smuzhiyun 	id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	if (id != prot->reqid) {
305*4882a593Smuzhiyun 		DHD_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
306*4882a593Smuzhiyun 		           dhd_ifname(dhd, ifidx), __FUNCTION__, id, prot->reqid));
307*4882a593Smuzhiyun 		ret = -EINVAL;
308*4882a593Smuzhiyun 		goto done;
309*4882a593Smuzhiyun 	}
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun #ifdef DHD_ULP
312*4882a593Smuzhiyun 	/* For ulp prototyping temporary */
313*4882a593Smuzhiyun 	if ((ret = dhd_ulp_check_ulp_request(dhd, buf)) < 0)
314*4882a593Smuzhiyun 		goto done;
315*4882a593Smuzhiyun #endif /* DHD_ULP */
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	/* Check the ERROR flag */
318*4882a593Smuzhiyun 	if (flags & CDCF_IOC_ERROR)
319*4882a593Smuzhiyun 	{
320*4882a593Smuzhiyun 		ret = ltoh32(msg->status);
321*4882a593Smuzhiyun 		/* Cache error from dongle */
322*4882a593Smuzhiyun 		dhd->dongle_error = ret;
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun done:
326*4882a593Smuzhiyun 	return ret;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun int
dhd_prot_ioctl(dhd_pub_t * dhd,int ifidx,wl_ioctl_t * ioc,void * buf,int len)330*4882a593Smuzhiyun dhd_prot_ioctl(dhd_pub_t *dhd, int ifidx, wl_ioctl_t * ioc, void * buf, int len)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	dhd_prot_t *prot = dhd->prot;
333*4882a593Smuzhiyun 	int ret = -1;
334*4882a593Smuzhiyun 	uint8 action;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	if ((dhd->busstate == DHD_BUS_DOWN) || dhd->hang_was_sent) {
337*4882a593Smuzhiyun 		DHD_ERROR(("%s : bus is down. we have nothing to do - bs: %d, has: %d\n",
338*4882a593Smuzhiyun 				__FUNCTION__, dhd->busstate, dhd->hang_was_sent));
339*4882a593Smuzhiyun 		goto done;
340*4882a593Smuzhiyun 	}
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	ASSERT(len <= WLC_IOCTL_MAXLEN);
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	if (len > WLC_IOCTL_MAXLEN)
347*4882a593Smuzhiyun 		goto done;
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	if (prot->pending == TRUE) {
350*4882a593Smuzhiyun 		DHD_ERROR(("CDC packet is pending!!!! cmd=0x%x (%lu) lastcmd=0x%x (%lu)\n",
351*4882a593Smuzhiyun 			ioc->cmd, (unsigned long)ioc->cmd, prot->lastcmd,
352*4882a593Smuzhiyun 			(unsigned long)prot->lastcmd));
353*4882a593Smuzhiyun 		if ((ioc->cmd == WLC_SET_VAR) || (ioc->cmd == WLC_GET_VAR)) {
354*4882a593Smuzhiyun 			DHD_TRACE(("iovar cmd=%s\n", buf ? (char*)buf : "\0"));
355*4882a593Smuzhiyun 		}
356*4882a593Smuzhiyun 		goto done;
357*4882a593Smuzhiyun 	}
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	prot->pending = TRUE;
360*4882a593Smuzhiyun 	prot->lastcmd = ioc->cmd;
361*4882a593Smuzhiyun 	action = ioc->set;
362*4882a593Smuzhiyun 	if (action & WL_IOCTL_ACTION_SET)
363*4882a593Smuzhiyun 		ret = dhdcdc_set_ioctl(dhd, ifidx, ioc->cmd, buf, len, action);
364*4882a593Smuzhiyun 	else {
365*4882a593Smuzhiyun 		ret = dhdcdc_query_ioctl(dhd, ifidx, ioc->cmd, buf, len, action);
366*4882a593Smuzhiyun 		if (ret > 0)
367*4882a593Smuzhiyun 			ioc->used = ret - sizeof(cdc_ioctl_t);
368*4882a593Smuzhiyun 	}
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	/* Too many programs assume ioctl() returns 0 on success */
371*4882a593Smuzhiyun 	if (ret >= 0)
372*4882a593Smuzhiyun 		ret = 0;
373*4882a593Smuzhiyun 	else {
374*4882a593Smuzhiyun 		cdc_ioctl_t *msg = &prot->msg;
375*4882a593Smuzhiyun 		ioc->needed = ltoh32(msg->len); /* len == needed when set/query fails from dongle */
376*4882a593Smuzhiyun 	}
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	/* Intercept the wme_dp ioctl here */
379*4882a593Smuzhiyun 	if ((!ret) && (ioc->cmd == WLC_SET_VAR) && (!strcmp(buf, "wme_dp"))) {
380*4882a593Smuzhiyun 		int slen, val = 0;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 		slen = strlen("wme_dp") + 1;
383*4882a593Smuzhiyun 		if (len >= (int)(slen + sizeof(int)))
384*4882a593Smuzhiyun 			bcopy(((char *)buf + slen), &val, sizeof(int));
385*4882a593Smuzhiyun 		dhd->wme_dp = (uint8) ltoh32(val);
386*4882a593Smuzhiyun 	}
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	prot->pending = FALSE;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun done:
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	return ret;
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun int
dhd_prot_iovar_op(dhd_pub_t * dhdp,const char * name,void * params,int plen,void * arg,int len,bool set)396*4882a593Smuzhiyun dhd_prot_iovar_op(dhd_pub_t *dhdp, const char *name,
397*4882a593Smuzhiyun                   void *params, int plen, void *arg, int len, bool set)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun 	return BCME_UNSUPPORTED;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun void
dhd_prot_dump(dhd_pub_t * dhdp,struct bcmstrbuf * strbuf)403*4882a593Smuzhiyun dhd_prot_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun 	if (!dhdp || !dhdp->prot) {
406*4882a593Smuzhiyun 		return;
407*4882a593Smuzhiyun 	}
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "Protocol CDC: reqid %d\n", dhdp->prot->reqid);
410*4882a593Smuzhiyun #ifdef PROP_TXSTATUS
411*4882a593Smuzhiyun 	dhd_wlfc_dump(dhdp, strbuf);
412*4882a593Smuzhiyun #endif // endif
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun /*	The FreeBSD PKTPUSH could change the packet buf pinter
416*4882a593Smuzhiyun 	so we need to make it changable
417*4882a593Smuzhiyun */
418*4882a593Smuzhiyun #define PKTBUF pktbuf
419*4882a593Smuzhiyun void
dhd_prot_hdrpush(dhd_pub_t * dhd,int ifidx,void * PKTBUF)420*4882a593Smuzhiyun dhd_prot_hdrpush(dhd_pub_t *dhd, int ifidx, void *PKTBUF)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun #ifdef BDC
423*4882a593Smuzhiyun 	struct bdc_header *h;
424*4882a593Smuzhiyun #endif /* BDC */
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun #ifdef BDC
429*4882a593Smuzhiyun 	/* Push BDC header used to convey priority for buses that don't */
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	PKTPUSH(dhd->osh, PKTBUF, BDC_HEADER_LEN);
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	h = (struct bdc_header *)PKTDATA(dhd->osh, PKTBUF);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
436*4882a593Smuzhiyun 	if (PKTSUMNEEDED(PKTBUF))
437*4882a593Smuzhiyun 		h->flags |= BDC_FLAG_SUM_NEEDED;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	h->priority = (PKTPRIO(PKTBUF) & BDC_PRIORITY_MASK);
440*4882a593Smuzhiyun 	h->flags2 = 0;
441*4882a593Smuzhiyun 	h->dataOffset = 0;
442*4882a593Smuzhiyun #endif /* BDC */
443*4882a593Smuzhiyun 	BDC_SET_IF_IDX(h, ifidx);
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun #undef PKTBUF	/* Only defined in the above routine */
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun uint
dhd_prot_hdrlen(dhd_pub_t * dhd,void * PKTBUF)448*4882a593Smuzhiyun dhd_prot_hdrlen(dhd_pub_t *dhd, void *PKTBUF)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun 	uint hdrlen = 0;
451*4882a593Smuzhiyun #ifdef BDC
452*4882a593Smuzhiyun 	/* Length of BDC(+WLFC) headers pushed */
453*4882a593Smuzhiyun 	hdrlen = BDC_HEADER_LEN + (((struct bdc_header *)PKTBUF)->dataOffset * 4);
454*4882a593Smuzhiyun #endif // endif
455*4882a593Smuzhiyun 	return hdrlen;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun int
dhd_prot_hdrpull(dhd_pub_t * dhd,int * ifidx,void * pktbuf,uchar * reorder_buf_info,uint * reorder_info_len)459*4882a593Smuzhiyun dhd_prot_hdrpull(dhd_pub_t *dhd, int *ifidx, void *pktbuf, uchar *reorder_buf_info,
460*4882a593Smuzhiyun 	uint *reorder_info_len)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun #ifdef BDC
463*4882a593Smuzhiyun 	struct bdc_header *h;
464*4882a593Smuzhiyun #endif // endif
465*4882a593Smuzhiyun 	uint8 data_offset = 0;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun #ifdef BDC
470*4882a593Smuzhiyun 	if (reorder_info_len)
471*4882a593Smuzhiyun 		*reorder_info_len = 0;
472*4882a593Smuzhiyun 	/* Pop BDC header used to convey priority for buses that don't */
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	if (PKTLEN(dhd->osh, pktbuf) < BDC_HEADER_LEN) {
475*4882a593Smuzhiyun 		DHD_ERROR(("%s: rx data too short (%d < %d)\n", __FUNCTION__,
476*4882a593Smuzhiyun 		           PKTLEN(dhd->osh, pktbuf), BDC_HEADER_LEN));
477*4882a593Smuzhiyun 		return BCME_ERROR;
478*4882a593Smuzhiyun 	}
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	h = (struct bdc_header *)PKTDATA(dhd->osh, pktbuf);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	if (!ifidx) {
483*4882a593Smuzhiyun 		/* for tx packet, skip the analysis */
484*4882a593Smuzhiyun 		data_offset = h->dataOffset;
485*4882a593Smuzhiyun 		PKTPULL(dhd->osh, pktbuf, BDC_HEADER_LEN);
486*4882a593Smuzhiyun 		goto exit;
487*4882a593Smuzhiyun 	}
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	*ifidx = BDC_GET_IF_IDX(h);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) != BDC_PROTO_VER) {
492*4882a593Smuzhiyun 		DHD_ERROR(("%s: non-BDC packet received, flags = 0x%x\n",
493*4882a593Smuzhiyun 		           dhd_ifname(dhd, *ifidx), h->flags));
494*4882a593Smuzhiyun 		if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) == BDC_PROTO_VER_1)
495*4882a593Smuzhiyun 			h->dataOffset = 0;
496*4882a593Smuzhiyun 		else
497*4882a593Smuzhiyun 		return BCME_ERROR;
498*4882a593Smuzhiyun 	}
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	if (h->flags & BDC_FLAG_SUM_GOOD) {
501*4882a593Smuzhiyun 		DHD_INFO(("%s: BDC packet received with good rx-csum, flags 0x%x\n",
502*4882a593Smuzhiyun 		          dhd_ifname(dhd, *ifidx), h->flags));
503*4882a593Smuzhiyun 		PKTSETSUMGOOD(pktbuf, TRUE);
504*4882a593Smuzhiyun 	}
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	PKTSETPRIO(pktbuf, (h->priority & BDC_PRIORITY_MASK));
507*4882a593Smuzhiyun 	data_offset = h->dataOffset;
508*4882a593Smuzhiyun 	PKTPULL(dhd->osh, pktbuf, BDC_HEADER_LEN);
509*4882a593Smuzhiyun #endif /* BDC */
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun #ifdef PROP_TXSTATUS
512*4882a593Smuzhiyun 	if (!DHD_PKTTAG_PKTDIR(PKTTAG(pktbuf))) {
513*4882a593Smuzhiyun 		/*
514*4882a593Smuzhiyun 		- parse txstatus only for packets that came from the firmware
515*4882a593Smuzhiyun 		*/
516*4882a593Smuzhiyun 		dhd_wlfc_parse_header_info(dhd, pktbuf, (data_offset << 2),
517*4882a593Smuzhiyun 			reorder_buf_info, reorder_info_len);
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	}
520*4882a593Smuzhiyun #endif /* PROP_TXSTATUS */
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun exit:
523*4882a593Smuzhiyun 	PKTPULL(dhd->osh, pktbuf, (data_offset << 2));
524*4882a593Smuzhiyun 	return 0;
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun int
dhd_prot_attach(dhd_pub_t * dhd)528*4882a593Smuzhiyun dhd_prot_attach(dhd_pub_t *dhd)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun 	dhd_prot_t *cdc;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	if (!(cdc = (dhd_prot_t *)DHD_OS_PREALLOC(dhd, DHD_PREALLOC_PROT, sizeof(dhd_prot_t)))) {
533*4882a593Smuzhiyun 		DHD_ERROR(("%s: kmalloc failed\n", __FUNCTION__));
534*4882a593Smuzhiyun 		goto fail;
535*4882a593Smuzhiyun 	}
536*4882a593Smuzhiyun 	memset(cdc, 0, sizeof(dhd_prot_t));
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	/* ensure that the msg buf directly follows the cdc msg struct */
539*4882a593Smuzhiyun 	if ((uintptr)(&cdc->msg + 1) != (uintptr)cdc->buf) {
540*4882a593Smuzhiyun 		DHD_ERROR(("dhd_prot_t is not correctly defined\n"));
541*4882a593Smuzhiyun 		goto fail;
542*4882a593Smuzhiyun 	}
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	dhd->prot = cdc;
545*4882a593Smuzhiyun #ifdef BDC
546*4882a593Smuzhiyun 	dhd->hdrlen += BDC_HEADER_LEN;
547*4882a593Smuzhiyun #endif // endif
548*4882a593Smuzhiyun 	dhd->maxctl = WLC_IOCTL_MAXLEN + sizeof(cdc_ioctl_t) + ROUND_UP_MARGIN;
549*4882a593Smuzhiyun 	return 0;
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun fail:
552*4882a593Smuzhiyun 	if (cdc != NULL)
553*4882a593Smuzhiyun 		DHD_OS_PREFREE(dhd, cdc, sizeof(dhd_prot_t));
554*4882a593Smuzhiyun 	return BCME_NOMEM;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /* ~NOTE~ What if another thread is waiting on the semaphore?  Holding it? */
558*4882a593Smuzhiyun void
dhd_prot_detach(dhd_pub_t * dhd)559*4882a593Smuzhiyun dhd_prot_detach(dhd_pub_t *dhd)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun #ifdef PROP_TXSTATUS
562*4882a593Smuzhiyun 	dhd_wlfc_deinit(dhd);
563*4882a593Smuzhiyun #endif // endif
564*4882a593Smuzhiyun 	DHD_OS_PREFREE(dhd, dhd->prot, sizeof(dhd_prot_t));
565*4882a593Smuzhiyun 	dhd->prot = NULL;
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun void
dhd_prot_dstats(dhd_pub_t * dhd)569*4882a593Smuzhiyun dhd_prot_dstats(dhd_pub_t *dhd)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	/*  copy bus stats */
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	dhd->dstats.tx_packets = dhd->tx_packets;
574*4882a593Smuzhiyun 	dhd->dstats.tx_errors = dhd->tx_errors;
575*4882a593Smuzhiyun 	dhd->dstats.rx_packets = dhd->rx_packets;
576*4882a593Smuzhiyun 	dhd->dstats.rx_errors = dhd->rx_errors;
577*4882a593Smuzhiyun 	dhd->dstats.rx_dropped = dhd->rx_dropped;
578*4882a593Smuzhiyun 	dhd->dstats.multicast = dhd->rx_multicast;
579*4882a593Smuzhiyun 	return;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun int
dhd_sync_with_dongle(dhd_pub_t * dhd)583*4882a593Smuzhiyun dhd_sync_with_dongle(dhd_pub_t *dhd)
584*4882a593Smuzhiyun {
585*4882a593Smuzhiyun 	int ret = 0;
586*4882a593Smuzhiyun 	wlc_rev_info_t revinfo;
587*4882a593Smuzhiyun #ifndef OEM_ANDROID
588*4882a593Smuzhiyun 	char buf[128];
589*4882a593Smuzhiyun #endif /* OEM_ANDROID */
590*4882a593Smuzhiyun 	DHD_TRACE(("%s: Enter\n", __FUNCTION__));
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun #ifndef OEM_ANDROID
593*4882a593Smuzhiyun 	/* Get the device MAC address */
594*4882a593Smuzhiyun 	strcpy(buf, "cur_etheraddr");
595*4882a593Smuzhiyun 	ret = dhd_wl_ioctl_cmd(dhd, WLC_GET_VAR, buf, sizeof(buf), FALSE, 0);
596*4882a593Smuzhiyun 	if (ret < 0)
597*4882a593Smuzhiyun 		goto done;
598*4882a593Smuzhiyun 	memcpy(dhd->mac.octet, buf, ETHER_ADDR_LEN);
599*4882a593Smuzhiyun #endif /* OEM_ANDROID */
600*4882a593Smuzhiyun #ifdef DHD_FW_COREDUMP
601*4882a593Smuzhiyun 	/* Check the memdump capability */
602*4882a593Smuzhiyun 	dhd_get_memdump_info(dhd);
603*4882a593Smuzhiyun #endif /* DHD_FW_COREDUMP */
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun #ifdef BCMASSERT_LOG
606*4882a593Smuzhiyun 	dhd_get_assert_info(dhd);
607*4882a593Smuzhiyun #endif /* BCMASSERT_LOG */
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	/* Get the device rev info */
610*4882a593Smuzhiyun 	memset(&revinfo, 0, sizeof(revinfo));
611*4882a593Smuzhiyun 	ret = dhd_wl_ioctl_cmd(dhd, WLC_GET_REVINFO, &revinfo, sizeof(revinfo), FALSE, 0);
612*4882a593Smuzhiyun 	if (ret < 0)
613*4882a593Smuzhiyun 		goto done;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	DHD_SSSR_DUMP_INIT(dhd);
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	dhd_process_cid_mac(dhd, TRUE);
618*4882a593Smuzhiyun 	ret = dhd_preinit_ioctls(dhd);
619*4882a593Smuzhiyun 	dhd_process_cid_mac(dhd, FALSE);
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	/* Always assumes wl for now */
622*4882a593Smuzhiyun 	dhd->iswl = TRUE;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun done:
625*4882a593Smuzhiyun 	return ret;
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun 
dhd_prot_init(dhd_pub_t * dhd)628*4882a593Smuzhiyun int dhd_prot_init(dhd_pub_t *dhd)
629*4882a593Smuzhiyun {
630*4882a593Smuzhiyun 	return BCME_OK;
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun void
dhd_prot_stop(dhd_pub_t * dhd)634*4882a593Smuzhiyun dhd_prot_stop(dhd_pub_t *dhd)
635*4882a593Smuzhiyun {
636*4882a593Smuzhiyun /* Nothing to do for CDC */
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun static void
dhd_get_hostreorder_pkts(void * osh,struct reorder_info * ptr,void ** pkt,uint32 * pkt_count,void ** pplast,uint8 start,uint8 end)640*4882a593Smuzhiyun dhd_get_hostreorder_pkts(void *osh, struct reorder_info *ptr, void **pkt,
641*4882a593Smuzhiyun 	uint32 *pkt_count, void **pplast, uint8 start, uint8 end)
642*4882a593Smuzhiyun {
643*4882a593Smuzhiyun 	void *plast = NULL, *p;
644*4882a593Smuzhiyun 	uint32 pkt_cnt = 0;
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	if (ptr->pend_pkts == 0) {
647*4882a593Smuzhiyun 		DHD_REORDER(("%s: no packets in reorder queue \n", __FUNCTION__));
648*4882a593Smuzhiyun 		*pplast = NULL;
649*4882a593Smuzhiyun 		*pkt_count = 0;
650*4882a593Smuzhiyun 		*pkt = NULL;
651*4882a593Smuzhiyun 		return;
652*4882a593Smuzhiyun 	}
653*4882a593Smuzhiyun 	do {
654*4882a593Smuzhiyun 		p = (void *)(ptr->p[start]);
655*4882a593Smuzhiyun 		ptr->p[start] = NULL;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 		if (p != NULL) {
658*4882a593Smuzhiyun 			if (plast == NULL)
659*4882a593Smuzhiyun 				*pkt = p;
660*4882a593Smuzhiyun 			else
661*4882a593Smuzhiyun 				PKTSETNEXT(osh, plast, p);
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 			plast = p;
664*4882a593Smuzhiyun 			pkt_cnt++;
665*4882a593Smuzhiyun 		}
666*4882a593Smuzhiyun 		start++;
667*4882a593Smuzhiyun 		if (start > ptr->max_idx)
668*4882a593Smuzhiyun 			start = 0;
669*4882a593Smuzhiyun 	} while (start != end);
670*4882a593Smuzhiyun 	*pplast = plast;
671*4882a593Smuzhiyun 	*pkt_count = pkt_cnt;
672*4882a593Smuzhiyun 	ptr->pend_pkts -= (uint8)pkt_cnt;
673*4882a593Smuzhiyun }
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun int
dhd_process_pkt_reorder_info(dhd_pub_t * dhd,uchar * reorder_info_buf,uint reorder_info_len,void ** pkt,uint32 * pkt_count)676*4882a593Smuzhiyun dhd_process_pkt_reorder_info(dhd_pub_t *dhd, uchar *reorder_info_buf, uint reorder_info_len,
677*4882a593Smuzhiyun 	void **pkt, uint32 *pkt_count)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	uint8 flow_id, max_idx, cur_idx, exp_idx;
680*4882a593Smuzhiyun 	struct reorder_info *ptr;
681*4882a593Smuzhiyun 	uint8 flags;
682*4882a593Smuzhiyun 	void *cur_pkt, *plast = NULL;
683*4882a593Smuzhiyun 	uint32 cnt = 0;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	if (pkt == NULL) {
686*4882a593Smuzhiyun 		if (pkt_count != NULL)
687*4882a593Smuzhiyun 			*pkt_count = 0;
688*4882a593Smuzhiyun 		return 0;
689*4882a593Smuzhiyun 	}
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	flow_id = reorder_info_buf[WLHOST_REORDERDATA_FLOWID_OFFSET];
692*4882a593Smuzhiyun 	flags = reorder_info_buf[WLHOST_REORDERDATA_FLAGS_OFFSET];
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	DHD_REORDER(("flow_id %d, flags 0x%02x, idx(%d, %d, %d)\n", flow_id, flags,
695*4882a593Smuzhiyun 		reorder_info_buf[WLHOST_REORDERDATA_CURIDX_OFFSET],
696*4882a593Smuzhiyun 		reorder_info_buf[WLHOST_REORDERDATA_EXPIDX_OFFSET],
697*4882a593Smuzhiyun 		reorder_info_buf[WLHOST_REORDERDATA_MAXIDX_OFFSET]));
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	/* validate flags and flow id */
700*4882a593Smuzhiyun 	if (flags == 0xFF) {
701*4882a593Smuzhiyun 		DHD_ERROR(("%s: invalid flags...so ignore this packet\n", __FUNCTION__));
702*4882a593Smuzhiyun 		*pkt_count = 1;
703*4882a593Smuzhiyun 		return 0;
704*4882a593Smuzhiyun 	}
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	cur_pkt = *pkt;
707*4882a593Smuzhiyun 	*pkt = NULL;
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	ptr = dhd->reorder_bufs[flow_id];
710*4882a593Smuzhiyun 	if (flags & WLHOST_REORDERDATA_DEL_FLOW) {
711*4882a593Smuzhiyun 		uint32 buf_size = sizeof(struct reorder_info);
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 		DHD_REORDER(("%s: Flags indicating to delete a flow id %d\n",
714*4882a593Smuzhiyun 			__FUNCTION__, flow_id));
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 		if (ptr == NULL) {
717*4882a593Smuzhiyun 			DHD_REORDER(("%s: received flags to cleanup, but no flow (%d) yet\n",
718*4882a593Smuzhiyun 				__FUNCTION__, flow_id));
719*4882a593Smuzhiyun 			*pkt_count = 1;
720*4882a593Smuzhiyun 			*pkt = cur_pkt;
721*4882a593Smuzhiyun 			return 0;
722*4882a593Smuzhiyun 		}
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 		dhd_get_hostreorder_pkts(dhd->osh, ptr, pkt, &cnt, &plast,
725*4882a593Smuzhiyun 			ptr->exp_idx, ptr->exp_idx);
726*4882a593Smuzhiyun 		/* set it to the last packet */
727*4882a593Smuzhiyun 		if (plast) {
728*4882a593Smuzhiyun 			PKTSETNEXT(dhd->osh, plast, cur_pkt);
729*4882a593Smuzhiyun 			cnt++;
730*4882a593Smuzhiyun 		}
731*4882a593Smuzhiyun 		else {
732*4882a593Smuzhiyun 			if (cnt != 0) {
733*4882a593Smuzhiyun 				DHD_ERROR(("%s: del flow: something fishy, pending packets %d\n",
734*4882a593Smuzhiyun 					__FUNCTION__, cnt));
735*4882a593Smuzhiyun 			}
736*4882a593Smuzhiyun 			*pkt = cur_pkt;
737*4882a593Smuzhiyun 			cnt = 1;
738*4882a593Smuzhiyun 		}
739*4882a593Smuzhiyun 		buf_size += ((ptr->max_idx + 1) * sizeof(void *));
740*4882a593Smuzhiyun 		MFREE(dhd->osh, ptr, buf_size);
741*4882a593Smuzhiyun 		dhd->reorder_bufs[flow_id] = NULL;
742*4882a593Smuzhiyun 		*pkt_count = cnt;
743*4882a593Smuzhiyun 		return 0;
744*4882a593Smuzhiyun 	}
745*4882a593Smuzhiyun 	/* all the other cases depend on the existance of the reorder struct for that flow id */
746*4882a593Smuzhiyun 	if (ptr == NULL) {
747*4882a593Smuzhiyun 		uint32 buf_size_alloc = sizeof(reorder_info_t);
748*4882a593Smuzhiyun 		max_idx = reorder_info_buf[WLHOST_REORDERDATA_MAXIDX_OFFSET];
749*4882a593Smuzhiyun 
750*4882a593Smuzhiyun 		buf_size_alloc += ((max_idx + 1) * sizeof(void*));
751*4882a593Smuzhiyun 		/* allocate space to hold the buffers, index etc */
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 		DHD_REORDER(("%s: alloc buffer of size %d size, reorder info id %d, maxidx %d\n",
754*4882a593Smuzhiyun 			__FUNCTION__, buf_size_alloc, flow_id, max_idx));
755*4882a593Smuzhiyun 		ptr = (struct reorder_info *)MALLOC(dhd->osh, buf_size_alloc);
756*4882a593Smuzhiyun 		if (ptr == NULL) {
757*4882a593Smuzhiyun 			DHD_ERROR(("%s: Malloc failed to alloc buffer\n", __FUNCTION__));
758*4882a593Smuzhiyun 			*pkt_count = 1;
759*4882a593Smuzhiyun 			return 0;
760*4882a593Smuzhiyun 		}
761*4882a593Smuzhiyun 		bzero(ptr, buf_size_alloc);
762*4882a593Smuzhiyun 		dhd->reorder_bufs[flow_id] = ptr;
763*4882a593Smuzhiyun 		ptr->p = (void *)(ptr+1);
764*4882a593Smuzhiyun 		ptr->max_idx = max_idx;
765*4882a593Smuzhiyun 	}
766*4882a593Smuzhiyun 	if (flags & WLHOST_REORDERDATA_NEW_HOLE)  {
767*4882a593Smuzhiyun 		DHD_REORDER(("%s: new hole, so cleanup pending buffers\n", __FUNCTION__));
768*4882a593Smuzhiyun 		if (ptr->pend_pkts) {
769*4882a593Smuzhiyun 			dhd_get_hostreorder_pkts(dhd->osh, ptr, pkt, &cnt, &plast,
770*4882a593Smuzhiyun 				ptr->exp_idx, ptr->exp_idx);
771*4882a593Smuzhiyun 			ptr->pend_pkts = 0;
772*4882a593Smuzhiyun 		}
773*4882a593Smuzhiyun 		ptr->cur_idx = reorder_info_buf[WLHOST_REORDERDATA_CURIDX_OFFSET];
774*4882a593Smuzhiyun 		ptr->exp_idx = reorder_info_buf[WLHOST_REORDERDATA_EXPIDX_OFFSET];
775*4882a593Smuzhiyun 		ptr->max_idx = reorder_info_buf[WLHOST_REORDERDATA_MAXIDX_OFFSET];
776*4882a593Smuzhiyun 		ptr->p[ptr->cur_idx] = cur_pkt;
777*4882a593Smuzhiyun 		ptr->pend_pkts++;
778*4882a593Smuzhiyun 		*pkt_count = cnt;
779*4882a593Smuzhiyun 	}
780*4882a593Smuzhiyun 	else if (flags & WLHOST_REORDERDATA_CURIDX_VALID) {
781*4882a593Smuzhiyun 		cur_idx = reorder_info_buf[WLHOST_REORDERDATA_CURIDX_OFFSET];
782*4882a593Smuzhiyun 		exp_idx = reorder_info_buf[WLHOST_REORDERDATA_EXPIDX_OFFSET];
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 		if ((exp_idx == ptr->exp_idx) && (cur_idx != ptr->exp_idx)) {
785*4882a593Smuzhiyun 			/* still in the current hole */
786*4882a593Smuzhiyun 			/* enqueue the current on the buffer chain */
787*4882a593Smuzhiyun 			if (ptr->p[cur_idx] != NULL) {
788*4882a593Smuzhiyun 				DHD_REORDER(("%s: HOLE: ERROR buffer pending..free it\n",
789*4882a593Smuzhiyun 					__FUNCTION__));
790*4882a593Smuzhiyun 				PKTFREE(dhd->osh, ptr->p[cur_idx], TRUE);
791*4882a593Smuzhiyun 				ptr->p[cur_idx] = NULL;
792*4882a593Smuzhiyun 			}
793*4882a593Smuzhiyun 			ptr->p[cur_idx] = cur_pkt;
794*4882a593Smuzhiyun 			ptr->pend_pkts++;
795*4882a593Smuzhiyun 			ptr->cur_idx = cur_idx;
796*4882a593Smuzhiyun 			DHD_REORDER(("%s: fill up a hole..pending packets is %d\n",
797*4882a593Smuzhiyun 				__FUNCTION__, ptr->pend_pkts));
798*4882a593Smuzhiyun 			*pkt_count = 0;
799*4882a593Smuzhiyun 			*pkt = NULL;
800*4882a593Smuzhiyun 		}
801*4882a593Smuzhiyun 		else if (ptr->exp_idx == cur_idx) {
802*4882a593Smuzhiyun 			/* got the right one ..flush from cur to exp and update exp */
803*4882a593Smuzhiyun 			DHD_REORDER(("%s: got the right one now, cur_idx is %d\n",
804*4882a593Smuzhiyun 				__FUNCTION__, cur_idx));
805*4882a593Smuzhiyun 			if (ptr->p[cur_idx] != NULL) {
806*4882a593Smuzhiyun 				DHD_REORDER(("%s: Error buffer pending..free it\n",
807*4882a593Smuzhiyun 					__FUNCTION__));
808*4882a593Smuzhiyun 				PKTFREE(dhd->osh, ptr->p[cur_idx], TRUE);
809*4882a593Smuzhiyun 				ptr->p[cur_idx] = NULL;
810*4882a593Smuzhiyun 			}
811*4882a593Smuzhiyun 			ptr->p[cur_idx] = cur_pkt;
812*4882a593Smuzhiyun 			ptr->pend_pkts++;
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 			ptr->cur_idx = cur_idx;
815*4882a593Smuzhiyun 			ptr->exp_idx = exp_idx;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 			dhd_get_hostreorder_pkts(dhd->osh, ptr, pkt, &cnt, &plast,
818*4882a593Smuzhiyun 				cur_idx, exp_idx);
819*4882a593Smuzhiyun 			*pkt_count = cnt;
820*4882a593Smuzhiyun 			DHD_REORDER(("%s: freeing up buffers %d, still pending %d\n",
821*4882a593Smuzhiyun 				__FUNCTION__, cnt, ptr->pend_pkts));
822*4882a593Smuzhiyun 		}
823*4882a593Smuzhiyun 		else {
824*4882a593Smuzhiyun 			uint8 end_idx;
825*4882a593Smuzhiyun 			bool flush_current = FALSE;
826*4882a593Smuzhiyun 			/* both cur and exp are moved now .. */
827*4882a593Smuzhiyun 			DHD_REORDER(("%s:, flow %d, both moved, cur %d(%d), exp %d(%d)\n",
828*4882a593Smuzhiyun 				__FUNCTION__, flow_id, ptr->cur_idx, cur_idx,
829*4882a593Smuzhiyun 				ptr->exp_idx, exp_idx));
830*4882a593Smuzhiyun 			if (flags & WLHOST_REORDERDATA_FLUSH_ALL)
831*4882a593Smuzhiyun 				end_idx = ptr->exp_idx;
832*4882a593Smuzhiyun 			else
833*4882a593Smuzhiyun 				end_idx = exp_idx;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 			/* flush pkts first */
836*4882a593Smuzhiyun 			dhd_get_hostreorder_pkts(dhd->osh, ptr, pkt, &cnt, &plast,
837*4882a593Smuzhiyun 				ptr->exp_idx, end_idx);
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 			if (cur_idx == ptr->max_idx) {
840*4882a593Smuzhiyun 				if (exp_idx == 0)
841*4882a593Smuzhiyun 					flush_current = TRUE;
842*4882a593Smuzhiyun 			} else {
843*4882a593Smuzhiyun 				if (exp_idx == cur_idx + 1)
844*4882a593Smuzhiyun 					flush_current = TRUE;
845*4882a593Smuzhiyun 			}
846*4882a593Smuzhiyun 			if (flush_current) {
847*4882a593Smuzhiyun 				if (plast)
848*4882a593Smuzhiyun 					PKTSETNEXT(dhd->osh, plast, cur_pkt);
849*4882a593Smuzhiyun 				else
850*4882a593Smuzhiyun 					*pkt = cur_pkt;
851*4882a593Smuzhiyun 				cnt++;
852*4882a593Smuzhiyun 			}
853*4882a593Smuzhiyun 			else {
854*4882a593Smuzhiyun 				ptr->p[cur_idx] = cur_pkt;
855*4882a593Smuzhiyun 				ptr->pend_pkts++;
856*4882a593Smuzhiyun 			}
857*4882a593Smuzhiyun 			ptr->exp_idx = exp_idx;
858*4882a593Smuzhiyun 			ptr->cur_idx = cur_idx;
859*4882a593Smuzhiyun 			*pkt_count = cnt;
860*4882a593Smuzhiyun 		}
861*4882a593Smuzhiyun 	}
862*4882a593Smuzhiyun 	else {
863*4882a593Smuzhiyun 		uint8 end_idx;
864*4882a593Smuzhiyun 		/* no real packet but update to exp_seq...that means explicit window move */
865*4882a593Smuzhiyun 		exp_idx = reorder_info_buf[WLHOST_REORDERDATA_EXPIDX_OFFSET];
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 		DHD_REORDER(("%s: move the window, cur_idx is %d, exp is %d, new exp is %d\n",
868*4882a593Smuzhiyun 			__FUNCTION__, ptr->cur_idx, ptr->exp_idx, exp_idx));
869*4882a593Smuzhiyun 		if (flags & WLHOST_REORDERDATA_FLUSH_ALL)
870*4882a593Smuzhiyun 			end_idx =  ptr->exp_idx;
871*4882a593Smuzhiyun 		else
872*4882a593Smuzhiyun 			end_idx =  exp_idx;
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 		dhd_get_hostreorder_pkts(dhd->osh, ptr, pkt, &cnt, &plast, ptr->exp_idx, end_idx);
875*4882a593Smuzhiyun 		if (plast)
876*4882a593Smuzhiyun 			PKTSETNEXT(dhd->osh, plast, cur_pkt);
877*4882a593Smuzhiyun 		else
878*4882a593Smuzhiyun 			*pkt = cur_pkt;
879*4882a593Smuzhiyun 		cnt++;
880*4882a593Smuzhiyun 		*pkt_count = cnt;
881*4882a593Smuzhiyun 		/* set the new expected idx */
882*4882a593Smuzhiyun 		ptr->exp_idx = exp_idx;
883*4882a593Smuzhiyun 	}
884*4882a593Smuzhiyun 	return 0;
885*4882a593Smuzhiyun }
886