summaryrefslogtreecommitdiff
path: root/qdf/linux/src/qdf_mem.c
blob: 86bc86d76143622c043d7e508cf1e10304070305 (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
/*
 * Copyright (c) 2014-2017 The Linux Foundation. All rights reserved.
 *
 * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
 *
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This file was originally distributed by Qualcomm Atheros, Inc.
 * under proprietary terms before Copyright ownership was assigned
 * to the Linux Foundation.
 */

/**
 * DOC: qdf_mem
 * This file provides OS dependent memory management APIs
 */

#include "qdf_debugfs.h"
#include "qdf_mem.h"
#include "qdf_nbuf.h"
#include "qdf_lock.h"
#include "qdf_mc_timer.h"
#include "qdf_module.h"
#include <qdf_trace.h>
#include "qdf_atomic.h"
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/string.h>

#ifdef CONFIG_MCL
#include <host_diag_core_event.h>
#else
#define host_log_low_resource_failure(code) do {} while (0)
#endif

#if defined(CONFIG_CNSS)
#include <net/cnss.h>
#endif

#ifdef CONFIG_WCNSS_MEM_PRE_ALLOC
#include <net/cnss_prealloc.h>
#endif

#ifdef MEMORY_DEBUG
#include <qdf_list.h>
qdf_list_t qdf_mem_list;
qdf_spinlock_t qdf_mem_list_lock;

static uint8_t WLAN_MEM_HEADER[] = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
					0x67, 0x68 };
static uint8_t WLAN_MEM_TAIL[] = { 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
					0x86, 0x87 };

/**
 * struct s_qdf_mem_struct - memory object to dubug
 * @node:	node to the list
 * @filename:	name of file
 * @line_num:	line number
 * @size:	size of the file
 * @header:	array that contains header
 * @in_use:	memory usage count
 */
struct s_qdf_mem_struct {
	qdf_list_node_t node;
	char *file_name;
	unsigned int line_num;
	unsigned int size;
	uint8_t header[8];
	qdf_atomic_t in_use;
};
#endif /* MEMORY_DEBUG */

/* Preprocessor Definitions and Constants */
#define QDF_GET_MEMORY_TIME_THRESHOLD 300

int qdf_dbg_mask;
qdf_declare_param(qdf_dbg_mask, int);
qdf_export_symbol(qdf_dbg_mask);

u_int8_t prealloc_disabled = 1;
qdf_declare_param(prealloc_disabled, byte);
qdf_export_symbol(prealloc_disabled);

#if defined WLAN_DEBUGFS && defined MEMORY_DEBUG

/**
 * struct __qdf_mem_stat - qdf memory statistics
 * @kmalloc:	total kmalloc allocations
 * @dma:	total dma allocations
 */
static struct __qdf_mem_stat {
	qdf_atomic_t kmalloc;
	qdf_atomic_t dma;
} qdf_mem_stat;


/**
 * struct __qdf_mem_info - memory statistics
 * @file_name:	the file which allocated memory
 * @line_num:	the line at which allocation happened
 * @size:	the size of allocation
 * @count:	how many allocations of same type
 *
 */
struct __qdf_mem_info {
	char *file_name;
	unsigned int line_num;
	unsigned int size;
	unsigned int count;
};

/* Debugfs root directory for qdf_mem */
static struct dentry *qdf_mem_debugfs_root;

/*
 * A table to identify duplicates in close proximity. The table depth defines
 * the proximity scope. A deeper table takes more time. Chose any optimum value.
 *
 */
#define QDF_MEM_STAT_TABLE_SIZE 4
static struct __qdf_mem_info qdf_mem_info_table[QDF_MEM_STAT_TABLE_SIZE];

static inline void qdf_mem_kmalloc_inc(qdf_size_t size)
{
	qdf_atomic_add(size, &qdf_mem_stat.kmalloc);
}

static inline void qdf_mem_dma_inc(qdf_size_t size)
{
	qdf_atomic_add(size, &qdf_mem_stat.dma);
}

static inline void qdf_mem_kmalloc_dec(qdf_size_t size)
{
	qdf_atomic_sub(size, &qdf_mem_stat.kmalloc);
}

static inline void qdf_mem_dma_dec(qdf_size_t size)
{
	qdf_atomic_sub(size, &qdf_mem_stat.dma);
}


/**
 * qdf_mem_info_table_init() - initialize the stat table
 *
 * Return: None
 */
static void qdf_mem_info_table_init(void)
{
	memset(&qdf_mem_info_table, 0, sizeof(qdf_mem_info_table));
}

/**
 * qdf_mem_get_node() - increase the node usage count
 * @n:	node
 *
 * An increased usage count will block the memory from getting released.
 * Initially the usage count is incremented from qdf_mem_malloc_debug().
 * Corresponding qdf_mem_free() will decrement the reference count and frees up
 * the memory when the usage count reaches zero. Here decrement and test is an
 * atomic operation in qdf_mem_free() to avoid any race condition.
 *
 * If a caller wants to take the ownership of an allocated memory, it can call
 * this function with the associated node.
 *
 * Return: None
 *
 */
static void qdf_mem_get_node(qdf_list_node_t *n)
{
	struct s_qdf_mem_struct *m = container_of(n, typeof(*m), node);

	qdf_atomic_inc(&m->in_use);
}

/**
 * qdf_mem_put_node_free() - decrease the node usage count and free memory
 * @n:	node
 *
 * Additionally it releases the memory when the usage count reaches zero. Usage
 * count is decremented and tested against zero in qdf_mem_free(). If the count
 * is 0, the node and associated memory gets freed.
 *
 * Return: None
 *
 */
static void qdf_mem_put_node_free(qdf_list_node_t *n)
{
	struct s_qdf_mem_struct *m = container_of(n, typeof(*m), node);

	/* qdf_mem_free() is expecting the same address returned by
	 * qdf_mem_malloc_debug(), which is 'm + sizeof(s_qdf_mem_struct)' */
	qdf_mem_free(m + 1);
}

