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