summaryrefslogtreecommitdiff
path: root/drivers/net/wireless/bcmdhd/dhd_msgbuf.c
blob: 76745f9d4a23fa44034d9518778a73fa4b7411db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
/*
 * Header file describing the internal (inter-module) DHD interfaces.
 *
 * Provides type definitions and function prototypes used to link the
 * DHD OS, bus, and protocol modules.
 *
 * Copyright (C) 1999-2016, Broadcom Corporation
 * 
 *      Unless you and Broadcom execute a separate written software license
 * agreement governing use of this software, this software is licensed to you
 * under the terms of the GNU General Public License version 2 (the "GPL"),
 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
 * following added to such license:
 * 
 *      As a special exception, the copyright holders of this software give you
 * permission to link this software with independent modules, and to copy and
 * distribute the resulting executable under terms of your choice, provided that
 * you also meet, for each linked independent module, the terms and conditions of
 * the license of that module.  An independent module is a module which is not
 * derived from this software.  The special exception does not apply to any
 * modifications of the software.
 * 
 *      Notwithstanding the above, under no circumstances may you combine this
 * software in any way with any other Broadcom software provided under a license
 * other than the GPL, without Broadcom's express prior written consent.
 *
 * $Id: dhd_msgbuf.c 452261 2014-01-29 19:30:23Z $
 */
#include <typedefs.h>
#include <osl.h>

#include <bcmutils.h>
#include <circularbuf.h>
#include <bcmmsgbuf.h>
#include <bcmendian.h>

#include <dngl_stats.h>
#include <dhd.h>
#include <dhd_proto.h>
#include <dhd_bus.h>
#include <dhd_dbg.h>


#ifdef PROP_TXSTATUS
#include <wlfc_proto.h>
#include <dhd_wlfc.h>
#endif
#include <pcie_core.h>
#include <bcmpcie.h>

#define RETRIES 2		/* # of retries to retrieve matching ioctl response */
#define IOCTL_HDR_LEN	12

#define DEFAULT_RX_BUFFERS_TO_POST	255
#define RXBUFPOST_THRESHOLD			16
#define RX_BUF_BURST				8

#define DHD_STOP_QUEUE_THRESHOLD	24
#define DHD_START_QUEUE_THRESHOLD	32
#define MAX_INLINE_IOCTL_LEN	64	/* anything beyond this len will not be inline reqst */

/* Required for Native to PktId mapping incase of 64bit hosts */
#define MAX_PKTID_ITEMS		(2048)

/* Given packet pointer and physical address, macro should return unique 32 bit pktid */
/* And given 32bit pktid, macro should return packet pointer and physical address */
extern void *pktid_map_init(void *osh, uint32 count);
extern void pktid_map_uninit(void *pktid_map_handle);
extern uint32 pktid_map_unique(void *pktid_map_handle,
	void *pkt, dmaaddr_t physaddr, uint32 physlen, uint32 dma);
extern void *pktid_get_packet(void *pktid_map_handle,
	uint32 id, dmaaddr_t *physaddr, uint32 *physlen);

#define NATIVE_TO_PKTID_INIT(osh, count)	pktid_map_init(osh, count)
#define NATIVE_TO_PKTID_UNINIT(pktid_map_handle)	pktid_map_uninit(pktid_map_handle)

#define NATIVE_TO_PKTID(pktid_map_handle, pkt, pa, pa_len, dma)	\
	pktid_map_unique((pktid_map_handle), (void *)(pkt), (pa), (uint32) (pa_len), (uint32)dma)
#define PKTID_TO_NATIVE(pktid_map_handle, id, pa, pa_len)		\
	pktid_get_packet((pktid_map_handle), (uint32)(id), (void *)&(pa), (uint32 *) &(pa_len))

#define MODX(x, n)	((x) & ((n) -1))
#define align(x, n)	(MODX(x, n) ? ((x) - MODX(x, n) + (n)) : ((x) - MODX(x, n)))
#define RX_DMA_OFFSET	8
#define IOCT_RETBUF_SIZE	(RX_DMA_OFFSET + WLC_IOCTL_MAXLEN)

typedef struct dhd_prot {
	uint32 reqid;
	uint16 hdr_len;
	uint32 lastcmd;
	uint32 pending;
	uint16 rxbufpost;
	uint16 max_rxbufpost;
	uint16 active_tx_count;
	uint16 max_tx_count;
	dmaaddr_t htod_physaddr;
	dmaaddr_t dtoh_physaddr;
	bool txflow_en;
	circularbuf_t *dtohbuf;
	circularbuf_t *htodbuf;
	uint32	rx_dataoffset;
	void*	retbuf;
	dmaaddr_t retbuf_phys;
	void*	ioctbuf;	/* For holding ioct request buf */
	dmaaddr_t ioctbuf_phys;	/* physical address for ioctbuf */
	dhd_mb_ring_t mb_ring_fn;
	void *htod_ring;
	void *dtoh_ring;
	/* Flag to check if splitbuf support is enabled. */
	/* Set to False at dhd_prot_attach. Set to True at dhd_prot_init */
	bool htodsplit;
	bool dtohsplit;
	/* H2D/D2H Ctrl rings */
	dmaaddr_t htod_ctrl_physaddr;	/* DMA mapped physical addr ofr H2D ctrl ring */
	dmaaddr_t dtoh_ctrl_physaddr;	/* DMA mapped phys addr for D2H ctrl ring */
	circularbuf_t *htod_ctrlbuf;	/* Cbuf handle for H2D ctrl ring */
	circularbuf_t *dtoh_ctrlbuf;	/* Cbuf handle for D2H ctrl ring */
	void *htod_ctrl_ring; /* address for H2D control buf */
	void *dtoh_ctrl_ring; /* address for D2H control buf */


	uint16	ioctl_seq_no;
	uint16	data_seq_no;
	void *pktid_map_handle;
} dhd_prot_t;

static int dhdmsgbuf_query_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd,
	void *buf, uint len, uint8 action);
static int dhd_msgbuf_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd,
	void *buf, uint len, uint8 action);
static int dhdmsgbuf_cmplt(dhd_pub_t *dhd, uint32 id, uint32 len, void* buf, void* retbuf);
static int dhd_msgbuf_init_dtoh(dhd_pub_t *dhd);

static int dhd_msgbuf_rxbuf_post(dhd_pub_t *dhd);
static int dhd_msgbuf_init_htod(dhd_pub_t *dhd);
static int dhd_msgbuf_init_htod_ctrl(dhd_pub_t *dhd);
static int dhd_msgbuf_init_dtoh_ctrl(dhd_pub_t *dhd);
static int dhd_prot_rxbufpost(dhd_pub_t *dhd, uint32 count);
static void dhd_prot_return_rxbuf(dhd_pub_t *dhd, uint16 rxcnt);
static void dhd_prot_rxcmplt_process(dhd_pub_t *dhd, void* buf);
static void dhd_prot_event_process(dhd_pub_t *dhd, uint8* buf, uint16 len);
static void dhd_prot_process_msgtype(dhd_pub_t *dhd, uint8* buf, uint16 len);
static void dhd_process_msgtype(dhd_pub_t *dhd, uint8* buf, uint16 len);

static void dhd_prot_txstatus_process(dhd_pub_t *dhd, void * buf);
static void dhd_prot_ioctcmplt_process(dhd_pub_t *dhd, void * buf);
void* dhd_alloc_circularbuf_space(dhd_pub_t *dhd, circularbuf_t *handle, uint16 msglen, uint path);
static int dhd_fillup_ioct_reqst(dhd_pub_t *dhd, uint16 len, uint cmd, void* buf, int ifidx);
static int dhd_fillup_ioct_reqst_ptrbased(dhd_pub_t *dhd, uint16 len, uint cmd, void* buf,
	int ifidx);
static INLINE void dhd_prot_packet_free(dhd_pub_t *dhd, uint32 pktid);
static INLINE void *dhd_prot_packet_get(dhd_pub_t *dhd, uint32 pktid);