/**
 * qdf_mem_get_first() - get the first node.
 *
 * Return: node
 */
static qdf_list_node_t *qdf_mem_get_first(void)
{
	QDF_STATUS status;
	qdf_list_node_t *node = NULL;

	qdf_spin_lock_bh(&qdf_mem_list_lock);
	status = qdf_list_peek_front(&qdf_mem_list, &node);
	if (QDF_STATUS_SUCCESS == status)
		qdf_mem_get_node(node);
	qdf_spin_unlock_bh(&qdf_mem_list_lock);

	return node;
}

/**
 * qdf_mem_get_next() - get the next node
 * @n: node
 *
 * Return: next node
 */
static qdf_list_node_t *qdf_mem_get_next(qdf_list_node_t *n)
{
	QDF_STATUS status;
	qdf_list_node_t *node = NULL;

	qdf_spin_lock_bh(&qdf_mem_list_lock);
	status = qdf_list_peek_next(&qdf_mem_list, n, &node);
	if (QDF_STATUS_SUCCESS == status)
		qdf_mem_get_node(node);

	qdf_spin_unlock_bh(&qdf_mem_list_lock);

	qdf_mem_put_node_free(n);

	return node;

}

static void qdf_mem_seq_print_header(struct seq_file *seq)
{
	seq_puts(seq, "\n");
	seq_puts(seq, "filename                             line         size x    no  [ total ]\n");
	seq_puts(seq, "\n");
}

/**
 * qdf_mem_info_table_insert() - insert node into an array
 * @n:	node
 *
 * Return:
 *	true  - success
 *	false - failure
 */
static bool qdf_mem_info_table_insert(qdf_list_node_t *n)
{
	int i;
	struct __qdf_mem_info *t = qdf_mem_info_table;
	bool dup;
	bool consumed;
	struct s_qdf_mem_struct *m = (struct s_qdf_mem_struct *)n;

	for (i = 0; i < QDF_MEM_STAT_TABLE_SIZE; i++) {
		if (!t[i].count) {
			t[i].file_name = m->file_name;
			t[i].line_num = m->line_num;
			t[i].size = m->size;
			t[i].count++;
			break;
		}
		dup = !strcmp(t[i].file_name, m->file_name) &&
		      (t[i].line_num == m->line_num) &&
		      (t[i].size == m->size);
		if (dup) {
			t[i].count++;
			break;
		}
	}

	consumed = (i < QDF_MEM_STAT_TABLE_SIZE);

	return consumed;
}

/**
 * qdf_mem_seq_print() - print the table using seq_printf
 * @seq:	seq_file handle
 *
 * Node table will be cleared once printed.
 *
 * Return: None
 */
static void qdf_mem_seq_print(struct seq_file *seq)
{
	int i;
	struct __qdf_mem_info *t = qdf_mem_info_table;

	for (i = 0; i < QDF_MEM_STAT_TABLE_SIZE && t[i].count; i++) {
		seq_printf(seq,
			   "%-35s%6d\t%6d x %4d\t[%7d]\n",
			   kbasename(t[i].file_name),
			   t[i].line_num, t[i].size,
			   t[i].count,
			   t[i].size * t[i].count);
	}

	qdf_mem_info_table_init();
}

/**
 * qdf_mem_seq_start() - sequential callback to start
 * @seq: seq_file handle
 * @pos: The start position of the sequence
 *
 * Return:
 *	SEQ_START_TOKEN - Prints header
 *	None zero value - Node
 *	NULL		- End of the sequence
 */
static void *qdf_mem_seq_start(struct seq_file *seq, loff_t *pos)
{
	if (*pos == 0) {
		qdf_mem_info_table_init();
		return SEQ_START_TOKEN;
	} else if (seq->private) {
		return qdf_mem_get_next(seq->private);
	}

	return NULL;
}

/**
 * qdf_mem_seq_next() - next sequential callback
 * @seq:	seq_file handle
 * @v:		the current iterator
 * @pos:	the current position [not used]
 *
 * Get the next node and release previous node.
 *
 * Return:
 *	None zero value - Next node
 *	NULL		- No more to process in the list
 */
static void *qdf_mem_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
	qdf_list_node_t *node;

	++*pos;

	if (v == SEQ_START_TOKEN)
		node = qdf_mem_get_first();
	else
		node = qdf_mem_get_next(v);

	return node;
}

/**
 * qdf_mem_seq_stop() - stop sequential callback
 * @seq:	seq_file handle
 * @v:		current iterator
 *
 * Return:	None
 */
static void qdf_mem_seq_stop(struct seq_file *seq, void *v)
{
	seq->private = v;
}

/**
 * qdf_mem_seq_show() - print sequential callback
 * @seq:	seq_file handle
 * @v:		current iterator
 *
 * Return: 0 - success
 */
static int qdf_mem_seq_show(struct seq_file *seq, void *v)
{

	if (v == SEQ_START_TOKEN) {
		qdf_mem_seq_print_header(seq);
		return 0;
	}

	while (!qdf_mem_info_table_insert(v))
		qdf_mem_seq_print(seq);

	return 0;
}

/* sequential file operation table */
static const struct seq_operations qdf_mem_seq_ops = {
	.start = qdf_mem_seq_start,
	.next  = qdf_mem_seq_next,
	.stop  = qdf_mem_seq_stop,
	.show  = qdf_mem_seq_show,
};


static int qdf_mem_debugfs_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &qdf_mem_seq_ops);
}

/* debugfs file operation table */
static const struct file_operations fops_qdf_mem_debugfs = {
	.owner = THIS_MODULE,
	.open = qdf_mem_debugfs_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = seq_release,
};

/**
 * qdf_mem_debugfs_init() - initialize routine
 *
 * Return: QDF_STATUS
 */
