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