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