static QDF_STATUS qdf_mem_debugfs_init(void)
{
	struct dentry *qdf_debugfs_root = qdf_debugfs_get_root();

	if (!qdf_debugfs_root)
		return QDF_STATUS_E_FAILURE;

	qdf_mem_debugfs_root = debugfs_create_dir("mem", qdf_debugfs_root);

	if (!qdf_mem_debugfs_root)
		return QDF_STATUS_E_FAILURE;

	debugfs_create_file("list",
			    S_IRUSR | S_IWUSR,
			    qdf_mem_debugfs_root,
			    NULL,
			    &fops_qdf_mem_debugfs);

	debugfs_create_atomic_t("kmalloc",
				S_IRUSR | S_IWUSR,
				qdf_mem_debugfs_root,
				&qdf_mem_stat.kmalloc);

	debugfs_create_atomic_t("dma",
				S_IRUSR | S_IWUSR,
				qdf_mem_debugfs_root,
				&qdf_mem_stat.dma);

	return QDF_STATUS_SUCCESS;
}


/**
 * qdf_mem_debugfs_exit() - cleanup routine
 *
 * Return: None
 */
static void qdf_mem_debugfs_exit(void)
{
	debugfs_remove_recursive(qdf_mem_debugfs_root);
	qdf_mem_debugfs_root = NULL;
}

#else /* WLAN_DEBUGFS && MEMORY_DEBUG */

static inline void qdf_mem_kmalloc_inc(qdf_size_t size) {}
static inline void qdf_mem_dma_inc(qdf_size_t size) {}
static inline void qdf_mem_kmalloc_dec(qdf_size_t size) {}
static inline void qdf_mem_dma_dec(qdf_size_t size) {}

#ifdef MEMORY_DEBUG

static QDF_STATUS qdf_mem_debugfs_init(void)
{
	return QDF_STATUS_E_NOSUPPORT;
}
static void qdf_mem_debugfs_exit(void) {}

#endif

#endif /* WLAN_DEBUGFS && MEMORY_DEBUG */

/**
 * __qdf_mempool_init() - Create and initialize memory pool
 *
 * @osdev: platform device object
 * @pool_addr: address of the pool created
 * @elem_cnt: no. of elements in pool
 * @elem_size: size of each pool element in bytes
 * @flags: flags
 *
 * return: Handle to memory pool or NULL if allocation failed
 */
int __qdf_mempool_init(qdf_device_t osdev, __qdf_mempool_t *pool_addr,
		       int elem_cnt, size_t elem_size, u_int32_t flags)
{
	__qdf_mempool_ctxt_t *new_pool = NULL;
	u_int32_t align = L1_CACHE_BYTES;
	unsigned long aligned_pool_mem;
	int pool_id;
	int i;

	if (prealloc_disabled) {
		/* TBD: We can maintain a list of pools in qdf_device_t
		 * to help debugging
		 * when pre-allocation is not enabled
		 */
		new_pool = (__qdf_mempool_ctxt_t *)
			kmalloc(sizeof(__qdf_mempool_ctxt_t), GFP_KERNEL);
		if (new_pool == NULL)
			return QDF_STATUS_E_NOMEM;

		memset(new_pool, 0, sizeof(*new_pool));
		/* TBD: define flags for zeroing buffers etc */
		new_pool->flags = flags;
		new_pool->elem_size = elem_size;
		new_pool->max_elem = elem_cnt;
		*pool_addr = new_pool;
		return 0;
	}

	for (pool_id = 0; pool_id < MAX_MEM_POOLS; pool_id++) {
		if (osdev->mem_pool[pool_id] == NULL)
			break;
	}

	if (pool_id == MAX_MEM_POOLS)
		return -ENOMEM;

	new_pool = osdev->mem_pool[pool_id] = (__qdf_mempool_ctxt_t *)
		kmalloc(sizeof(__qdf_mempool_ctxt_t), GFP_KERNEL);
	if (new_pool == NULL)
		return -ENOMEM;

	memset(new_pool, 0, sizeof(*new_pool));
	/* TBD: define flags for zeroing buffers etc */
	new_pool->flags = flags;
	new_pool->pool_id = pool_id;

	/* Round up the element size to cacheline */
	new_pool->elem_size = roundup(elem_size, L1_CACHE_BYTES);
	new_pool->mem_size = elem_cnt * new_pool->elem_size +
				((align)?(align - 1):0);

	new_pool->pool_mem = kzalloc(new_pool->mem_size, GFP_KERNEL);
	if (new_pool->pool_mem == NULL) {
			/* TBD: Check if we need get_free_pages above */
		kfree(new_pool);
		osdev->mem_pool[pool_id] = NULL;
		return -ENOMEM;
	}

	spin_lock_init(&new_pool->lock);

	/* Initialize free list */
	aligned_pool_mem = (unsigned long)(new_pool->pool_mem) +
			((align) ? (unsigned long)(new_pool->pool_mem)%align:0);
	STAILQ_INIT(&new_pool->free_list);

	for (i = 0; i < elem_cnt; i++)
		STAILQ_INSERT_TAIL(&(new_pool->free_list),
			(mempool_elem_t *)(aligned_pool_mem +
			(new_pool->elem_size * i)), mempool_entry);


	new_pool->free_cnt = elem_cnt;
	*pool_addr = new_pool;
	return 0;
}
qdf_export_symbol(__qdf_mempool_init);

/**
 * __qdf_mempool_destroy() - Destroy memory pool
 * @osdev: platform device object
 * @Handle: to memory pool
 *
 * Returns: none
 */
void __qdf_mempool_destroy(qdf_device_t osdev, __qdf_mempool_t pool)
{
	int pool_id = 0;

	if (!pool)
		return;

	if (prealloc_disabled) {
		kfree(pool);
		return;
	}

	pool_id = pool->pool_id;

	/* TBD: Check if free count matches elem_cnt if debug is enabled */
	kfree(pool->pool_mem);
	kfree(pool);
	osdev->mem_pool[pool_id] = NULL;
}
qdf_export_symbol(__qdf_mempool_destroy);