/* Linkage, sets prot link and updates hdrlen in pub */
int dhd_prot_attach(dhd_pub_t *dhd)
{
	uint alloced = 0;

	dhd_prot_t *msg_buf;
	if (!(msg_buf = (dhd_prot_t *)DHD_OS_PREALLOC(dhd, DHD_PREALLOC_PROT,
		sizeof(dhd_prot_t)))) {
			DHD_ERROR(("%s: kmalloc failed\n", __FUNCTION__));
			goto fail;
		}
	memset(msg_buf, 0, sizeof(dhd_prot_t));

	msg_buf->hdr_len = sizeof(ioctl_req_hdr_t) + sizeof(cmn_msg_hdr_t) + sizeof(ret_buf_t);
	msg_buf->dtohbuf = MALLOC(dhd->osh, sizeof(circularbuf_t));
	msg_buf->htodbuf = MALLOC(dhd->osh, sizeof(circularbuf_t));

	memset(msg_buf->dtohbuf, 0, sizeof(circularbuf_t));
	memset(msg_buf->htodbuf, 0, sizeof(circularbuf_t));

	dhd->prot = msg_buf;
	dhd->maxctl = WLC_IOCTL_MAXLEN + msg_buf->hdr_len;

	/* ret buf for ioctl */
	msg_buf->retbuf = DMA_ALLOC_CONSISTENT(dhd->osh, IOCT_RETBUF_SIZE, 4,
		&alloced, &msg_buf->retbuf_phys, NULL);
	if (msg_buf->retbuf ==  NULL) {
		ASSERT(0);
		return BCME_NOMEM;
	}

	ASSERT(MODX((unsigned long)msg_buf->retbuf, 4) == 0);

	msg_buf->ioctbuf = DMA_ALLOC_CONSISTENT(dhd->osh, MSGBUF_MAX_MSG_SIZE, 4,
		&alloced, &msg_buf->ioctbuf_phys, NULL);

	if (msg_buf->ioctbuf ==  NULL) {
		ASSERT(0);
		return BCME_NOMEM;
	}

	ASSERT(MODX((unsigned long)msg_buf->ioctbuf, 4) == 0);

	msg_buf->pktid_map_handle = NATIVE_TO_PKTID_INIT(dhd->osh, MAX_PKTID_ITEMS);
	if (msg_buf->pktid_map_handle == NULL) {
		ASSERT(0);
		return BCME_NOMEM;
	}

	msg_buf->htod_ring = DMA_ALLOC_CONSISTENT(dhd->osh, HOST_TO_DNGL_MSGBUF_SZ, 4,
		&alloced, &msg_buf->htod_physaddr, NULL);
	if (msg_buf->htod_ring ==  NULL) {
		ASSERT(0);
		return BCME_NOMEM;
	}

	ASSERT(MODX((unsigned long)msg_buf->htod_ring, 4) == 0);

	msg_buf->dtoh_ring = DMA_ALLOC_CONSISTENT(dhd->osh, DNGL_TO_HOST_MSGBUF_SZ, 4,
		&alloced, &msg_buf->dtoh_physaddr, NULL);
	if (msg_buf->dtoh_ring ==  NULL) {
		ASSERT(0);
		return BCME_NOMEM;
	}

	ASSERT(MODX((unsigned long)msg_buf->dtoh_ring, 4) == 0);

	/* At this point we assume splitbuf is not supported by dongle */
	msg_buf->htodsplit = FALSE;
	msg_buf->dtohsplit = FALSE;


	return 0;

fail:
#ifndef CONFIG_DHD_USE_STATIC_BUF
	if (msg_buf != NULL)
		MFREE(dhd->osh, msg_buf, sizeof(dhd_prot_t));
#endif /* CONFIG_DHD_USE_STATIC_BUF */
	return BCME_NOMEM;
}

/* Unlink, frees allocated protocol memory (including dhd_prot) */
void dhd_prot_detach(dhd_pub_t *dhd)
{
	 /* Stop the protocol module */
	if (dhd->prot) {

		if (dhd->prot->dtoh_ring) {
			DMA_FREE_CONSISTENT(dhd->osh, dhd->prot->dtoh_ring,
				DNGL_TO_HOST_MSGBUF_SZ, dhd->prot->dtoh_physaddr, NULL);

			dhd->prot->dtoh_ring = NULL;
			PHYSADDRHISET(dhd->prot->dtoh_physaddr, 0);
			PHYSADDRLOSET(dhd->prot->dtoh_physaddr, 0);
		}

		if (dhd->prot->htod_ring) {
			DMA_FREE_CONSISTENT(dhd->osh, dhd->prot->htod_ring,
				HOST_TO_DNGL_MSGBUF_SZ, dhd->prot->htod_physaddr, NULL);

			dhd->prot->htod_ring =  NULL;
			PHYSADDRHISET(dhd->prot->htod_physaddr, 0);
			PHYSADDRLOSET(dhd->prot->htod_physaddr, 0);
		}

		if (dhd->prot->dtohbuf) {
			MFREE(dhd->osh, dhd->prot->dtohbuf, sizeof(circularbuf_t));
			dhd->prot->dtohbuf = NULL;
		}

		if (dhd->prot->htodbuf) {
			MFREE(dhd->osh, dhd->prot->htodbuf, sizeof(circularbuf_t));
			dhd->prot->htodbuf = NULL;
		}

		if (dhd->prot->htod_ctrl_ring) {
			DMA_FREE_CONSISTENT(dhd->osh, dhd->prot->htod_ctrl_ring,
				HOST_TO_DNGL_CTRLRING_SZ, dhd->prot->htod_ctrl_physaddr, NULL);

			dhd->prot->htod_ctrl_ring = NULL;
			dhd->prot->htod_ctrl_physaddr = 0;
		}

		if (dhd->prot->dtoh_ctrl_ring) {
			DMA_FREE_CONSISTENT(dhd->osh, dhd->prot->dtoh_ctrl_ring,
				DNGL_TO_HOST_CTRLRING_SZ, dhd->prot->dtoh_ctrl_physaddr, NULL);

			dhd->prot->dtoh_ctrl_ring = NULL;
			dhd->prot->dtoh_ctrl_physaddr = 0;
		}

		if (dhd->prot->htod_ctrlbuf) {
			MFREE(dhd->osh, dhd->prot->htod_ctrlbuf, sizeof(circularbuf_t));
			dhd->prot->htod_ctrlbuf = NULL;
		}

		if (dhd->prot->dtoh_ctrlbuf) {
			MFREE(dhd->osh, dhd->prot->dtoh_ctrlbuf, sizeof(circularbuf_t));
			dhd->prot->dtoh_ctrlbuf = NULL;
		}

		if (dhd->prot->retbuf) {
			DMA_FREE_CONSISTENT(dhd->osh, dhd->prot->retbuf,
			IOCT_RETBUF_SIZE, dhd->prot->retbuf_phys, NULL);
			dhd->prot->retbuf = NULL;
		}

		if (dhd->prot->ioctbuf) {
			DMA_FREE_CONSISTENT(dhd->osh, dhd->prot->ioctbuf,
			MSGBUF_MAX_MSG_SIZE, dhd->prot->ioctbuf_phys, NULL);

			dhd->prot->ioctbuf = NULL;
		}

		NATIVE_TO_PKTID_UNINIT(dhd->prot->pktid_map_handle);

#ifndef CONFIG_DHD_USE_STATIC_BUF
		MFREE(dhd->osh, dhd->prot, sizeof(dhd_prot_t));
#endif /* CONFIG_DHD_USE_STATIC_BUF */

		dhd->prot = NULL;
	}
}

void
dhd_prot_rx_dataoffset(dhd_pub_t *dhd, uint32 rx_offset)
{
	dhd_prot_t *prot = dhd->prot;
	prot->rx_dataoffset = rx_offset;
}


/* Initialize protocol: sync w/dongle state.
 * Sets dongle media info (iswl, drv_version, mac address).
 */
int dhd_prot_init(dhd_pub_t *dhd)
{
	int ret = 0;
	wlc_rev_info_t revinfo;
	dhd_prot_t *prot = dhd->prot;
	uint32 shared_flags;
	DHD_TRACE(("%s: Enter\n", __FUNCTION__));

	dhd_bus_cmn_readshared(dhd->bus, &prot->max_tx_count, TOTAL_LFRAG_PACKET_CNT);
	if (prot->max_tx_count == 0) {
		/* This can happen if LFrag pool is not enabled for the LFRAG's */
		/* on the dongle. Let's use some default value */
		prot->max_tx_count = 64;
	}
	DHD_INFO(("%s:%d: MAX_TX_COUNT = %d\n", __FUNCTION__, __LINE__, prot->max_tx_count));

	dhd_bus_cmn_readshared(dhd->bus, &prot->max_rxbufpost, MAX_HOST_RXBUFS);
	if (prot->max_rxbufpost == 0) {
		/* This would happen if the dongle firmware is not */
		/* using the latest shared structure template */
		prot->max_rxbufpost = DEFAULT_RX_BUFFERS_TO_POST;
	}
	DHD_INFO(("%s:%d: MAX_RXBUFPOST = %d\n", __FUNCTION__, __LINE__, prot->max_rxbufpost));

	prot->active_tx_count = 0;
	prot->txflow_en = FALSE;
	prot->mb_ring_fn = dhd_bus_get_mbintr_fn(dhd->bus);
	prot->data_seq_no = 0;
	prot->ioctl_seq_no = 0;
	/* initialise msgbufs */
	shared_flags = dhd_bus_get_sharedflags(dhd->bus);
	if (shared_flags & PCIE_SHARED_HTOD_SPLIT) {
		prot->htodsplit = TRUE;
		if (dhd_msgbuf_init_htod_ctrl(dhd) == BCME_NOMEM)
		{
			prot->htodsplit = FALSE;
			DHD_ERROR(("%s:%d: HTOD ctrl ring alloc failed!\n",
				__FUNCTION__, __LINE__));
		}
	}
	if (shared_flags & PCIE_SHARED_DTOH_SPLIT) {
		prot->dtohsplit = TRUE;
		if (dhd_msgbuf_init_dtoh_ctrl(dhd) == BCME_NOMEM)
		{
			prot->dtohsplit = FALSE;
			DHD_ERROR(("%s:%d: DTOH ctrl ring alloc failed!\n",
				__FUNCTION__, __LINE__));
		}
	}
	ret = dhd_msgbuf_init_htod(dhd);
	ret = dhd_msgbuf_init_dtoh(dhd);
	ret = dhd_msgbuf_rxbuf_post(dhd);


	/* Get the device rev info */
	memset(&revinfo, 0, sizeof(revinfo));
	ret = dhd_wl_ioctl_cmd(dhd, WLC_GET_REVINFO, &revinfo, sizeof(revinfo), FALSE, 0);
	if (ret < 0)
		goto done;
#if defined(WL_CFG80211)
	if (dhd_download_fw_on_driverload)
#endif /* defined(WL_CFG80211) */
		ret = dhd_preinit_ioctls(dhd);
	/* Always assumes wl for now */
	dhd->iswl = TRUE;
done:
	return ret;

}

