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