/**
 * __qdf_mempool_alloc() - Allocate an element memory pool
 *
 * @osdev: platform device object
 * @Handle: to memory pool
 *
 * Return: Pointer to the allocated element or NULL if the pool is empty
 */
void *__qdf_mempool_alloc(qdf_device_t osdev, __qdf_mempool_t pool)
{
	void *buf = NULL;

	if (!pool)
		return NULL;

	if (prealloc_disabled)
		return  qdf_mem_malloc(pool->elem_size);

	spin_lock_bh(&pool->lock);

	buf = STAILQ_FIRST(&pool->free_list);
	if (buf != NULL) {
		STAILQ_REMOVE_HEAD(&pool->free_list, mempool_entry);
		pool->free_cnt--;
	}

	/* TBD: Update free count if debug is enabled */
	spin_unlock_bh(&pool->lock);

	return buf;
}
qdf_export_symbol(__qdf_mempool_alloc);

/**
 * __qdf_mempool_free() - Free a memory pool element
 * @osdev: Platform device object
 * @pool: Handle to memory pool
 * @buf: Element to be freed
 *
 * Returns: none
 */
void __qdf_mempool_free(qdf_device_t osdev, __qdf_mempool_t pool, void *buf)
{
	if (!pool)
		return;


	if (prealloc_disabled)
		return qdf_mem_free(buf);

	spin_lock_bh(&pool->lock);
	pool->free_cnt++;

	STAILQ_INSERT_TAIL
		(&pool->free_list, (mempool_elem_t *)buf, mempool_entry);
	spin_unlock_bh(&pool->lock);
}
qdf_export_symbol(__qdf_mempool_free);

/**
 * qdf_mem_alloc_outline() - allocation QDF memory
 * @osdev: platform device object
 * @size: Number of bytes of memory to allocate.
 *
 * This function will dynamicallly allocate the specified number of bytes of
 * memory.
 *
 * Return:
 * Upon successful allocate, returns a non-NULL pointer to the allocated
 * memory.  If this function is unable to allocate the amount of memory
 * specified (for any reason) it returns NULL.
 */
void *
qdf_mem_alloc_outline(qdf_device_t osdev, size_t size)
{
	return qdf_mem_malloc(size);
}
qdf_export_symbol(qdf_mem_alloc_outline);

/**
 * qdf_mem_free_outline() - QDF memory free API
 * @ptr: Pointer to the starting address of the memory to be free'd.
 *
 * This function will free the memory pointed to by 'ptr'. It also checks
 * is memory is corrupted or getting double freed and panic.
 *
 * Return: none
 */
void
qdf_mem_free_outline(void *buf)
{
	qdf_mem_free(buf);
}
qdf_export_symbol(qdf_mem_free_outline);

/**
 * qdf_mem_zero_outline() - zero out memory
 * @buf: pointer to memory that will be set to zero
 * @size: number of bytes zero
 *
 * This function sets the memory location to all zeros, essentially clearing
 * the memory.
 *
 * Return: none
 */
void
qdf_mem_zero_outline(void *buf, qdf_size_t size)
{
	qdf_mem_zero(buf, size);
}
qdf_export_symbol(qdf_mem_zero_outline);

#ifdef CONFIG_WCNSS_MEM_PRE_ALLOC
/**
 * qdf_mem_prealloc_get() - conditionally pre-allocate memory
 * @size: the number of bytes to allocate
 *
 * If size if greater than WCNSS_PRE_ALLOC_GET_THRESHOLD, this function returns
 * a chunk of pre-allocated memory. If size if less than or equal to
 * WCNSS_PRE_ALLOC_GET_THRESHOLD, or an error occurs, NULL is returned instead.
 *
 * Return: NULL on failure, non-NULL on success
 */
static void *qdf_mem_prealloc_get(size_t size)
{
	void *mem;

	if (size <= WCNSS_PRE_ALLOC_GET_THRESHOLD)
		return NULL;

	mem = wcnss_prealloc_get(size);
	if (mem)
		memset(mem, 0, size);

	return mem;
}

static inline bool qdf_mem_prealloc_put(void *ptr)
{
	return wcnss_prealloc_put(ptr);
}
#else
static inline void *qdf_mem_prealloc_get(size_t size)
{
	return NULL;
}

static inline bool qdf_mem_prealloc_put(void *ptr)
{
	return false;
}
#endif /* CONFIG_WCNSS_MEM_PRE_ALLOC */

/* External Function implementation */
#ifdef MEMORY_DEBUG

/**
 * qdf_mem_init() - initialize qdf memory debug functionality
 *
 * Return: none
 */
void qdf_mem_init(void)
{
	/* Initalizing the list with maximum size of 60000 */
	qdf_list_create(&qdf_mem_list, 60000);
	qdf_spinlock_create(&qdf_mem_list_lock);
	qdf_net_buf_debug_init();
	qdf_mem_debugfs_init();
	return;
}
qdf_export_symbol(qdf_mem_init);

/**
 * qdf_mem_clean() - display memory leak debug info and free leaked pointers
 *
 * Return: none
 */