static INLINE void BCMFASTPATH
dhd_prot_packet_free(dhd_pub_t *dhd, uint32 pktid)
{
	void *PKTBUF;
	dmaaddr_t pa;
	uint32 pa_len;
	PKTBUF = PKTID_TO_NATIVE(dhd->prot->pktid_map_handle, pktid, pa, pa_len);
	DMA_UNMAP(dhd->osh, (uint) pa, (uint) pa_len, DMA_TX, 0, 0);
	PKTFREE(dhd->osh, PKTBUF, TRUE);
	return;
}

static INLINE void * BCMFASTPATH
dhd_prot_packet_get(dhd_pub_t *dhd, uint32 pktid)
{
	void *PKTBUF;
	ulong pa;
	uint32 pa_len;
	PKTBUF = PKTID_TO_NATIVE(dhd->prot->pktid_map_handle, pktid, pa, pa_len);
	DMA_UNMAP(dhd->osh, (uint) pa, (uint) pa_len, DMA_RX, 0, 0);
	return PKTBUF;
}

static int BCMFASTPATH
dhd_msgbuf_rxbuf_post(dhd_pub_t *dhd)
{
	dhd_prot_t *prot = dhd->prot;
	unsigned long flags;
	uint32 fillbufs;
	uint32 i;
	fillbufs = prot->max_rxbufpost - prot->rxbufpost;

	for (i = 0; i < fillbufs; ) {
		int retcount;
		uint32 buf_count = (fillbufs - i) > RX_BUF_BURST ? RX_BUF_BURST : (fillbufs - i);

		flags = dhd_os_spin_lock(dhd);
		retcount = dhd_prot_rxbufpost(dhd, buf_count);
		if (retcount > 0) {
			prot->rxbufpost += (uint16)retcount;
			i += (uint16)retcount;
			dhd_os_spin_unlock(dhd, flags);
		} else {
			dhd_os_spin_unlock(dhd, flags);
			break;
		}
	}

	return 0;
}

static int BCMFASTPATH
dhd_prot_rxbufpost(dhd_pub_t *dhd, uint32 count)
{
	void *p;
	uint16 pktsz = 2048;
	uint32 i;
	rxdesc_msghdr_t *rxbuf_post;
	rx_lenptr_tup_t *rx_tup;
	dmaaddr_t physaddr;
	uint32 pktlen;
	uint32 msglen = sizeof(rxdesc_msghdr_t) + count * sizeof(rx_lenptr_tup_t);

	dhd_prot_t *prot = dhd->prot;
	circularbuf_t *htod_msgbuf = (circularbuf_t *)prot->htodbuf;

	rxbuf_post = (rxdesc_msghdr_t *)dhd_alloc_circularbuf_space(dhd,
		htod_msgbuf, (uint16)msglen, HOST_TO_DNGL_DATA);
	if (rxbuf_post == NULL) {
		DHD_INFO(("%s:%d: HTOD Msgbuf Not available\n",
			__FUNCTION__, __LINE__));
		return -1;
	}

	/* CMN msg header */
	rxbuf_post->msg.msglen = htol16((uint16)msglen);
	rxbuf_post->msg.msgtype = MSG_TYPE_RXBUF_POST;
	rxbuf_post->msg.ifidx = 0;
	rxbuf_post->msg.u.seq.seq_no = htol16(++prot->data_seq_no);

	/* RX specific hdr */
	rxbuf_post->rsvd0 = 0;
	rxbuf_post->rsvd1 = 0;
	rxbuf_post->descnt = (uint8)count;

	rx_tup = (rx_lenptr_tup_t *) &(rxbuf_post->rx_tup[0]);

	for (i = 0; i < count; i++) {
		if ((p = PKTGET(dhd->osh, pktsz, FALSE)) == NULL) {
			DHD_ERROR(("%s:%d: PKTGET for rxbuf failed\n", __FUNCTION__, __LINE__));
			printf("%s:%d: PKTGET for rxbuf failed. Need to handle this gracefully\n",
				__FUNCTION__, __LINE__);
			return -1;
		}

		pktlen = PKTLEN(dhd->osh, p);
		physaddr = DMA_MAP(dhd->osh, PKTDATA(dhd->osh, p), pktlen, DMA_RX, 0, 0);
		if (physaddr == 0) {
			DHD_ERROR(("Something really bad, unless 0 is a valid phyaddr\n"));
			ASSERT(0);
		}
		/* Each bufid-len-ptr tuple */
		rx_tup->rxbufid = htol32(NATIVE_TO_PKTID(dhd->prot->pktid_map_handle,
			p, physaddr, pktlen, DMA_RX));
		rx_tup->len = htol16((uint16)PKTLEN(dhd->osh, p));
		rx_tup->rsvd2 = 0;
		rx_tup->ret_buf.high_addr = htol32(PHYSADDRHI(physaddr));
		rx_tup->ret_buf.low_addr  = htol32(PHYSADDRLO(physaddr));

		rx_tup++;
	}

	/* Since, we are filling the data directly into the bufptr obtained
	 * from the msgbuf, we can directly call the write_complete
	 */
	circularbuf_write_complete(htod_msgbuf, (uint16)msglen);

	return count;
}

void BCMFASTPATH
dhd_msgbuf_ringbell(void *ctx)
{
	dhd_pub_t *dhd = (dhd_pub_t *) ctx;
	dhd_prot_t *prot = dhd->prot;
	circularbuf_t *htod_msgbuf = (circularbuf_t *)prot->htodbuf;

	/* Following will take care of writing both the Write and End pointers (32 bits) */
	dhd_bus_cmn_writeshared(dhd->bus, &(CIRCULARBUF_WRITE_PTR(htod_msgbuf)),
		sizeof(uint32), HOST_TO_DNGL_WPTR);

	prot->mb_ring_fn(dhd->bus, *(uint32 *) &(CIRCULARBUF_WRITE_PTR(htod_msgbuf)));
}

void BCMFASTPATH
dhd_ctrlbuf_ringbell(void *ctx)
{
	dhd_pub_t *dhd = (dhd_pub_t *) ctx;
	dhd_prot_t *prot = dhd->prot;
	circularbuf_t *htod_ctrlbuf = (circularbuf_t *)prot->htod_ctrlbuf;

	/* Following will take care of writing both the Write and End pointers (32 bits) */
	dhd_bus_cmn_writeshared(dhd->bus, &(CIRCULARBUF_WRITE_PTR(htod_ctrlbuf)),
		sizeof(uint32), HTOD_CTRL_WPTR);

	prot->mb_ring_fn(dhd->bus, *(uint32 *) &(CIRCULARBUF_WRITE_PTR(htod_ctrlbuf)));
}

static int
dhd_msgbuf_init_htod(dhd_pub_t *dhd)
{
	dhd_prot_t *prot = dhd->prot;
	circularbuf_t *htod_msgbuf = (circularbuf_t *)prot->htodbuf;

	circularbuf_init(htod_msgbuf, prot->htod_ring, HOST_TO_DNGL_MSGBUF_SZ);
	circularbuf_register_cb(htod_msgbuf, dhd_msgbuf_ringbell, (void *)dhd);
	dhd_bus_cmn_writeshared(dhd->bus, &prot->htod_physaddr,
		sizeof(prot->htod_physaddr), HOST_TO_DNGL_BUF_ADDR);

	dhd_bus_cmn_writeshared(dhd->bus, &(CIRCULARBUF_WRITE_PTR(htod_msgbuf)),
		sizeof(uint32), HOST_TO_DNGL_WPTR);

	return 0;

}
static int
dhd_msgbuf_init_dtoh(dhd_pub_t *dhd)
{
	dhd_prot_t *prot = dhd->prot;
	circularbuf_t *dtoh_msgbuf = (circularbuf_t *)prot->dtohbuf;

	prot->rxbufpost = 0;
	circularbuf_init(dtoh_msgbuf, prot->dtoh_ring, DNGL_TO_HOST_MSGBUF_SZ);
	dhd_bus_cmn_writeshared(dhd->bus, &prot->dtoh_physaddr,
		sizeof(prot->dtoh_physaddr), DNGL_TO_HOST_BUF_ADDR);

	dhd_bus_cmn_writeshared(dhd->bus, &CIRCULARBUF_READ_PTR(dtoh_msgbuf),
		sizeof(uint16), DNGL_TO_HOST_RPTR);

	/* One dummy interrupt to the device. This would trigger */
	/* the msgbuf initializations at the device side.        */
	/* Send dummy intr to device here, only if support for split data/ctrl rings is disabled */
	/* Else send the dummy initialization intr at dtoh ctrl buf init */

	dhd_bus_ringbell(dhd->bus, PCIE_INTB);
	return 0;
}

