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