void qdf_mem_clean(void)
{
	uint32_t list_size;
	list_size = qdf_list_size(&qdf_mem_list);
	if (list_size) {
		qdf_list_node_t *node;
		QDF_STATUS qdf_status;

		struct s_qdf_mem_struct *mem_struct;
		char *prev_mleak_file = "";
		unsigned int prev_mleak_line_num = 0;
		unsigned int prev_mleak_sz = 0;
		unsigned int mleak_cnt = 0;

		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_INFO,
			  "%s: List is not Empty. list_size %d ",
			  __func__, (int)list_size);

		do {
			qdf_spin_lock(&qdf_mem_list_lock);
			qdf_status =
				qdf_list_remove_front(&qdf_mem_list, &node);
			qdf_spin_unlock(&qdf_mem_list_lock);
			if (QDF_STATUS_SUCCESS == qdf_status) {
				mem_struct = (struct s_qdf_mem_struct *)node;
				/* Take care to log only once multiple memory
				   leaks from the same place */
				if (strcmp(prev_mleak_file,
					mem_struct->file_name)
				    || (prev_mleak_line_num !=
					mem_struct->line_num)
				    || (prev_mleak_sz != mem_struct->size)) {
					if (mleak_cnt != 0) {
						QDF_TRACE(QDF_MODULE_ID_QDF,
							  QDF_TRACE_LEVEL_FATAL,
							  "%d Time Memory Leak@ File %s, @Line %d, size %d",
							  mleak_cnt,
							  prev_mleak_file,
							  prev_mleak_line_num,
							  prev_mleak_sz);
					}
					prev_mleak_file = mem_struct->file_name;
					prev_mleak_line_num =
						 mem_struct->line_num;
					prev_mleak_sz = mem_struct->size;
					mleak_cnt = 0;
				}
				mleak_cnt++;
				kfree((void *)mem_struct);
			}
		} while (qdf_status == QDF_STATUS_SUCCESS);

		/* Print last memory leak from the module */
		if (mleak_cnt) {
			QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_FATAL,
				  "%d Time memory Leak@ File %s, @Line %d, size %d",
				  mleak_cnt, prev_mleak_file,
				  prev_mleak_line_num, prev_mleak_sz);
		}
#ifdef CONFIG_HALT_KMEMLEAK
		BUG_ON(0);
#endif
	}
}
qdf_export_symbol(qdf_mem_clean);

/**
 * qdf_mem_exit() - exit qdf memory debug functionality
 *
 * Return: none
 */
void qdf_mem_exit(void)
{
	qdf_mem_debugfs_exit();
	qdf_net_buf_debug_exit();
	qdf_mem_clean();
	qdf_list_destroy(&qdf_mem_list);
}
qdf_export_symbol(qdf_mem_exit);

/**
 * qdf_mem_malloc_debug() - debug version of QDF memory allocation API
 * @size: Number of bytes of memory to allocate.
 * @file_name: File name from which memory allocation is called
 * @line_num: Line number from which memory allocation is called
 *
 * This function will dynamicallly allocate the specified number of bytes of
 * memory and ad it in qdf tracking list to check against memory leaks and
 * corruptions
 *
 * Return:
 * Upon successful allocate, returns a non-NULL pointer to the allocated
 * memory.  If this function is unable to allocate the amount of memory
 * specified (for any reason) it returns %NULL.
 */
void *qdf_mem_malloc_debug(size_t size,
			char *file_name, uint32_t line_num)
{
	struct s_qdf_mem_struct *mem_struct;
	void *mem_ptr = NULL;
	uint32_t new_size;
	int flags = GFP_KERNEL;
	unsigned long  time_before_kzalloc;

	if (size > (1024 * 1024) || size == 0) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s: called with invalid arg; passed in %zu !!!",
			  __func__, size);
		host_log_low_resource_failure(WIFI_EVENT_MEMORY_FAILURE);
		return NULL;
	}

	mem_ptr = qdf_mem_prealloc_get(size);
	if (mem_ptr)
		return mem_ptr;

	if (in_interrupt() || irqs_disabled() || in_atomic())
		flags = GFP_ATOMIC;

	new_size = size + sizeof(struct s_qdf_mem_struct) + 8;/*TBD: what is 8*/
	time_before_kzalloc = qdf_mc_timer_get_system_time();
	mem_struct = (struct s_qdf_mem_struct *)kzalloc(new_size, flags);
	/**
	 * If time taken by kmalloc is greater than
	 * QDF_GET_MEMORY_TIME_THRESHOLD msec
	 */
	if (qdf_mc_timer_get_system_time() - time_before_kzalloc >=
					  QDF_GET_MEMORY_TIME_THRESHOLD)
			QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
				  "%s: kzalloc took %lu msec for size %zu called from %p_s at line %d",
			 __func__,
			 qdf_mc_timer_get_system_time() - time_before_kzalloc,
			 size, (void *)_RET_IP_, line_num);

	if (mem_struct != NULL) {
		QDF_STATUS qdf_status;

		mem_struct->file_name = file_name;
		mem_struct->line_num = line_num;
		mem_struct->size = size;
		qdf_atomic_inc(&mem_struct->in_use);
		qdf_mem_kmalloc_inc(size);

		qdf_mem_copy(&mem_struct->header[0],
			     &WLAN_MEM_HEADER[0], sizeof(WLAN_MEM_HEADER));

		qdf_mem_copy((uint8_t *) (mem_struct + 1) + size,
			     &WLAN_MEM_TAIL[0], sizeof(WLAN_MEM_TAIL));

		qdf_spin_lock_irqsave(&qdf_mem_list_lock);
		qdf_status = qdf_list_insert_front(&qdf_mem_list,
						   &mem_struct->node);
		qdf_spin_unlock_irqrestore(&qdf_mem_list_lock);
		if (QDF_STATUS_SUCCESS != qdf_status) {
			QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
				  "%s: Unable to insert node into List qdf_status %d",
				  __func__, qdf_status);
		}

		mem_ptr = (void *)(mem_struct + 1);
	}

	return mem_ptr;
}
qdf_export_symbol(qdf_mem_malloc_debug);

/**
 * qdf_mem_validate_node_for_free() - validate that the node is in a list
 * @qdf_node: node to check for being in a list
 *
 * qdf_node should be a non null value.
 *
 * Return: true if the node validly linked in an anchored doubly linked list
 */
static bool qdf_mem_validate_node_for_free(qdf_list_node_t *qdf_node)
{
	struct list_head *node = qdf_node;

	/*
	 * if the node is an empty list, it is not tied to an anchor node
	 * and must have been removed with list_del_init
	 */
	if (list_empty(node))
		return false;

	if (node->prev == NULL)
		return false;

	if (node->next == NULL)
		return false;

	if (node->prev->next != node)
		return false;

	if (node->next->prev != node)
		return false;

	return true;
}