/* Allocate space for HTOD ctrl ring on host and initialize handle/doorbell for the same */
static int dhd_msgbuf_init_htod_ctrl(dhd_pub_t *dhd)
{
	uint alloced;
	dhd_prot_t *prot = dhd->prot;
	prot->htod_ctrlbuf = MALLOC(dhd->osh, sizeof(circularbuf_t));
	memset(prot->htod_ctrlbuf, 0, sizeof(circularbuf_t));

	prot->htod_ctrl_ring = DMA_ALLOC_CONSISTENT(dhd->osh, HOST_TO_DNGL_CTRLRING_SZ, 4,
		&alloced, &prot->htod_ctrl_physaddr, NULL);
	if (prot->htod_ctrl_ring ==  NULL) {
		return BCME_NOMEM;
	}

	ASSERT(MODX((unsigned long)prot->htod_ctrl_ring, 4) == 0);

	circularbuf_init(prot->htod_ctrlbuf, prot->htod_ctrl_ring, HOST_TO_DNGL_CTRLRING_SZ);
	circularbuf_register_cb(prot->htod_ctrlbuf, dhd_ctrlbuf_ringbell, (void *)dhd);
	dhd_bus_cmn_writeshared(dhd->bus, &prot->htod_ctrl_physaddr,
		sizeof(prot->htod_ctrl_physaddr), HOST_TO_DNGL_CTRLBUF_ADDR);

	dhd_bus_cmn_writeshared(dhd->bus, &(CIRCULARBUF_WRITE_PTR(prot->htod_ctrlbuf)),
		sizeof(uint32), HTOD_CTRL_WPTR);

	return 0;
}
/* Allocate space for DTOH ctrl ring on host and initialize msgbuf handle in dhd_prot_t */
static int dhd_msgbuf_init_dtoh_ctrl(dhd_pub_t *dhd)
{
	uint alloced;
	dhd_prot_t *prot = dhd->prot;
	prot->dtoh_ctrlbuf = MALLOC(dhd->osh, sizeof(circularbuf_t));
	memset(prot->dtoh_ctrlbuf, 0, sizeof(circularbuf_t));

	prot->dtoh_ctrl_ring = DMA_ALLOC_CONSISTENT(dhd->osh, DNGL_TO_HOST_CTRLRING_SZ, 4,
		&alloced, &prot->dtoh_ctrl_physaddr, NULL);
	if (prot->dtoh_ctrl_ring ==  NULL) {
		return BCME_NOMEM;
	}
	ASSERT(MODX((unsigned long)prot->dtoh_ctrl_ring, 4) == 0);

	circularbuf_init(prot->dtoh_ctrlbuf, prot->dtoh_ctrl_ring, DNGL_TO_HOST_CTRLRING_SZ);
	dhd_bus_cmn_writeshared(dhd->bus, &prot->dtoh_ctrl_physaddr,
		sizeof(prot->dtoh_ctrl_physaddr), DNGL_TO_HOST_CTRLBUF_ADDR);

	dhd_bus_cmn_writeshared(dhd->bus, &(CIRCULARBUF_READ_PTR(prot->dtoh_ctrlbuf)),
		sizeof(uint32), DTOH_CTRL_RPTR);
	return 0;
}

int BCMFASTPATH
dhd_prot_process_msgbuf(dhd_pub_t *dhd)
{
	dhd_prot_t *prot = dhd->prot;
	circularbuf_t *dtoh_msgbuf = (circularbuf_t *)prot->dtohbuf;

	dhd_bus_cmn_readshared(dhd->bus, &CIRCULARBUF_WRITE_PTR(dtoh_msgbuf), DNGL_TO_HOST_WPTR);

	/* Process all the messages - DTOH direction */
	while (TRUE) {
		uint8 *src_addr;
		uint16 src_len;

		src_addr = circularbuf_get_read_ptr(dtoh_msgbuf, &src_len);
		if (src_addr == NULL)
			break;

		/* Prefetch data to populate the cache */
		OSL_PREFETCH(src_addr);

		dhd_prot_process_msgtype(dhd, src_addr, src_len);
		circularbuf_read_complete(dtoh_msgbuf, src_len);

		/* Write to dngl rd ptr */
		dhd_bus_cmn_writeshared(dhd->bus, &CIRCULARBUF_READ_PTR(dtoh_msgbuf),
			sizeof(uint16), DNGL_TO_HOST_RPTR);
	}

	return 0;
}

int BCMFASTPATH
dhd_prot_process_ctrlbuf(dhd_pub_t * dhd)
{
	dhd_prot_t *prot = dhd->prot;
	circularbuf_t *dtoh_ctrlbuf = (circularbuf_t *)prot->dtoh_ctrlbuf;

	dhd_bus_cmn_readshared(dhd->bus, &CIRCULARBUF_WRITE_PTR(dtoh_ctrlbuf), DTOH_CTRL_WPTR);

	/* Process all the messages - DTOH direction */
	while (TRUE) {
		uint8 *src_addr;
		uint16 src_len;

		src_addr = circularbuf_get_read_ptr(dtoh_ctrlbuf, &src_len);
		if (src_addr == NULL) {
			break;
		}
		/* Prefetch data to populate the cache */
		OSL_PREFETCH(src_addr);

		dhd_prot_process_msgtype(dhd, src_addr, src_len);
		circularbuf_read_complete(dtoh_ctrlbuf, src_len);

		/* Write to dngl rd ptr */
		dhd_bus_cmn_writeshared(dhd->bus, &CIRCULARBUF_READ_PTR(dtoh_ctrlbuf),
			sizeof(uint16), DTOH_CTRL_RPTR);
	}

	return 0;
}

static void BCMFASTPATH
dhd_prot_process_msgtype(dhd_pub_t *dhd, uint8* buf, uint16 len)
{
	dhd_prot_t *prot = dhd->prot;
	uint32 cur_dma_len = 0;

	DHD_TRACE(("%s: process msgbuf of len %d\n", __FUNCTION__, len));

	while (len > 0) {
		ASSERT(len > (sizeof(cmn_msg_hdr_t) + prot->rx_dataoffset));
		if (prot->rx_dataoffset) {
			cur_dma_len = *(uint32 *) buf;
			ASSERT(cur_dma_len <= len);
			buf += prot->rx_dataoffset;
			len -= (uint16)prot->rx_dataoffset;
		}
		else {
			cur_dma_len = len;
		}
		dhd_process_msgtype(dhd, buf, (uint16)cur_dma_len);
		len -= (uint16)cur_dma_len;
		buf += cur_dma_len;
	}
}


static void
dhd_check_sequence_num(cmn_msg_hdr_t *msg)
{
	static uint32 ioctl_seq_no_old = 0;
	static uint32 data_seq_no_old = 0;

	switch (msg->msgtype) {
		case MSG_TYPE_IOCTL_CMPLT:
			if (msg->u.seq.seq_no && msg->u.seq.seq_no != (ioctl_seq_no_old + 1))
			{
				DHD_ERROR(("Error in IOCTL MsgBuf Sequence number!!"
				"new seq no %u, old seq number %u\n",
				msg->u.seq.seq_no, ioctl_seq_no_old));
			}
			ioctl_seq_no_old  = msg->u.seq.seq_no;
			break;

		case MSG_TYPE_RX_CMPLT:
		case MSG_TYPE_WL_EVENT :
		case MSG_TYPE_TX_STATUS :
		case MSG_TYPE_LOOPBACK:
			if (msg->u.seq.seq_no && msg->u.seq.seq_no != (data_seq_no_old + 1))
			{
				DHD_ERROR(("Error in DATA MsgBuf Sequence number!!"
					"new seq no %u	 old seq number %u\n",
					msg->u.seq.seq_no, data_seq_no_old));
			}
			data_seq_no_old = msg->u.seq.seq_no;
			break;

		default:
			printf("Unknown MSGTYPE in %s \n", __FUNCTION__);
			break;

	}
}

static void BCMFASTPATH
dhd_process_msgtype(dhd_pub_t *dhd, uint8* buf, uint16 len)
{
	uint16 pktlen = len;
	uint16 msglen;
	uint8 msgtype;
	cmn_msg_hdr_t *msg = NULL;
	while (pktlen > 0) {
		msg = (cmn_msg_hdr_t *)buf;
		msgtype = msg->msgtype;
		msglen = msg->msglen;

		/* Prefetch data to populate the cache */
		OSL_PREFETCH(buf+msglen);

		dhd_check_sequence_num(msg);

		DHD_INFO(("msgtype %d, msglen is %d \n", msgtype, msglen));
		switch (msgtype) {
			case MSG_TYPE_IOCTL_CMPLT:
				DHD_INFO((" MSG_TYPE_IOCTL_CMPLT\n"));
				dhd_prot_ioctcmplt_process(dhd, buf);
				break;
			case MSG_TYPE_RX_CMPLT:
				DHD_INFO((" MSG_TYPE_RX_CMPLT\n"));
				dhd_prot_rxcmplt_process(dhd, buf);
				break;
			case MSG_TYPE_WL_EVENT:
				DHD_INFO((" MSG_TYPE_WL_EVENT\n"));
				dhd_prot_event_process(dhd, buf, msglen);
				break;
			case MSG_TYPE_TX_STATUS:
				DHD_INFO((" MSG_TYPE_TX_STATUS\n"));
				dhd_prot_txstatus_process(dhd, buf);
				break;
			case MSG_TYPE_LOOPBACK:
				bcm_print_bytes("LPBK RESP: ", (uint8 *)msg, msglen);
				DHD_ERROR((" MSG_TYPE_LOOPBACK, len %d\n", msglen));
				break;
			default :
				DHD_ERROR(("Unknown state in %s,"
				"rxoffset %d\n", __FUNCTION__, dhd->prot->rx_dataoffset));
				bcm_print_bytes("UNKNOWN msg", (uchar *)msg, msglen);
				break;

		}

		DHD_INFO(("pktlen is %d, msglen is %d\n", pktlen, msglen));
		if (pktlen < msglen)  {
			return;
		}
		pktlen = pktlen - msglen;
		buf = buf + msglen;
	}
}
static void
dhd_prot_ioctcmplt_process(dhd_pub_t *dhd, void * buf)
{
	uint32 retlen, status, inline_data = 0;
	uint32 pkt_id, xt_id;

	ioct_resp_hdr_t * ioct_resp = (ioct_resp_hdr_t *)buf;
	retlen = ltoh32(ioct_resp->ret_len);
	pkt_id = ltoh32(ioct_resp->pkt_id);
	xt_id = ltoh32(ioct_resp->xt_id);
	status = ioct_resp->status;
	if (retlen <= 4) {
		inline_data = ltoh32(ioct_resp->inline_data);
	} else {
		OSL_CACHE_INV((void *) dhd->prot->retbuf, retlen);
	}
	DHD_CTL(("status from the pkt_id is %d, ioctl is %d, ret_len is %d, xt_id %d\n",
		pkt_id, status, retlen, xt_id));

	if (retlen == 0)
		retlen = 1;

	dhd_bus_update_retlen(dhd->bus, retlen, pkt_id, status, inline_data);
	dhd_os_ioctl_resp_wake(dhd);
}

