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