/**
 * qdf_mem_free() - QDF memory free API
 * @ptr: Pointer to the starting address of the memory to be free'd.
 *
 * This function will free the memory pointed to by 'ptr'. It also checks
 * is memory is corrupted or getting double freed and panic.
 *
 * Return: none
 */
void qdf_mem_free(void *ptr)
{
	struct s_qdf_mem_struct *mem_struct;

	/* freeing a null pointer is valid */
	if (qdf_unlikely(ptr == NULL))
		return;

	mem_struct = ((struct s_qdf_mem_struct *)ptr) - 1;

	if (qdf_unlikely(mem_struct == NULL)) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_FATAL,
			  "%s: null mem_struct", __func__);
		QDF_BUG(0);
	}

	if (qdf_mem_prealloc_put(ptr))
		return;

	if (!qdf_atomic_dec_and_test(&mem_struct->in_use))
		return;

	qdf_spin_lock_irqsave(&qdf_mem_list_lock);

	/*
	 * invalid memory access when checking the header/tailer
	 * would be a use after free and would indicate a double free
	 * or invalid pointer passed.
	 */
	if (qdf_mem_cmp(mem_struct->header, &WLAN_MEM_HEADER[0],
			sizeof(WLAN_MEM_HEADER)))
		goto error;

	/*
	 * invalid memory access while checking validate node
	 * would indicate corruption in the nodes pointed to
	 */
	if (!qdf_mem_validate_node_for_free(&mem_struct->node))
		goto error;

	/*
	 * invalid memory access here is unlikely and would imply
	 * that the size value was corrupted/incorrect.
	 * It is unlikely that the above checks would pass in a
	 * double free case.
	 */
	if (qdf_mem_cmp((uint8_t *) ptr + mem_struct->size,
			&WLAN_MEM_TAIL[0], sizeof(WLAN_MEM_TAIL)))
		goto error;

	/*
	 * make the node an empty list before doing the spin unlock
	 * The empty list check will guarantee that we avoid a race condition.
	 */
	list_del_init(&mem_struct->node);
	qdf_mem_list.count--;
	qdf_spin_unlock_irqrestore(&qdf_mem_list_lock);
	qdf_mem_kmalloc_dec(mem_struct->size);
	kfree(mem_struct);
	return;

error:
	if (!qdf_list_has_node(&qdf_mem_list, &mem_struct->node)) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_FATAL,
			  "%s: Unallocated memory (double free?)",
			  __func__);
		qdf_spin_unlock_irqrestore(&qdf_mem_list_lock);
		QDF_BUG(0);
	}

	if (qdf_mem_cmp(mem_struct->header, &WLAN_MEM_HEADER[0],
				sizeof(WLAN_MEM_HEADER))) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_FATAL,
			  "Memory Header is corrupted.");
		qdf_spin_unlock_irqrestore(&qdf_mem_list_lock);
		QDF_BUG(0);
	}

	if (!qdf_mem_validate_node_for_free(&mem_struct->node)) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_FATAL,
			  "Memory_struct is corrupted.");
		qdf_spin_unlock_irqrestore(&qdf_mem_list_lock);
		QDF_BUG(0);
	}

	if (qdf_mem_cmp((uint8_t *) ptr + mem_struct->size,
			&WLAN_MEM_TAIL[0], sizeof(WLAN_MEM_TAIL))) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_FATAL,
			  "Memory Trailer is corrupted. mem_info: Filename %s, line_num %d",
			  mem_struct->file_name, (int)mem_struct->line_num);
		qdf_spin_unlock_irqrestore(&qdf_mem_list_lock);
		QDF_BUG(0);
	}

	QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_FATAL,
		  "%s unexpected error", __func__);
	qdf_spin_unlock_irqrestore(&qdf_mem_list_lock);
	QDF_BUG(0);
}
qdf_export_symbol(qdf_mem_free);
#else

/**
 * qdf_mem_malloc() - allocation QDF memory
 * @size: Number of bytes of memory to allocate.
 *
 * This function will dynamicallly allocate the specified number of bytes of
 * memory.
 *
 * Return:
 * Upon successful allocate, returns a non-NULL pointer to the allocated
 * memory.  If this function is unable to allocate the amount of memory
 * specified (for any reason) it returns NULL.
 */
void *qdf_mem_malloc(size_t size)
{
	int flags = GFP_KERNEL;
	void *mem;

	mem = qdf_mem_prealloc_get(size);
	if (mem)
		return mem;

	if (in_interrupt() || irqs_disabled())
		flags = GFP_ATOMIC;

	return kzalloc(size, flags);
}
qdf_export_symbol(qdf_mem_malloc);

/**
 * qdf_mem_free() - free QDF memory
 * @ptr: Pointer to the starting address of the memory to be free'd.
 *
 * This function will free the memory pointed to by 'ptr'.
 *
 * Return: None
 */
void qdf_mem_free(void *ptr)
{
	if (ptr == NULL)
		return;

	if (qdf_mem_prealloc_put(ptr))
		return;

	kfree(ptr);
}
qdf_export_symbol(qdf_mem_free);
#endif

/**
 * qdf_mem_multi_pages_alloc() - allocate large size of kernel memory
 * @osdev: OS device handle pointer
 * @pages: Multi page information storage
 * @element_size: Each element size
 * @element_num: Total number of elements should be allocated
 * @memctxt: Memory context
 * @cacheable: Coherent memory or cacheable memory
 *
 * This function will allocate large size of memory over multiple pages.
 * Large size of contiguous memory allocation will fail frequently, then
 * instead of allocate large memory by one shot, allocate through multiple, non
 * contiguous memory and combine pages when actual usage
 *
 * Return: None
 */