static void BCMFASTPATH
dhd_prot_txstatus_process(dhd_pub_t *dhd, void * buf)
{
	dhd_prot_t *prot = dhd->prot;
	txstatus_hdr_t * txstatus;
	unsigned long flags;
	uint32 pktid;

	/* locks required to protect circular buffer accesses */
	flags = dhd_os_spin_lock(dhd);

	txstatus = (txstatus_hdr_t *)buf;
	pktid = ltoh32(txstatus->pktid);

	prot->active_tx_count--;

	ASSERT(pktid != 0);
	dhd_prot_packet_free(dhd, pktid);

	if (prot->txflow_en == TRUE) {
		/* If the pktpool availability is above the high watermark, */
		/* let's resume the flow of packets to dongle. */
		if ((prot->max_tx_count - prot->active_tx_count) > DHD_START_QUEUE_THRESHOLD) {
			dhd_bus_start_queue(dhd->bus);
			prot->txflow_en = FALSE;
		}
	}

	dhd_os_spin_unlock(dhd, flags);
	return;
}

static void
dhd_prot_event_process(dhd_pub_t *dhd, uint8* buf, uint16 len)
{
	wl_event_hdr_t * evnt;
	uint32 bufid;
	uint16 buflen;
	int ifidx = 0;
	uint pkt_count = 1;
	void* pkt;
	unsigned long flags;

	/* Event complete header */
	evnt = (wl_event_hdr_t *)buf;
	bufid = ltoh32(evnt->rxbufid);
	buflen = ltoh16(evnt->retbuf_len);

	/* Post another rxbuf to the device */
	dhd_prot_return_rxbuf(dhd, 1);

	/* locks required to protect pktid_map */
	flags = dhd_os_spin_lock(dhd);

	pkt = dhd_prot_packet_get(dhd, ltoh32(bufid));

	dhd_os_spin_unlock(dhd, flags);

	/* DMA RX offset updated through shared area */
	if (dhd->prot->rx_dataoffset)
		PKTPULL(dhd->osh, pkt, dhd->prot->rx_dataoffset);

	PKTSETLEN(dhd->osh, pkt, buflen);

	/* remove WL header */
	PKTPULL(dhd->osh, pkt, 4); /* WL Header */

	dhd_bus_rx_frame(dhd->bus, pkt, ifidx, pkt_count);
}

static void BCMFASTPATH
dhd_prot_rxcmplt_process(dhd_pub_t *dhd, void* buf)
{
	rxcmplt_hdr_t *rxcmplt_h;
	rxcmplt_tup_t *rx_tup;
	uint32 bufid;
	uint16 buflen, cmpltcnt;
	uint16 data_offset;             /* offset at which data starts */
	void * pkt;
	int ifidx = 0;
	uint pkt_count = 0;
	uint32 i;
	void *pkthead = NULL;
	void *pkttail = NULL;

	/* RXCMPLT HDR */
	rxcmplt_h = (rxcmplt_hdr_t *)buf;
	cmpltcnt = ltoh16(rxcmplt_h->rxcmpltcnt);

	/* Post another set of rxbufs to the device */
	dhd_prot_return_rxbuf(dhd, cmpltcnt);
	ifidx = rxcmplt_h->msg.ifidx;

	rx_tup = (rxcmplt_tup_t *) &(rxcmplt_h->rx_tup[0]);
	for (i = 0; i < cmpltcnt; i++) {
		unsigned long flags;

		bufid = ltoh32(rx_tup->rxbufid);
		buflen = ltoh16(rx_tup->retbuf_len);

		/* offset from which data starts is populated in rxstatus0 */
		data_offset = ltoh16(rx_tup->data_offset);

		/* locks required to protect pktid_map */
		flags = dhd_os_spin_lock(dhd);
		pkt = dhd_prot_packet_get(dhd, ltoh32(bufid));
		dhd_os_spin_unlock(dhd, flags);

		/* data_offset from buf start */
		if (data_offset) {
			/* data offset given from dongle after split rx */
			PKTPULL(dhd->osh, pkt, data_offset); /* data offset */
		} else {
			/* DMA RX offset updated through shared area */
			if (dhd->prot->rx_dataoffset)
				PKTPULL(dhd->osh, pkt, dhd->prot->rx_dataoffset);
		}

		/* Actual length of the packet */
		PKTSETLEN(dhd->osh, pkt, buflen);

		/* remove WL header */
		PKTPULL(dhd->osh, pkt, 4); /* WL Header */

		pkt_count++;
		rx_tup++;

		/* Chain the packets and release in one shot to dhd_linux. */
		/* Interface and destination checks are not required here. */
		PKTSETNEXT(dhd->osh, pkt, NULL);
		if (pkttail == NULL) {
			pkthead = pkttail = pkt;
		} else {
			PKTSETNEXT(dhd->osh, pkttail, pkt);
			pkttail = pkt;
		}
	}

	if (pkthead) {
		/* Release the packets to dhd_linux */
		dhd_bus_rx_frame(dhd->bus, pkthead, ifidx, pkt_count);
	}
}
/* Stop protocol: sync w/dongle state. */
void dhd_prot_stop(dhd_pub_t *dhd)
{
	/* nothing to do for pcie */
}

/* Add any protocol-specific data header.
 * Caller must reserve prot_hdrlen prepend space.
 */
void dhd_prot_hdrpush(dhd_pub_t *dhd, int ifidx, void *PKTBUF)
{
	return;
}

#define PKTBUF pktbuf

int BCMFASTPATH
dhd_prot_txdata(dhd_pub_t *dhd, void *PKTBUF, uint8 ifidx)
{
	unsigned long flags;
	dhd_prot_t *prot = dhd->prot;
	circularbuf_t *htod_msgbuf = (circularbuf_t *)prot->htodbuf;
	txdescr_msghdr_t *txdesc = NULL;
	tx_lenptr_tup_t *tx_tup;
	dmaaddr_t physaddr;
	uint8 *pktdata;
	uint8 *etherhdr;
	uint16 pktlen;
	uint16 hdrlen;
	uint32 pktid;

	/* Extract the data pointer and length information */
	pktdata = PKTDATA(dhd->osh, PKTBUF);
	pktlen  = (uint16)PKTLEN(dhd->osh, PKTBUF);

	/* Extract the ethernet header and adjust the data pointer and length */
	etherhdr = pktdata;
	pktdata += ETHER_HDR_LEN;
	pktlen  -= ETHER_HDR_LEN;


	flags = dhd_os_spin_lock(dhd);

	/* Map the data pointer to a DMA-able address */
	physaddr = DMA_MAP(dhd->osh, pktdata, pktlen, DMA_TX, 0, 0);
	if (physaddr == 0) {
		DHD_ERROR(("Something really bad, unless 0 is a valid phyaddr\n"));
		ASSERT(0);
	}

	/* Create a unique 32-bit packet id */
	pktid = NATIVE_TO_PKTID(dhd->prot->pktid_map_handle, PKTBUF, physaddr, pktlen, DMA_TX);

	/* Reserve space in the circular buffer */
	hdrlen =  sizeof(txdescr_msghdr_t) + (1 * sizeof(tx_lenptr_tup_t));

	txdesc = (txdescr_msghdr_t *)dhd_alloc_circularbuf_space(dhd,
		htod_msgbuf, hdrlen, HOST_TO_DNGL_DATA);
	if (txdesc == NULL) {
		dhd_prot_packet_free(dhd, pktid);
		dhd_os_spin_unlock(dhd, flags);

		DHD_INFO(("%s:%d: HTOD Msgbuf Not available TxCount = %d\n",
			__FUNCTION__, __LINE__, prot->active_tx_count));
		return BCME_NORESOURCE;
	}

	/* Form the Tx descriptor message buffer */

	/* Common message hdr */
	txdesc->txcmn.msg.msglen = htol16(hdrlen);
	txdesc->txcmn.msg.msgtype = MSG_TYPE_TX_POST;
	txdesc->txcmn.msg.u.seq.seq_no = htol16(++prot->data_seq_no);

	/* Ethernet header */
	txdesc->txcmn.hdrlen = htol16(ETHER_HDR_LEN);
	bcopy(etherhdr, txdesc->txhdr, ETHER_HDR_LEN);

	/* Packet ID */
	txdesc->txcmn.pktid = htol32(pktid);

	/* Descriptor count - Linux needs only one */
	txdesc->txcmn.descrcnt = 0x1;

	tx_tup = (tx_lenptr_tup_t *) &(txdesc->tx_tup);

	/* Descriptor - 0 */
	tx_tup->pktlen = htol16(pktlen);
	tx_tup->ret_buf.high_addr = htol32(PHYSADDRHI(physaddr));
	tx_tup->ret_buf.low_addr  = htol32(PHYSADDRLO(physaddr));
	/* Descriptor 1 - should be filled here - if required */

	/* Reserved for future use */
	txdesc->txcmn.priority = (uint8)PKTPRIO(PKTBUF);
	txdesc->txcmn.flowid   = 0;
	txdesc->txcmn.msg.ifidx = ifidx;

	/* Since, we are filling the data directly into the bufptr obtained
	 * from the circularbuf, we can directly call the write_complete
	 */
	circularbuf_write_complete(htod_msgbuf, hdrlen);

	prot->active_tx_count++;

	/* If we have accounted for most of the lfrag packets on the dongle, */
	/* it's time to stop the packet flow - Assert flow control. */
	if ((prot->max_tx_count - prot->active_tx_count) < DHD_STOP_QUEUE_THRESHOLD) {
		dhd_bus_stop_queue(dhd->bus);
		prot->txflow_en = TRUE;
	}

	dhd_os_spin_unlock(dhd, flags);

	return BCME_OK;
}

