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