void qdf_mem_multi_pages_alloc(qdf_device_t osdev,
			       struct qdf_mem_multi_page_t *pages,
			       size_t element_size, uint16_t element_num,
			       qdf_dma_context_t memctxt, bool cacheable)
{
	uint16_t page_idx;
	struct qdf_mem_dma_page_t *dma_pages;
	void **cacheable_pages = NULL;
	uint16_t i;

	pages->num_element_per_page = PAGE_SIZE / element_size;
	if (!pages->num_element_per_page) {
		qdf_print("Invalid page %d or element size %d",
			  (int)PAGE_SIZE, (int)element_size);
		goto out_fail;
	}

	pages->num_pages = element_num / pages->num_element_per_page;
	if (element_num % pages->num_element_per_page)
		pages->num_pages++;

	if (cacheable) {
		/* Pages information storage */
		pages->cacheable_pages = qdf_mem_malloc(
			pages->num_pages * sizeof(pages->cacheable_pages));
		if (!pages->cacheable_pages) {
			qdf_print("Cacheable page storage alloc fail");
			goto out_fail;
		}

		cacheable_pages = pages->cacheable_pages;
		for (page_idx = 0; page_idx < pages->num_pages; page_idx++) {
			cacheable_pages[page_idx] = qdf_mem_malloc(PAGE_SIZE);
			if (!cacheable_pages[page_idx]) {
				qdf_print("cacheable page alloc fail, pi %d",
					  page_idx);
				goto page_alloc_fail;
			}
		}
		pages->dma_pages = NULL;
	} else {
		pages->dma_pages = qdf_mem_malloc(
			pages->num_pages * sizeof(struct qdf_mem_dma_page_t));
		if (!pages->dma_pages) {
			qdf_print("dmaable page storage alloc fail");
			goto out_fail;
		}

		dma_pages = pages->dma_pages;
		for (page_idx = 0; page_idx < pages->num_pages; page_idx++) {
			dma_pages->page_v_addr_start =
				qdf_mem_alloc_consistent(osdev, osdev->dev,
					 PAGE_SIZE,
					&dma_pages->page_p_addr);
			if (!dma_pages->page_v_addr_start) {
				qdf_print("dmaable page alloc fail pi %d",
					page_idx);
				goto page_alloc_fail;
			}
			dma_pages->page_v_addr_end =
				dma_pages->page_v_addr_start + PAGE_SIZE;
			dma_pages++;
		}
		pages->cacheable_pages = NULL;
	}
	return;

page_alloc_fail:
	if (cacheable) {
		for (i = 0; i < page_idx; i++)
			qdf_mem_free(pages->cacheable_pages[i]);
		qdf_mem_free(pages->cacheable_pages);
	} else {
		dma_pages = pages->dma_pages;
		for (i = 0; i < page_idx; i++) {
			qdf_mem_free_consistent(osdev, osdev->dev, PAGE_SIZE,
				dma_pages->page_v_addr_start,
				dma_pages->page_p_addr, memctxt);
			dma_pages++;
		}
		qdf_mem_free(pages->dma_pages);
	}

out_fail:
	pages->cacheable_pages = NULL;
	pages->dma_pages = NULL;
	pages->num_pages = 0;
	return;
}
qdf_export_symbol(qdf_mem_multi_pages_alloc);

/**
 * qdf_mem_multi_pages_free() - free large size of kernel memory
 * @osdev: OS device handle pointer
 * @pages: Multi page information storage
 * @memctxt: Memory context
 * @cacheable: Coherent memory or cacheable memory
 *
 * This function will free large size of memory over multiple pages.
 *
 * Return: None
 */
void qdf_mem_multi_pages_free(qdf_device_t osdev,
			      struct qdf_mem_multi_page_t *pages,
			      qdf_dma_context_t memctxt, bool cacheable)
{
	unsigned int page_idx;
	struct qdf_mem_dma_page_t *dma_pages;

	if (cacheable) {
		for (page_idx = 0; page_idx < pages->num_pages; page_idx++)
			qdf_mem_free(pages->cacheable_pages[page_idx]);
		qdf_mem_free(pages->cacheable_pages);
	} else {
		dma_pages = pages->dma_pages;
		for (page_idx = 0; page_idx < pages->num_pages; page_idx++) {
			qdf_mem_free_consistent(osdev, osdev->dev, PAGE_SIZE,
				dma_pages->page_v_addr_start,
				dma_pages->page_p_addr, memctxt);
			dma_pages++;
		}
		qdf_mem_free(pages->dma_pages);
	}

	pages->cacheable_pages = NULL;
	pages->dma_pages = NULL;
	pages->num_pages = 0;
	return;
}
qdf_export_symbol(qdf_mem_multi_pages_free);

/**
 * qdf_mem_copy() - copy memory
 * @dst_addr: Pointer to destination memory location (to copy to)
 * @src_addr: Pointer to source memory location (to copy from)
 * @num_bytes: Number of bytes to copy.
 *
 * Copy host memory from one location to another, similar to memcpy in
 * standard C.  Note this function does not specifically handle overlapping
 * source and destination memory locations.  Calling this function with
 * overlapping source and destination memory locations will result in
 * unpredictable results.  Use qdf_mem_move() if the memory locations
 * for the source and destination are overlapping (or could be overlapping!)
 *
 * Return: none
 */
void qdf_mem_copy(void *dst_addr, const void *src_addr, uint32_t num_bytes)
{
	if (0 == num_bytes) {
		/* special case where dst_addr or src_addr can be NULL */
		return;
	}

	if ((dst_addr == NULL) || (src_addr == NULL)) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s called with NULL parameter, source:%p destination:%p",
			  __func__, src_addr, dst_addr);
		QDF_ASSERT(0);
		return;
	}
	memcpy(dst_addr, src_addr, num_bytes);
}
qdf_export_symbol(qdf_mem_copy);

/**
 * qdf_mem_zero() - zero out memory
 * @ptr: pointer to memory that will be set to zero
 * @num_bytes: number of bytes zero
 *
 * This function sets the memory location to all zeros, essentially clearing
 * the memory.
 *
 * Return: None
 */