#undef PKTBUF	/* Only defined in the above routine */
int dhd_prot_hdrpull(dhd_pub_t *dhd, int *ifidx, void *pkt, uchar *buf, uint *len)
{
	return 0;
}

static void BCMFASTPATH
dhd_prot_return_rxbuf(dhd_pub_t *dhd, uint16 rxcnt)
{
	dhd_prot_t *prot = dhd->prot;

	prot->rxbufpost -= rxcnt;
	if (prot->rxbufpost <= (prot->max_rxbufpost - RXBUFPOST_THRESHOLD))
		dhd_msgbuf_rxbuf_post(dhd);

	return;
}

/* Use protocol to issue ioctl to dongle */
int dhd_prot_ioctl(dhd_pub_t *dhd, int ifidx, wl_ioctl_t * ioc, void * buf, int len)
{
	dhd_prot_t *prot = dhd->prot;
	int ret = -1;
	uint8 action;

	if ((dhd->busstate == DHD_BUS_DOWN) || dhd->hang_was_sent) {
		DHD_ERROR(("%s : bus is down. we have nothing to do\n", __FUNCTION__));
		goto done;
	}

	DHD_TRACE(("%s: Enter\n", __FUNCTION__));

	ASSERT(len <= WLC_IOCTL_MAXLEN);

	if (len > WLC_IOCTL_MAXLEN)
		goto done;

	if (prot->pending == TRUE) {
		DHD_ERROR(("packet is pending!!!! cmd=0x%x (%lu) lastcmd=0x%x (%lu)\n",
			ioc->cmd, (unsigned long)ioc->cmd, prot->lastcmd,
			(unsigned long)prot->lastcmd));
		if ((ioc->cmd == WLC_SET_VAR) || (ioc->cmd == WLC_GET_VAR)) {
			DHD_TRACE(("iovar cmd=%s\n", (char*)buf));
		}
		goto done;
	}

	prot->pending = TRUE;
	prot->lastcmd = ioc->cmd;
	action = ioc->set;
	if (action & WL_IOCTL_ACTION_SET) {
		ret = dhd_msgbuf_set_ioctl(dhd, ifidx, ioc->cmd, buf, len, action);
	} else {
		ret = dhdmsgbuf_query_ioctl(dhd, ifidx, ioc->cmd, buf, len, action);
		if (ret > 0)
			ioc->used = ret;
	}
	/* Too many programs assume ioctl() returns 0 on success */
	if (ret >= 0)
		ret = 0;
	else {
		DHD_INFO(("%s: status ret value is %d \n", __FUNCTION__, ret));
		dhd->dongle_error = ret;
	}

	/* Intercept the wme_dp ioctl here */
	if ((!ret) && (ioc->cmd == WLC_SET_VAR) && (!strcmp(buf, "wme_dp"))) {
		int slen, val = 0;

		slen = strlen("wme_dp") + 1;
		if (len >= (int)(slen + sizeof(int)))
			bcopy(((char *)buf + slen), &val, sizeof(int));
		dhd->wme_dp = (uint8) ltoh32(val);
	}

	prot->pending = FALSE;

done:
	return ret;

}

int
dhdmsgbuf_lpbk_req(dhd_pub_t *dhd, uint len)
{
	unsigned long flags;
	dhd_prot_t *prot = dhd->prot;
	circularbuf_t *htod_msgbuf;

	ioct_reqst_hdr_t *ioct_rqst;

	uint16 hdrlen = sizeof(ioct_reqst_hdr_t);
	uint16 msglen = len + hdrlen;

	if (dhd->prot->htodsplit)
		htod_msgbuf = (circularbuf_t *) prot->htod_ctrlbuf;
	else
		htod_msgbuf = (circularbuf_t *) prot->htodbuf;

	if (msglen  > MSGBUF_MAX_MSG_SIZE)
		msglen = MSGBUF_MAX_MSG_SIZE;

	msglen = align(msglen, 4);

	/* locks required to protect circular buffer accesses */
	flags = dhd_os_spin_lock(dhd);

	if (dhd->prot->htodsplit) {
		ioct_rqst = (ioct_reqst_hdr_t *)dhd_alloc_circularbuf_space(dhd,
			htod_msgbuf, msglen, HOST_TO_DNGL_CTRL);
	}
	else {
		ioct_rqst = (ioct_reqst_hdr_t *)dhd_alloc_circularbuf_space(dhd,
			htod_msgbuf, msglen, HOST_TO_DNGL_DATA);
	}

	if (ioct_rqst == NULL) {
		dhd_os_spin_unlock(dhd, flags);
		return 0;
	}

	{
		uint8 *ptr;
		uint16 i;

		ptr = (uint8 *)ioct_rqst;
		for (i = 0; i < msglen; i++) {
			ptr[i] = i % 256;
		}
	}


	/* Common msg buf hdr */
	ioct_rqst->msg.msglen = htol16(msglen);
	ioct_rqst->msg.msgtype = MSG_TYPE_LOOPBACK;
	ioct_rqst->msg.ifidx = 0;
	ioct_rqst->msg.u.seq.seq_no = htol16(++prot->data_seq_no);

	bcm_print_bytes("LPBK REQ: ", (uint8 *)ioct_rqst, msglen);

	circularbuf_write_complete(htod_msgbuf, msglen);

	dhd_os_spin_unlock(dhd, flags);

	return 0;
}


static int
dhdmsgbuf_query_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf, uint len, uint8 action)
{
	dhd_prot_t *prot = dhd->prot;

	int ret = 0;

	DHD_TRACE(("%s: Enter\n", __FUNCTION__));

	/* Respond "bcmerror" and "bcmerrorstr" with local cache */
	if (cmd == WLC_GET_VAR && buf)
	{
		if (!strcmp((char *)buf, "bcmerrorstr"))
		{
			strncpy((char *)buf, bcmerrorstr(dhd->dongle_error), BCME_STRLEN);
			goto done;
		}
		else if (!strcmp((char *)buf, "bcmerror"))
		{
			*(int *)buf = dhd->dongle_error;
			goto done;
		}
	}

	/* Fill up msgbuf for ioctl req */
	if (len < MAX_INLINE_IOCTL_LEN) {
		/* Inline ioct resuest */
		ret = dhd_fillup_ioct_reqst(dhd, (uint16)len, cmd, buf, ifidx);
	} else {
		/* Non inline ioct resuest */
		ret = dhd_fillup_ioct_reqst_ptrbased(dhd, (uint16)len, cmd, buf, ifidx);
	}

	DHD_INFO(("ACTION %d ifdix %d cmd %d len %d \n",
		action, ifidx, cmd, len));

	/* wait for interrupt and get first fragment */
	ret = dhdmsgbuf_cmplt(dhd, prot->reqid, len, buf, prot->retbuf);

done:
	return ret;
}
static int
dhdmsgbuf_cmplt(dhd_pub_t *dhd, uint32 id, uint32 len, void* buf, void* retbuf)
{
	dhd_prot_t *prot = dhd->prot;
	ioct_resp_hdr_t  ioct_resp;
	uint8* data;
	int retlen;
	int msgbuf_len = 0;

	DHD_TRACE(("%s: Enter\n", __FUNCTION__));

	retlen = dhd_bus_rxctl(dhd->bus, (uchar*)&ioct_resp, msgbuf_len);

	if (retlen <= 0)
		return -1;

	/* get ret buf */
	if (buf != NULL) {
		if (retlen <= 4) {
			bcopy((void*)&ioct_resp.inline_data, buf, retlen);
			DHD_INFO(("%s: data is %d, ret_len is %d\n",
				__FUNCTION__, ioct_resp.inline_data, retlen));
		}
		else {
			data = (uint8*)retbuf;
			bcopy((void*)&data[prot->rx_dataoffset], buf, retlen);
		}
	}
	return ioct_resp.status;
}
static int
dhd_msgbuf_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf, uint len, uint8 action)
{
	dhd_prot_t *prot = dhd->prot;

	int ret = 0;

	DHD_TRACE(("%s: Enter \n", __FUNCTION__));
	DHD_TRACE(("%s: cmd %d len %d\n", __FUNCTION__, cmd, len));

	if (dhd->busstate == DHD_BUS_DOWN) {
		DHD_ERROR(("%s : bus is down. we have nothing to do\n", __FUNCTION__));
		return -EIO;
	}

	/* don't talk to the dongle if fw is about to be reloaded */
	if (dhd->hang_was_sent) {
		DHD_ERROR(("%s: HANG was sent up earlier. Not talking to the chip\n",
			__FUNCTION__));
		return -EIO;
	}

	/* Fill up msgbuf for ioctl req */
	if (len < MAX_INLINE_IOCTL_LEN) {
		/* Inline ioct resuest */
		ret = dhd_fillup_ioct_reqst(dhd, (uint16)len, cmd, buf, ifidx);
	} else {
		/* Non inline ioct resuest */
		ret = dhd_fillup_ioct_reqst_ptrbased(dhd, (uint16)len, cmd, buf, ifidx);
	}

	DHD_INFO(("ACTIOn %d ifdix %d cmd %d len %d \n",
		action, ifidx, cmd, len));

	ret = dhdmsgbuf_cmplt(dhd, prot->reqid, len, buf, prot->retbuf);

	return ret;
}
/* Handles a protocol control response asynchronously */
int dhd_prot_ctl_complete(dhd_pub_t *dhd)
{
	return 0;
}

/* Check for and handle local prot-specific iovar commands */
int dhd_prot_iovar_op(dhd_pub_t *dhd, const char *name,
                             void *params, int plen, void *arg, int len, bool set)
{
	return BCME_UNSUPPORTED;
}

/* Add prot dump output to a buffer */
void dhd_prot_dump(dhd_pub_t *dhd, struct bcmstrbuf *strbuf)
{

}

/* Update local copy of dongle statistics */
void dhd_prot_dstats(dhd_pub_t *dhd)
{
		return;
}

int dhd_process_pkt_reorder_info(dhd_pub_t *dhd, uchar *reorder_info_buf,
	uint reorder_info_len, void **pkt, uint32 *free_buf_count)
{
	return 0;
}
/* post a dummy message to interrupt dongle */
/* used to process cons commands */
int
dhd_post_dummy_msg(dhd_pub_t *dhd)
{
	unsigned long flags;
	hostevent_hdr_t *hevent = NULL;
	uint16 msglen = sizeof(hostevent_hdr_t);

	dhd_prot_t *prot = dhd->prot;
	circularbuf_t *htod_msgbuf;

	/* locks required to protect circular buffer accesses */
	flags = dhd_os_spin_lock(dhd);
	if (dhd->prot->htodsplit) {
		htod_msgbuf = (circularbuf_t *)prot->htod_ctrlbuf;
		hevent = (hostevent_hdr_t *)dhd_alloc_circularbuf_space(dhd,
			htod_msgbuf, msglen, HOST_TO_DNGL_CTRL);
	}
	else {
		htod_msgbuf = (circularbuf_t *)prot->htodbuf;
		hevent = (hostevent_hdr_t *)dhd_alloc_circularbuf_space(dhd,
			htod_msgbuf, msglen, HOST_TO_DNGL_DATA);
	}

	if (hevent == NULL) {
		dhd_os_spin_unlock(dhd, flags);
		return -1;
	}

	/* CMN msg header */
	hevent->msg.msglen = htol16(msglen);
	hevent->msg.msgtype = MSG_TYPE_HOST_EVNT;
	hevent->msg.ifidx = 0;
	hevent->msg.u.seq.seq_no = htol16(++prot->data_seq_no);

	/* Event payload */
	hevent->evnt_pyld = htol32(HOST_EVENT_CONS_CMD);

	/* Since, we are filling the data directly into the bufptr obtained
	 * from the msgbuf, we can directly call the write_complete
	 */
	circularbuf_write_complete(htod_msgbuf, msglen);
	dhd_os_spin_unlock(dhd, flags);

	return 0;
}
void * BCMFASTPATH
dhd_alloc_circularbuf_space(dhd_pub_t *dhd, circularbuf_t *handle, uint16 msglen, uint path)
{
	void * ret_buf;

	ret_buf = circularbuf_reserve_for_write(handle, msglen);
	if (ret_buf == NULL) {
		/* Try again after updating the read ptr from dongle */
		if (path == HOST_TO_DNGL_DATA)
			dhd_bus_cmn_readshared(dhd->bus, &(CIRCULARBUF_READ_PTR(handle)),
			HOST_TO_DNGL_RPTR);
		else if (path == HOST_TO_DNGL_CTRL)
			dhd_bus_cmn_readshared(dhd->bus, &(CIRCULARBUF_READ_PTR(handle)),
			HTOD_CTRL_RPTR);
		else
			DHD_ERROR(("%s:%d: Unknown path value \n", __FUNCTION__, __LINE__));
		ret_buf = circularbuf_reserve_for_write(handle, msglen);
		if (ret_buf == NULL) {
			DHD_INFO(("%s:%d: HTOD Msgbuf Not available \n", __FUNCTION__, __LINE__));
			return NULL;
		}
	}

	return ret_buf;
}
INLINE bool
dhd_prot_dtohsplit(dhd_pub_t* dhd)
{
	return dhd->prot->dtohsplit;
}
static int
dhd_fillup_ioct_reqst(dhd_pub_t *dhd, uint16 len, uint cmd, void* buf, int ifidx)
{
	dhd_prot_t *prot = dhd->prot;
	ioct_reqst_hdr_t *ioct_rqst;
	uint16 hdrlen = sizeof(ioct_reqst_hdr_t);
	uint16 msglen = len + hdrlen;
	circularbuf_t *htod_msgbuf;
	unsigned long flags;
	uint16 rqstlen = len;

	/* Limit ioct request to MSGBUF_MAX_MSG_SIZE bytes including hdrs */
	if (rqstlen + hdrlen > MSGBUF_MAX_MSG_SIZE)
		rqstlen = MSGBUF_MAX_MSG_SIZE - hdrlen;

	/* Messge = hdr + rqstbuf */
	msglen = rqstlen + hdrlen;

	/* align it to 4 bytes, so that all start addr form cbuf is 4 byte aligned */
	msglen = align(msglen, 4);

	/* locks required to protect circular buffer accesses */
	flags = dhd_os_spin_lock(dhd);

	/* Request for cbuf space */
	if (dhd->prot->htodsplit) {
		htod_msgbuf = (circularbuf_t *)prot->htod_ctrlbuf;
		ioct_rqst = (ioct_reqst_hdr_t *)dhd_alloc_circularbuf_space(dhd,
			htod_msgbuf, msglen, HOST_TO_DNGL_CTRL);
	}
	else {
		htod_msgbuf = (circularbuf_t *)prot->htodbuf;
		ioct_rqst = (ioct_reqst_hdr_t *)dhd_alloc_circularbuf_space(dhd,
			htod_msgbuf, msglen, HOST_TO_DNGL_DATA);
	}

	if (ioct_rqst == NULL) {
		dhd_os_spin_unlock(dhd, flags);
		return -1;
	}

	/* Common msg buf hdr */
	ioct_rqst->msg.msglen = htol16(msglen);
	ioct_rqst->msg.msgtype = MSG_TYPE_IOCTL_REQ;
	ioct_rqst->msg.ifidx = (uint8)ifidx;
	ioct_rqst->msg.u.seq.seq_no = htol16(++prot->ioctl_seq_no);

	/* Ioctl specific Message buf header */
	ioct_rqst->ioct_hdr.cmd = htol32(cmd);
	ioct_rqst->ioct_hdr.pkt_id = htol32(++prot->reqid);
	ioct_rqst->ioct_hdr.retbuf_len = htol16(len);
	ioct_rqst->ioct_hdr.xt_id = (uint16)ioct_rqst->ioct_hdr.pkt_id;
	DHD_CTL(("sending IOCTL_REQ cmd %d, pkt_id %d  xt_id %d\n",
		ioct_rqst->ioct_hdr.cmd, ioct_rqst->ioct_hdr.pkt_id, ioct_rqst->ioct_hdr.xt_id));

	/* Ret buf ptr */
	ioct_rqst->ret_buf.high_addr = htol32(PHYSADDRHI(prot->retbuf_phys));
	ioct_rqst->ret_buf.low_addr  = htol32(PHYSADDRLO(prot->retbuf_phys));

	/* copy ioct payload */
	if (buf)
		memcpy(&ioct_rqst[1], buf, rqstlen);

	/* upd wrt ptr and raise interrupt */
	circularbuf_write_complete(htod_msgbuf, msglen);
	dhd_os_spin_unlock(dhd, flags);

	return 0;
}
/* Non inline ioct request */
/* Form a ioctl request first as per ioctptr_reqst_hdr_t header in the circular buffer */
/* Form a separate request buffer where a 4 byte cmn header is added in the front */
/* buf contents from parent function is copied to remaining section of this buffer */
static int
dhd_fillup_ioct_reqst_ptrbased(dhd_pub_t *dhd, uint16 len, uint cmd, void* buf, int ifidx)
{
	dhd_prot_t *prot = dhd->prot;
	ioctptr_reqst_hdr_t *ioct_rqst;
	uint16 msglen = sizeof(ioctptr_reqst_hdr_t);
	circularbuf_t * htod_msgbuf;
	cmn_msg_hdr_t * ioct_buf;	/* For ioctl payload */
	uint16 alignlen, rqstlen = len;
	unsigned long flags;

	/* Limit ioct request to MSGBUF_MAX_MSG_SIZE bytes including hdrs */
	if ((rqstlen  + sizeof(cmn_msg_hdr_t)) > MSGBUF_MAX_MSG_SIZE)
		rqstlen = MSGBUF_MAX_MSG_SIZE - sizeof(cmn_msg_hdr_t);

	/* align it to 4 bytes, so that all start addr form cbuf is 4 byte aligned */
	alignlen = align(rqstlen, 4);

	/* locks required to protect circular buffer accesses */
	flags = dhd_os_spin_lock(dhd);
	/* Request for cbuf space */
	if (dhd->prot->htodsplit) {
		htod_msgbuf = (circularbuf_t *)prot->htod_ctrlbuf;
		ioct_rqst = (ioctptr_reqst_hdr_t*)dhd_alloc_circularbuf_space(dhd,
			htod_msgbuf, msglen, HOST_TO_DNGL_CTRL);
	}
	else {
		htod_msgbuf = (circularbuf_t *)prot->htodbuf;
		ioct_rqst = (ioctptr_reqst_hdr_t*)dhd_alloc_circularbuf_space(dhd,
			htod_msgbuf, msglen, HOST_TO_DNGL_DATA);
	}
	if (ioct_rqst == NULL) {
		dhd_os_spin_unlock(dhd, flags);
		return -1;
	}

	/* Common msg buf hdr */
	ioct_rqst->msg.msglen = htol16(msglen);
	ioct_rqst->msg.msgtype = MSG_TYPE_IOCTLPTR_REQ;
	ioct_rqst->msg.ifidx = (uint8)ifidx;
	ioct_rqst->msg.u.seq.seq_no = htol16(++prot->ioctl_seq_no);

	/* Ioctl specific Message buf header */
	ioct_rqst->ioct_hdr.cmd = htol32(cmd);
	ioct_rqst->ioct_hdr.pkt_id = htol32(++prot->reqid);
	ioct_rqst->ioct_hdr.retbuf_len = htol16(len);
	ioct_rqst->ioct_hdr.xt_id = (uint16)ioct_rqst->ioct_hdr.pkt_id;

	DHD_CTL(("sending IOCTL_PTRREQ cmd %d, pkt_id %d  xt_id %d\n",
		ioct_rqst->ioct_hdr.cmd, ioct_rqst->ioct_hdr.pkt_id, ioct_rqst->ioct_hdr.xt_id));

	/* Ret buf ptr */
	ioct_rqst->ret_buf.high_addr = htol32(PHYSADDRHI(prot->retbuf_phys));
	ioct_rqst->ret_buf.low_addr  = htol32(PHYSADDRLO(prot->retbuf_phys));

	/* copy ioct payload */
	ioct_buf = (cmn_msg_hdr_t *) prot->ioctbuf;
	ioct_buf->msglen = htol16(alignlen + sizeof(cmn_msg_hdr_t));
	ioct_buf->msgtype = MSG_TYPE_IOCT_PYLD;

	if (buf) {
		memcpy(&ioct_buf[1], buf, rqstlen);
		OSL_CACHE_FLUSH((void *) prot->ioctbuf, rqstlen+sizeof(cmn_msg_hdr_t));
	}

	if ((ulong)ioct_buf % 4)
		printf("host ioct address unaligned !!!!! \n");

	/* populate ioctl buffer info */
	ioct_rqst->ioct_hdr.buflen = htol16(alignlen + sizeof(cmn_msg_hdr_t));
	ioct_rqst->ioct_buf.high_addr = htol32(PHYSADDRHI(prot->ioctbuf_phys));
	ioct_rqst->ioct_buf.low_addr  = htol32(PHYSADDRLO(prot->ioctbuf_phys));

	/* upd wrt ptr and raise interrupt */
	circularbuf_write_complete(htod_msgbuf, msglen);

	dhd_os_spin_unlock(dhd, flags);

	return 0;
}