void qdf_mem_zero(void *ptr, uint32_t num_bytes)
{
	if (0 == num_bytes) {
		/* special case where ptr can be NULL */
		return;
	}

	if (ptr == NULL) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s called with NULL parameter ptr", __func__);
		return;
	}
	memset(ptr, 0, num_bytes);
}
qdf_export_symbol(qdf_mem_zero);

/**
 * qdf_mem_set() - set (fill) memory with a specified byte value.
 * @ptr: Pointer to memory that will be set
 * @num_bytes: Number of bytes to be set
 * @value: Byte set in memory
 *
 * Return: None
 */
void qdf_mem_set(void *ptr, uint32_t num_bytes, uint32_t value)
{
	if (ptr == NULL) {
		qdf_print("%s called with NULL parameter ptr", __func__);
		return;
	}
	memset(ptr, value, num_bytes);
}
qdf_export_symbol(qdf_mem_set);

/**
 * qdf_mem_move() - move memory
 * @dst_addr: pointer to destination memory location (to move to)
 * @src_addr: pointer to source memory location (to move from)
 * @num_bytes: number of bytes to move.
 *
 * Move host memory from one location to another, similar to memmove in
 * standard C.  Note this function *does* handle overlapping
 * source and destination memory locations.

 * Return: None
 */
void qdf_mem_move(void *dst_addr, const void *src_addr, uint32_t num_bytes)
{
	if (0 == num_bytes) {
		/* special case where dst_addr or src_addr can be NULL */
		return;
	}

	if ((dst_addr == NULL) || (src_addr == NULL)) {
		QDF_TRACE(QDF_MODULE_ID_QDF, QDF_TRACE_LEVEL_ERROR,
			  "%s called with NULL parameter, source:%p destination:%p",
			  __func__, src_addr, dst_addr);
		QDF_ASSERT(0);
		return;
	}
	memmove(dst_addr, src_addr, num_bytes);
}
qdf_export_symbol(qdf_mem_move);

#if defined(A_SIMOS_DEVHOST) || defined(HIF_SDIO) || defined(HIF_USB)
/**
 * qdf_mem_alloc_consistent() - allocates consistent qdf memory
 * @osdev: OS device handle
 * @dev: Pointer to device handle
 * @size: Size to be allocated
 * @phy_addr: Physical address
 *
 * Return: pointer of allocated memory or null if memory alloc fails
 */
void *qdf_mem_alloc_consistent(qdf_device_t osdev, void *dev, qdf_size_t size,
			       qdf_dma_addr_t *phy_addr)
{
	void *vaddr;

	vaddr = qdf_mem_malloc(size);
	*phy_addr = ((uintptr_t) vaddr);
	/* using this type conversion to suppress "cast from pointer to integer
	 * of different size" warning on some platforms
	 */
	BUILD_BUG_ON(sizeof(*phy_addr) < sizeof(vaddr));
	return vaddr;
}

#else
void *qdf_mem_alloc_consistent(qdf_device_t osdev, void *dev, qdf_size_t size,
			       qdf_dma_addr_t *phy_addr)
{
	int flags = GFP_KERNEL;
	void *alloc_mem = NULL;

	if (in_interrupt() || irqs_disabled() || in_atomic())
		flags = GFP_ATOMIC;

	alloc_mem = dma_alloc_coherent(dev, size, phy_addr, flags);
	if (alloc_mem == NULL)
		qdf_print("%s Warning: unable to alloc consistent memory of size %zu!\n",
			__func__, size);
	qdf_mem_dma_inc(size);
	return alloc_mem;
}

#endif
qdf_export_symbol(qdf_mem_alloc_consistent);

#if defined(A_SIMOS_DEVHOST) ||  defined(HIF_SDIO) || defined(HIF_USB)
/**
 * qdf_mem_free_consistent() - free consistent qdf memory
 * @osdev: OS device handle
 * @size: Size to be allocated
 * @vaddr: virtual address
 * @phy_addr: Physical address
 * @mctx: Pointer to DMA context
 *
 * Return: none
 */
inline void qdf_mem_free_consistent(qdf_device_t osdev, void *dev,
				    qdf_size_t size, void *vaddr,
				    qdf_dma_addr_t phy_addr,
				    qdf_dma_context_t memctx)
{
	qdf_mem_free(vaddr);
	return;
}

#else
inline void qdf_mem_free_consistent(qdf_device_t osdev, void *dev,
				    qdf_size_t size, void *vaddr,
				    qdf_dma_addr_t phy_addr,
				    qdf_dma_context_t memctx)
{
	dma_free_coherent(dev, size, vaddr, phy_addr);
	qdf_mem_dma_dec(size);
}

#endif
qdf_export_symbol(qdf_mem_free_consistent);

/**
 * qdf_mem_dma_sync_single_for_device() - assign memory to device
 * @osdev: OS device handle
 * @bus_addr: dma address to give to the device
 * @size: Size of the memory block
 * @direction: direction data will be DMAed
 *
 * Assign memory to the remote device.
 * The cache lines are flushed to ram or invalidated as needed.
 *
 * Return: none
 */
void qdf_mem_dma_sync_single_for_device(qdf_device_t osdev,
					qdf_dma_addr_t bus_addr,
					qdf_size_t size,
					enum dma_data_direction direction)
{
	dma_sync_single_for_device(osdev->dev, bus_addr,  size, direction);
}
qdf_export_symbol(qdf_mem_dma_sync_single_for_device);

/**
 * qdf_mem_dma_sync_single_for_cpu() - assign memory to CPU
 * @osdev: OS device handle
 * @bus_addr: dma address to give to the cpu
 * @size: Size of the memory block
 * @direction: direction data will be DMAed
 *
 * Assign memory to the CPU.
 *
 * Return: none
 */
void qdf_mem_dma_sync_single_for_cpu(qdf_device_t osdev,
				     qdf_dma_addr_t bus_addr,
				     qdf_size_t size,
				     enum dma_data_direction direction)
{
	dma_sync_single_for_cpu(osdev->dev, bus_addr,  size, direction);
}
qdf_export_symbol(qdf_mem_dma_sync_single_for_cpu);