/* Packet to PacketID mapper */
typedef struct {
	ulong native;
	dmaaddr_t pa;
	uint32 pa_len;
	uchar dma;
} pktid_t;

typedef struct {
	void	*osh;
	void	*mwbmap_hdl;
	pktid_t *pktid_list;
	uint32	count;
} pktid_map_t;


void *pktid_map_init(void *osh, uint32 count)
{
	pktid_map_t *handle;

	handle = (pktid_map_t *) MALLOC(osh, sizeof(pktid_map_t));
	if (handle == NULL) {
		printf("%s:%d: MALLOC failed for size %d\n",
			__FUNCTION__, __LINE__, (uint32) sizeof(pktid_map_t));
		return NULL;
	}
	handle->osh = osh;
	handle->count = count;
	handle->mwbmap_hdl = bcm_mwbmap_init(osh, count);
	if (handle->mwbmap_hdl == NULL) {
		printf("%s:%d: bcm_mwbmap_init failed for count %d\n",
			__FUNCTION__, __LINE__, count);
		MFREE(osh, handle, sizeof(pktid_map_t));
		return NULL;
	}

	handle->pktid_list = (pktid_t *) MALLOC(osh, sizeof(pktid_t) * (count+1));
	if (handle->pktid_list == NULL) {
		printf("%s:%d: MALLOC failed for count %d / total = %d\n",
			__FUNCTION__, __LINE__, count, (uint32) sizeof(pktid_t) * count);
		bcm_mwbmap_fini(osh, handle->mwbmap_hdl);
		MFREE(osh, handle, sizeof(pktid_map_t));
		return NULL;
	}

	return handle;
}

void
pktid_map_uninit(void *pktid_map_handle)
{
	pktid_map_t *handle = (pktid_map_t *) pktid_map_handle;
	uint32 ix;

	if (handle != NULL) {
		void *osh = handle->osh;
		for (ix = 0; ix < MAX_PKTID_ITEMS; ix++)
		{
			if (!bcm_mwbmap_isfree(handle->mwbmap_hdl, ix)) {
				/* Mark the slot as free */
				bcm_mwbmap_free(handle->mwbmap_hdl, ix);
				/*
				Here we can do dma unmapping for 32 bit also.
				Since this in removal path, it will not affect performance
				*/
				DMA_UNMAP(osh, (uint) handle->pktid_list[ix+1].pa,
					(uint) handle->pktid_list[ix+1].pa_len,
					handle->pktid_list[ix+1].dma, 0, 0);
				PKTFREE(osh,
					(unsigned long*)handle->pktid_list[ix+1].native, TRUE);
			}
		}
		bcm_mwbmap_fini(osh, handle->mwbmap_hdl);
		MFREE(osh, handle->pktid_list, sizeof(pktid_t) * (handle->count+1));
		MFREE(osh, handle, sizeof(pktid_map_t));
	}
	return;
}

uint32 BCMFASTPATH
pktid_map_unique(void *pktid_map_handle, void *pkt, dmaaddr_t physaddr, uint32 physlen, uint32 dma)
{
	uint32 id;
	pktid_map_t *handle = (pktid_map_t *) pktid_map_handle;

	if (handle == NULL) {
		printf("%s:%d: Error !!! pktid_map_unique called without initing pktid_map\n",
			__FUNCTION__, __LINE__);
		return 0;
	}
	id = bcm_mwbmap_alloc(handle->mwbmap_hdl);
	if (id == BCM_MWBMAP_INVALID_IDX) {
		printf("%s:%d: bcm_mwbmap_alloc failed. Free Count = %d\n",
			__FUNCTION__, __LINE__, bcm_mwbmap_free_cnt(handle->mwbmap_hdl));
		return 0;
	}

	/* id=0 is invalid as we use this for error checking in the dongle */
	id += 1;
	handle->pktid_list[id].native = (ulong) pkt;
	handle->pktid_list[id].pa     = physaddr;
	handle->pktid_list[id].pa_len = (uint32) physlen;
	handle->pktid_list[id].dma = dma;

	return id;
}

void * BCMFASTPATH
pktid_get_packet(void *pktid_map_handle, uint32 id, dmaaddr_t *physaddr, uint32 *physlen)
{
	void *native = NULL;
	pktid_map_t *handle = (pktid_map_t *) pktid_map_handle;
	if (handle == NULL) {
		printf("%s:%d: Error !!! pktid_get_packet called without initing pktid_map\n",
			__FUNCTION__, __LINE__);
		return NULL;
	}

	/* Debug check */
	if (bcm_mwbmap_isfree(handle->mwbmap_hdl, (id-1))) {
		printf("%s:%d: Error !!!. How can the slot (%d) be free if the app is using it.\n",
			__FUNCTION__, __LINE__, (id-1));
		return NULL;
	}

	native = (void *) handle->pktid_list[id].native;
	*physaddr = handle->pktid_list[id].pa;
	*physlen  = (uint32) handle->pktid_list[id].pa_len;

	/* Mark the slot as free */
	bcm_mwbmap_free(handle->mwbmap_hdl, (id-1));

	return native;
}