summaryrefslogtreecommitdiff
path: root/drivers/dsp/syslink/multicore_ipc/listmp_sharedmemory.c
blob: 1530a89717a5b3bdf653aeaf0ddc70ed94c47c8a (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
/*
 *  listmp_sharedmemory.c
 *
 *  The listmp_sharedmemory is a linked-list based module designed to be
 *  used in a multi-processor environment.  It is designed to
 *  provide a means of communication between different processors.
 *
 *  Copyright (C) 2008-2009 Texas Instruments, Inc.
 *
 *  This package is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 *  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 *  WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
 *  PURPOSE.
 */

/*!
 *  This module is instance based. Each instance requires a small
 *  piece of shared memory. This is specified via the #shared_addr
 *  parameter to the create. The proper #shared_addr_size parameter
 *  can be determined via the #shared_memreq call. Note: the
 *  parameters to this function must be the same that will used to
 *  create or open the instance.
 *  The listmp_sharedmemory module uses a #NameServer instance
 *  to store instance information when an instance is created and
 *  the name parameter is non-NULL. If a name is supplied, it must
 *  be unique for all listmp_sharedmemory instances.
 *  The #create also initializes the shared memory as needed. The
 *  shared memory must be initialized to 0 or all ones
 *  (e.g. 0xFFFFFFFFF) before the listmp_sharedmemory instance
 *  is created.
 *  Once an instance is created, an open can be performed. The #open
 *  is used to gain access to the same listmp_sharedmemory instance.
 *  Generally an instance is created on one processor and opened
 *  on the other processor.
 *  The open returns a listmp_sharedmemory instance handle like the
 *  create, however the open does not modify the shared memory.
 *  Generally an instance is created on one processor and opened
 *  on the other processor.
 *  There are two options when opening the instance:
 *  @li Supply the same name as specified in the create. The
 *  listmp_sharedmemory module queries the NameServer to get the
 *  needed information.
 *  @li Supply the same #shared_addr value as specified in the
 *  create.
 *  If the open is called before the instance is created, open
 *  returns NULL.
 *  There is currently a list of restrictions for the module:
 *  @li Both processors must have the same endianness. Endianness
 *  conversion may supported in a future version of
 *  listmp_sharedmemory.
 *  @li The module will be made a gated module
 */


/* Standard headers */
#include <linux/types.h>
#include <linux/module.h>

/* Utilities headers */
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/string.h>

/* Syslink headers */
#include <syslink/atomic_linux.h>

/* Module level headers */
#include <nameserver.h>
#include <sharedregion.h>
#include <multiproc.h>
#include "_listmp.h"
#include <listmp.h>
#include <gatepeterson.h>
#include <listmp_sharedmemory.h>


/* =============================================================================
 * Globals
 * =============================================================================
 */
/*! @brief Macro to make a correct module magic number with refCount */
#define LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(x)                                  \
				((LISTMPSHAREDMEMORY_MODULEID << 12u) | (x))

/*!
 *  @brief  Name of the reserved NameServer used for listmp_sharedmemory.
 */
#define LISTMP_SHAREDMEMORY_NAMESERVER	"ListMPSharedMemory"

/*!
 *  @brief  Cache size
 */
#define LISTMP_SHAREDMEMORY_CACHESIZE	128



/* =============================================================================
 * Structures and Enums
 * =============================================================================
 */
/*! @brief structure for listmp_sharedmemory module state */
struct listmp_sharedmemory_module_object {
	atomic_t              ref_count;
	/*!< Reference count */
	void *ns_handle;
	/*!< Handle to the local NameServer used for storing GP objects */
	struct list_head obj_list;
	/*!< List holding created listmp_sharedmemory objects */
	struct mutex *local_gate;
	/*!< Handle to lock for protecting obj_list */
	struct listmp_config cfg;
	/*!< Current config values */
	struct listmp_config default_cfg;
	/*!< Default config values */
	listmp_sharedmemory_params default_inst_params;
	/*!< Default instance creation parameters */
};

/*!
 *  @var	listmp_sharedmemory_nameServer
 *
 *  @brief  Name of the reserved NameServer used for listmp_sharedmemory.
 */
static
struct listmp_sharedmemory_module_object listmp_sharedmemory_state = {
			.default_cfg.max_name_len = 32,
			.default_cfg.use_name_server = true,
			.default_inst_params.shared_addr = 0,
			.default_inst_params.shared_addr_size = 0,
			.default_inst_params.name = NULL,
			.default_inst_params.gate = NULL,
			.default_inst_params.list_type = listmp_type_SHARED};

/*!
 *  @brief  Structure for the internal Handle for the listmp_sharedmemory.
 */
struct listmp_sharedmemory_obj{
	struct list_head list_elem;
	/*!< Used for creating a linked list */
	VOLATILE struct listmp_elem *listmp_elem;
	/*!< Used for storing listmp_sharedmemory element */
	struct listmp_proc_attrs *owner;
	/*!< Creator's attributes associated with an instance */
	listmp_sharedmemory_params params;
	/*!< the parameter structure */
	void *ns_key;
	u32 index;
	/*!< the index for SrPtr */
	VOLATILE struct listmp_attrs *attrs;
	/*!< Shared memory attributes */
	void *top;
	/*!< Pointer to the top Object */
};


/* =============================================================================
 * Function definitions
 * =============================================================================
 */
/*
 * ======== _listmp_sharedmemory_create ========
 *  Purpose:
 *  Creates a new instance of listmp_sharedmemory module. This is an internal
 *  function because both listmp_sharedmemory_create and
 *  listmp_sharedmemory_open call use the same functionality.
 */
static listmp_sharedmemory_handle
_listmp_sharedmemory_create(listmp_sharedmemory_params *params,
				u32 create_flag);


/* =============================================================================
 * Function API's
 * =============================================================================
 */
/*
 * ======== listmp_sharedmemory_get_config ========
 *  Purpose:
 *  Function to get configuration parameters to setup the
 *  the listmp_sharedmemory module.
 */
int listmp_sharedmemory_get_config(struct listmp_config *cfgParams)
{
	int status = 0;

	if (WARN_ON(cfgParams == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true)
		/* If setup has not yet been called) */
		memcpy(cfgParams, &listmp_sharedmemory_state.default_cfg,
			sizeof(struct listmp_config));
	else
		memcpy(cfgParams, &listmp_sharedmemory_state.cfg,
			sizeof(struct listmp_config));
	return 0;

exit:
	printk(KERN_ERR "listmp_sharedmemory_get_config failed: "
		"status = 0x%x\n", status);
	return status;
}

/*
 * ======== listmp_sharedmemory_setup ========
 *  Purpose:
 *  Function to setup the listmp_sharedmemory module.
 */
int listmp_sharedmemory_setup(struct listmp_config *config)
{
	int status = 0;
	int status1 = 0;
	void *nshandle = NULL;
	struct nameserver_params params;
	struct listmp_config tmp_cfg;

	/* This sets the refCount variable is not initialized, upper 16 bits is
	* written with module Id to ensure correctness of refCount variable.
	*/
	atomic_cmpmask_and_set(&listmp_sharedmemory_state.ref_count,
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0));

	if (atomic_inc_return(&listmp_sharedmemory_state.ref_count)
				!= LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) {
		return 1;
	}

	if (config == NULL) {
		listmp_sharedmemory_get_config(&tmp_cfg);
		config = &tmp_cfg;
	}

	if (WARN_ON(config->max_name_len == 0)) {
		status = -EINVAL;
		goto exit;
	}

	if (likely((config->use_name_server == true))) {
		/* Initialize the parameters */
		nameserver_params_init(&params);
		params.max_value_len = 4;
		params.max_name_len = config->max_name_len;
		/* Create the nameserver for modules */
		nshandle = nameserver_create(
				LISTMP_SHAREDMEMORY_NAMESERVER, &params);
		if (unlikely(nshandle == NULL)) {
			status = LISTMPSHAREDMEMORY_E_FAIL;
			goto exit;
		}
	}

	listmp_sharedmemory_state.ns_handle = nshandle;
	/* Construct the list object */
	INIT_LIST_HEAD(&listmp_sharedmemory_state.obj_list);
	/* Create a lock for protecting list object */
	listmp_sharedmemory_state.local_gate = \
		kmalloc(sizeof(struct mutex), GFP_KERNEL);
	if (listmp_sharedmemory_state.local_gate == NULL) {
		status = -ENOMEM;
		goto clean_nameserver;
	}

	mutex_init(listmp_sharedmemory_state.local_gate);
	/* Copy the cfg */
	memcpy(&listmp_sharedmemory_state.cfg, config,
			sizeof(struct listmp_config));
	return 0;

clean_nameserver:
	printk(KERN_ERR "listmp_sharedmemory_setup: Failed to create the local "
		"gate! status = 0x%x\n", status);
	atomic_set(&listmp_sharedmemory_state.ref_count,
					LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0));
	status1 = nameserver_delete
		(&(listmp_sharedmemory_state.ns_handle));
	BUG_ON(status1 < 0);

exit:
	printk(KERN_ERR "listmp_sharedmemory_setup failed! status = 0x%x\n",
		status);
	return status;
}

/*
 * ======== listmp_sharedmemory_destroy ========
 *  Purpose:
 *  Function to destroy the listmp_sharedmemory module.
 */
int listmp_sharedmemory_destroy(void)
{
	int status = 0;
	int status1 = 0;
	struct list_head *elem = NULL;
	struct list_head *head = &listmp_sharedmemory_state.obj_list;
	struct list_head *next;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}

	if (atomic_dec_return(&listmp_sharedmemory_state.ref_count)
			== LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0)) {
		/* Temporarily increment refCount here. */
		atomic_set(&listmp_sharedmemory_state.ref_count,
				LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1));
		/* Check if any listmp_sharedmemory instances have not been
		 * deleted so far. If not, delete them. */
		for (elem = (head)->next; elem != (head); elem = next) {
			/* Retain the next pointer so it doesn't
			   get overwritten */
			next = elem->next;
			if (((struct listmp_sharedmemory_obj *) elem)->owner
				->proc_id == multiproc_get_id(NULL)) {
				status1 = listmp_sharedmemory_delete(
				(listmp_sharedmemory_handle *)
				&(((struct listmp_sharedmemory_obj *) \
							elem)->top));
				WARN_ON(status1 < 0);
			} else {
				status1 = listmp_sharedmemory_close(
				(listmp_sharedmemory_handle)
				(((struct listmp_sharedmemory_obj *) \
							elem)->top));
				WARN_ON(status1 < 0);
			}
		}

		/* Again reset refCount. */
		atomic_set(&listmp_sharedmemory_state.ref_count,
				LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0));
		if (likely(listmp_sharedmemory_state.cfg.use_name_server
								== true)) {
			/* Delete the nameserver for modules */
			status = nameserver_delete(
				&(listmp_sharedmemory_state.ns_handle));
			BUG_ON(status < 0);
		}

		/* Destruct the list object */
		list_del(&listmp_sharedmemory_state.obj_list);
		/* Delete the list lock */
		kfree(listmp_sharedmemory_state.local_gate);
		listmp_sharedmemory_state.local_gate = NULL;
		memset(&listmp_sharedmemory_state.cfg, 0,
			sizeof(struct listmp_config));
		/* Decrease the refCount */
		atomic_set(&listmp_sharedmemory_state.ref_count,
				LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0));
	}

exit:
	if (status < 0) {
		printk(KERN_ERR "listmp_sharedmemory_destroy failed! "
			"status = 0x%x\n", status);
	}
	return status;
}

/*
 * ======== listmp_sharedmemory_params_init ========
 *  Purpose:
 *  Function to initialize the config-params structure with supplier-specified
 *  defaults before instance creation.
 */
void listmp_sharedmemory_params_init(listmp_sharedmemory_handle handle,
				listmp_sharedmemory_params *params)
{
	s32 status = 0;
	struct listmp_sharedmemory_obj *obj = NULL;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}

	if (WARN_ON(params == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	if (handle == NULL) {
		memcpy(params,
		&(listmp_sharedmemory_state.default_inst_params),
		sizeof(listmp_sharedmemory_params));
	} else {
		obj = (struct listmp_sharedmemory_obj *)
				(((listmp_sharedmemory_object *) handle)->obj);

			memcpy((void *)&obj->params,
			(void *)params,
			 sizeof(listmp_sharedmemory_params));
	}
	return;

exit:
	printk(KERN_ERR "listmp_sharedmemory_params_init failed! "
		"status = 0x%x\n", status);
	return;
}

/*
 * ======== listmp_sharedmemory_create ========
 *  Purpose:
 *  Creates a new instance of listmp_sharedmemory module.
 */
listmp_sharedmemory_handle listmp_sharedmemory_create(
				listmp_sharedmemory_params *params)
{
	s32 status = 0;
	listmp_sharedmemory_object *handle = NULL;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}

	if (WARN_ON(params == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	if (WARN_ON((params->shared_addr == (u32)NULL)
		&& (params->list_type != listmp_type_FAST))) {
		status = -EINVAL;
		goto exit;
	}
	if (WARN_ON(params->shared_addr_size
		< listmp_sharedmemory_shared_memreq(params))) {
		status = -EINVAL;
		goto exit;
	}

	handle = (listmp_sharedmemory_object *)
		_listmp_sharedmemory_create(params, true);

exit:
	if (status < 0) {
		printk(KERN_ERR "listmp_sharedmemory_create failed! "
			"status = 0x%x\n", status);
	}
	return (listmp_sharedmemory_handle) handle;
}

/*
 * ======== listmp_sharedmemory_delete ========
 *  Purpose:
 *  Deletes a instance of listmp_sharedmemory instance object.
 */
int listmp_sharedmemory_delete(listmp_sharedmemory_handle *listmp_handleptr)
{
	int status = 0;
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	listmp_sharedmemory_params *params = NULL;
	u32 key;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}
	if (WARN_ON(listmp_handleptr == NULL)) {
		status = -EINVAL;
		goto exit;
	}
	if (WARN_ON(*listmp_handleptr == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	handle = (listmp_sharedmemory_object *) (*listmp_handleptr);
	obj = (struct listmp_sharedmemory_obj *) handle->obj;
	params = (listmp_sharedmemory_params *) &obj->params;

	if (obj->owner->proc_id != multiproc_get_id(NULL)) {
		status = -EPERM;
		goto exit;
	}

	if (obj->owner->open_count > 1) {
		status = -EBUSY;
		goto exit;
	}

	if (obj->owner->open_count != 1) {
		status = -EBUSY;
		goto exit;
	}

	/* Remove from  the local list */
	key = mutex_lock_interruptible(
				listmp_sharedmemory_state.local_gate);
	list_del(&obj->list_elem);
	mutex_unlock(listmp_sharedmemory_state.local_gate);

	if (likely(params->name != NULL)) {
		/* Free memory for the name */
		kfree(params->name);
		/* Remove from the name server */
		if (likely(listmp_sharedmemory_state.cfg.use_name_server)) {
			if (obj->ns_key != NULL) {
				nameserver_remove_entry(
					listmp_sharedmemory_state.ns_handle,
					obj->ns_key);
				obj->ns_key = NULL;
			}
		}
	}

	/* Free memory for the processor info's */
	kfree(obj->owner);
	/* Now free the handle */
	kfree(obj);
	obj = NULL;

	/* Now free the handle */
	kfree(handle);
	handle = NULL;
	*listmp_handleptr = NULL;

exit:
	if (status < 0) {
		printk(KERN_ERR "listmp_sharedmemory_delete failed! "
			"status = 0x%x\n", status);
	}
	return status;

}


/*
 * ======== listmp_sharedmemory_shared_memreq ========
 *  Purpose:
 *  Function to return the amount of shared memory required for creation of
 *  each instance.
 */
int listmp_sharedmemory_shared_memreq(listmp_sharedmemory_params *params)
{
	int retval = 0;

	if (WARN_ON(params == NULL)) {
		retval = -EINVAL;
		goto exit;
	}

	if (params->list_type == listmp_type_SHARED)
		retval = (LISTMP_SHAREDMEMORY_CACHESIZE * 2);

exit:
	if (retval < 0) {
		printk(KERN_ERR "listmp_sharedmemory_shared_memreq failed! "
			"retval = 0x%x\n", retval);
	}
	/*! @retval	((1 * sizeof(struct listmp_elem))
	*!		+  1 * sizeof(struct listmp_attrs)) if params is null */
	/*! @retval (2 * cacheSize) if params is provided */
	/*! @retval (0) if hardware queue */
	return retval;
}

/*
 * ======== listmp_sharedmemory_open ========
 *  Purpose:
 *  Function to open a listmp_sharedmemory instance
 */
int listmp_sharedmemory_open(listmp_sharedmemory_handle *listmp_handleptr,
				listmp_sharedmemory_params *params)
{
	int status = 0;
	bool done_flag = false;
	struct list_head *elem;
	u32 key;
	u32 shared_shm_base;
	struct listmp_attrs *attrs;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}
	if (WARN_ON(listmp_handleptr == NULL)) {
		status = -EINVAL;
		goto exit;
	}
	if (WARN_ON(params == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	if (WARN_ON((listmp_sharedmemory_state.cfg.use_name_server == false)
				&& (params->shared_addr == (u32)NULL))) {
		status = -EINVAL;
		goto exit;
	}

	if (WARN_ON((listmp_sharedmemory_state.cfg.use_name_server == true)
					&& (params->shared_addr == (u32)NULL))
						&& (params->name == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	/* First check in the local list */
	list_for_each(elem, &listmp_sharedmemory_state.obj_list) {
		if (((struct listmp_sharedmemory_obj *)elem)->params.shared_addr
						== params->shared_addr) {
			key = mutex_lock_interruptible(
				listmp_sharedmemory_state.local_gate);
			if (((struct listmp_sharedmemory_obj *)elem)
				->owner->proc_id
				== multiproc_get_id(NULL))
				((struct listmp_sharedmemory_obj *)elem)
					->owner->open_count++;
			mutex_unlock(
				listmp_sharedmemory_state.local_gate);
			*listmp_handleptr = \
				(((struct listmp_sharedmemory_obj *)
				elem)->top);
			done_flag = true;
			break;
		} else if ((params->name != NULL)
			&& (((struct listmp_sharedmemory_obj *)elem) \
						->params.name != NULL)){
			if (strcmp(((struct listmp_sharedmemory_obj *)elem)
				->params.name, params->name) == 0) {
				key = mutex_lock_interruptible(
					listmp_sharedmemory_state.local_gate);
				if (((struct listmp_sharedmemory_obj *)elem)
					->owner->proc_id
					== multiproc_get_id(NULL))
					((struct listmp_sharedmemory_obj *)elem)
						->owner->open_count++;
				mutex_unlock(
					listmp_sharedmemory_state.local_gate);
				*listmp_handleptr = \
					(((struct listmp_sharedmemory_obj *)
					elem)->top);
				done_flag = true;
				break;
			}
		}
	}

	if (likely(done_flag == false)) {
		if (unlikely(params->shared_addr == NULL)) {
			if (likely(listmp_sharedmemory_state.cfg.use_name_server
								 == true)){
				/* Find in name server */
				status =
				nameserver_get(
					listmp_sharedmemory_state.ns_handle,
					params->name,
					&shared_shm_base,
					sizeof(u32),
					NULL);
				if (status >= 0)
					params->shared_addr =
						sharedregion_get_ptr(
						(u32 *)shared_shm_base);
				if (params->shared_addr == NULL) {
					status =
					LISTMPSHAREDMEMORY_E_NOTCREATED;
					goto exit;
				}
			}
		}
	}

	if (status >= 0) {
		attrs = (struct listmp_attrs *) (params->shared_addr);
		if (unlikely(attrs->status != (LISTMP_SHAREDMEMORY_CREATED)))
				status = LISTMPSHAREDMEMORY_E_NOTCREATED;
		else if (unlikely(attrs->version !=
					(LISTMP_SHAREDMEMORY_VERSION)))
				status = LISTMPSHAREDMEMORY_E_VERSION;
	}

	if (likely(status >= 0))
		*listmp_handleptr = (listmp_sharedmemory_handle)
				_listmp_sharedmemory_create(params, false);

exit:
	if (status < 0) {
		printk(KERN_ERR "listmp_sharedmemory_open failed! "
			"status = 0x%x\n", status);
	}
	return status;
}

/*
 * ======== listmp_sharedmemory_close ========
 *  Purpose:
 *  Function to close a previously opened instance
 */
int listmp_sharedmemory_close(listmp_sharedmemory_handle  listmp_handle)
{
	int status = 0;
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	listmp_sharedmemory_params *params = NULL;
	u32 key;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}

	if (WARN_ON(listmp_handle == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	handle = (listmp_sharedmemory_object *)listmp_handle;
	obj = (struct listmp_sharedmemory_obj *) handle->obj;
	if (obj == NULL) {
		status = -EINVAL;
		goto exit;
	}

	key = mutex_lock_interruptible(
		listmp_sharedmemory_state.local_gate);
	if (obj->owner->proc_id == multiproc_get_id(NULL))
		(obj)->owner->open_count--;

	params = (listmp_sharedmemory_params *) &obj->params;
	/* Check if ListMP is opened on same processor*/
	if (((struct listmp_sharedmemory_obj *)obj)->owner->creator == false) {
		list_del(&obj->list_elem);
		/* remove from the name server */
		if (params->name != NULL)
			/* Free memory for the name */
			kfree(params->name);

		kfree(obj->owner);
		kfree(obj);
		obj = NULL;
		kfree(handle);
		handle = NULL;
	}

	mutex_unlock(listmp_sharedmemory_state.local_gate);

exit:
	if (status < 0) {
		printk(KERN_ERR "listmp_sharedmemory_close failed! "
			"status = 0x%x\n", status);
	}
	return status;
}

/*
 * ======== listmp_sharedmemory_empty ========
 *  Purpose:
 *  Function to check if the shared memory list is empty
 */
bool listmp_sharedmemory_empty(listmp_sharedmemory_handle listmp_handle)
{

	int status = 0;
	bool is_empty = false;
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	s32 retval = 0;
	struct listmp_elem *sharedHead;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}

	if (WARN_ON(listmp_handle == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	handle = (listmp_sharedmemory_object *)listmp_handle;
	obj = (struct listmp_sharedmemory_obj *) handle->obj;

	if (obj->params.gate != NULL) {
		retval = gatepeterson_enter(obj->params.gate);
		if (retval < 0) {
			status = -EINVAL;
			goto exit;
		}
	}

	/*! @retval true if list is empty */
	sharedHead = (struct listmp_elem *)(sharedregion_get_srptr(
				(void *)obj->listmp_elem, obj->index));

	if (obj->listmp_elem->next == sharedHead)
		is_empty = true;

	if (obj->params.gate != NULL)
		gatepeterson_leave(obj->params.gate, 0);

exit:
	return is_empty;
}

/*
 * ======== listmp_sharedmemory_get_head ========
 *  Purpose:
 *  Function to get head element from a shared memory list
 */
void *listmp_sharedmemory_get_head(listmp_sharedmemory_handle listmp_handle)
{
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	struct listmp_elem *elem = NULL;
	struct listmp_elem *localHeadNext = NULL;
	struct listmp_elem *localNext = NULL;
	struct listmp_elem *sharedHead = NULL;
	s32 retval = 0;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true)
		goto exit;

	if (WARN_ON(listmp_handle == NULL)) {
		/*! @retval  NULL if listmp_handle passed is NULL */
		elem = NULL;
		goto exit;
	}

	handle = (listmp_sharedmemory_object *)listmp_handle;
	obj = (struct listmp_sharedmemory_obj *) handle->obj;

	if (obj->params.gate != NULL) {
		retval = gatepeterson_enter(obj->params.gate);
		if (retval < 0)
			goto exit;
	}

	localHeadNext = sharedregion_get_ptr((u32 *)obj->listmp_elem->next);
	/* See if the listmp_sharedmemory_object was empty */
	if (localHeadNext == (struct listmp_elem *)obj->listmp_elem) {
		/*! @retval NULL if list is empty */
		elem = NULL ;
	} else {
		/* Elem to return */
		elem = localHeadNext;

		localNext = sharedregion_get_ptr((u32 *)elem->next);
		sharedHead = (struct listmp_elem *) sharedregion_get_srptr(
				(void *)obj->listmp_elem, obj->index);

		/* Fix the head of the list next pointer */
		obj->listmp_elem->next = elem->next;

		/* Fix the prev pointer of the new first elem on the
		list */
		localNext->prev = sharedHead;
	}

	if (obj->params.gate != NULL)
		gatepeterson_leave(obj->params.gate, 0);

exit:
	return elem;
}

/*
 * ======== listmp_sharedmemory_get_tail ========
 *  Purpose:
 *  Function to get tail element from a shared memory list
 */
void *listmp_sharedmemory_get_tail(listmp_sharedmemory_handle listmp_handle)
{
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	struct listmp_elem *elem = NULL;
	struct listmp_elem *localHeadNext = NULL;
	struct listmp_elem *localHeadPrev = NULL;
	struct listmp_elem *localPrev = NULL;
	struct listmp_elem *sharedHead = NULL;
	s32 retval = 0;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true)
		goto exit;

	if (WARN_ON(listmp_sharedmemory_state.ns_handle == NULL)) {
		/*! @retval NULL if Module was not initialized */
		elem = NULL;
		goto exit;
	}
	if (WARN_ON(listmp_handle == NULL)) {
		/*! @retval NULL if listmp_handle passed is NULL */
		elem = NULL;
		goto exit;
	}

	handle = (listmp_sharedmemory_object *)listmp_handle;
	obj = (struct listmp_sharedmemory_obj *) handle->obj;

	if (obj->params.gate != NULL) {
		retval = gatepeterson_enter(obj->params.gate);
		if (retval < 0)
			goto exit;
	}

	localHeadNext = sharedregion_get_ptr((u32 *)obj->listmp_elem->next);
	localHeadPrev = sharedregion_get_ptr((u32 *)obj->listmp_elem->prev);

	/* See if the listmp_sharedmemory_object was empty */
	if (localHeadNext == (struct listmp_elem *)obj->listmp_elem) {
		/* Empty, return NULL */
		elem = NULL ;
	} else {
		/* Elem to return */
		elem = localHeadPrev;
		localPrev = sharedregion_get_ptr((u32 *)elem->prev);
		sharedHead = (struct listmp_elem *) sharedregion_get_srptr(
					(void *)obj->listmp_elem, obj->index);
		obj->listmp_elem->prev = elem->prev;
		localPrev->next = sharedHead;
	}

	if (obj->params.gate != NULL)
		gatepeterson_leave(obj->params.gate, 0);

exit:
	return elem;
}

/*
 * ======== listmp_sharedmemory_put_head ========
 *  Purpose:
 *  Function to put head element into a shared memory list
 */
int listmp_sharedmemory_put_head(listmp_sharedmemory_handle listmp_handle,
				struct listmp_elem *elem)
{
	int status = 0;
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	struct listmp_elem *localNextElem = NULL;
	struct listmp_elem *sharedElem = NULL;
	struct listmp_elem *sharedHead = NULL;
	s32 retval = 0;
	u32 index;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}

	if (WARN_ON(listmp_handle == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	handle = (listmp_sharedmemory_object *)listmp_handle;
	obj = (struct listmp_sharedmemory_obj *) handle->obj;

	index = sharedregion_get_index(elem);
	sharedElem = (struct listmp_elem *) sharedregion_get_srptr(elem, index);
	sharedHead = (struct listmp_elem *)sharedregion_get_srptr(
					(void *)obj->listmp_elem, obj->index);

	if (obj->params.gate != NULL) {
		retval = gatepeterson_enter(obj->params.gate);
		if (retval < 0) {
			status = -EINVAL;
			goto exit;
		}
	}

	/* Add the new elem into the list */
	elem->next = obj->listmp_elem->next;
	elem->prev = sharedHead;
	localNextElem = sharedregion_get_ptr((u32 *)elem->next);
	localNextElem->prev = sharedElem;
	obj->listmp_elem->next = sharedElem;

	if (obj->params.gate != NULL)
		gatepeterson_leave(obj->params.gate, 0);

exit:
	if (status < 0) {
		printk(KERN_ERR "listmp_sharedmemory_put_head failed! "
			"status = 0x%x\n", status);
	}
	return status;
}

/*
 * ======== listmp_sharedmemory_put_tail ========
 *  Purpose:
 *  Function to put tail element into a shared memory list
 */
int listmp_sharedmemory_put_tail(listmp_sharedmemory_handle listmp_handle,
				struct listmp_elem *elem)
{
	int status = 0;
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	struct listmp_elem *localPrevElem = NULL;
	struct listmp_elem *sharedElem = NULL;
	struct listmp_elem *sharedHead = NULL;
	s32 retval = 0;
	u32 index;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}

	if (WARN_ON(elem == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	handle = (listmp_sharedmemory_object *)listmp_handle;
	obj = (struct listmp_sharedmemory_obj *) handle->obj;

	/* Safe to do outside the gate */
	index = sharedregion_get_index(elem);
	sharedElem = (struct listmp_elem *)
			sharedregion_get_srptr(elem, index);
	sharedHead = (struct listmp_elem *)sharedregion_get_srptr
				((void *)obj->listmp_elem,
				obj->index);

	if (obj->params.gate != NULL) {
		retval = gatepeterson_enter(obj->params.gate);
		if (retval < 0) {
			status = -EINVAL;
			goto exit;
		}
	}

	/* Add the new elem into the list */
	elem->next = sharedHead;
	elem->prev = obj->listmp_elem->prev;
	localPrevElem = sharedregion_get_ptr((u32 *)elem->prev);
	localPrevElem->next = sharedElem;
	obj->listmp_elem->prev = sharedElem;

	if (obj->params.gate != NULL)
		gatepeterson_leave(obj->params.gate, 0);

exit:
	if (status < 0) {
		printk(KERN_ERR "listmp_sharedmemory_put_tail failed! "
			"status = 0x%x\n", status);
	}
	return status;
}

/*
 * ======== listmp_sharedmemory_insert ========
 *  Purpose:
 *  Function to insert an element into a shared memory list
 */
int listmp_sharedmemory_insert(listmp_sharedmemory_handle  listmp_handle,
				struct listmp_elem *new_elem,
				struct listmp_elem *cur_elem)
{
	int status = 0;
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	struct listmp_elem *localPrevElem = NULL;
	struct listmp_elem *sharedNewElem = NULL;
	struct listmp_elem *sharedCurElem = NULL;
	s32 retval = 0;
	u32 index;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}

	if (WARN_ON(new_elem == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	handle = (listmp_sharedmemory_object *)listmp_handle;
	obj = (struct listmp_sharedmemory_obj *) handle->obj;

	if (obj->params.gate != NULL) {
		retval = gatepeterson_enter(obj->params.gate);
		if (retval < 0) {
			status = -EINVAL;
			goto exit;
		}
	}

	/* If NULL change cur_elem to the obj */
	if (cur_elem == NULL)
		cur_elem = (struct listmp_elem *)obj->listmp_elem->next;

	/* Get SRPtr for new_elem */
	index = sharedregion_get_index(new_elem);
	sharedNewElem = (struct listmp_elem *)
				sharedregion_get_srptr(new_elem, index);

	/* Get SRPtr for cur_elem */
	index = sharedregion_get_index(cur_elem);
	sharedCurElem = (struct listmp_elem *)
				sharedregion_get_srptr(cur_elem, index);

	/* Get SRPtr for cur_elem->prev */
	localPrevElem = sharedregion_get_ptr((u32 *)cur_elem->prev);

	new_elem->next = sharedCurElem;
	new_elem->prev = cur_elem->prev;
	localPrevElem->next = sharedNewElem;
	cur_elem->prev = sharedNewElem;

	if (obj->params.gate != NULL)
		gatepeterson_leave(obj->params.gate, 0);

exit:
	if (status < 0) {
		printk(KERN_ERR "listmp_sharedmemory_insert failed! "
			"status = 0x%x\n", status);
	}
	return status;
}

/*
 * ======== listmp_sharedmemory_remove ========
 *  Purpose:
 *  Function to remove a element from a shared memory list
 */
int listmp_sharedmemory_remove(listmp_sharedmemory_handle  listmp_handle,
				struct listmp_elem *elem)
{
	int status = 0;
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj	*obj = NULL;
	struct listmp_elem *localPrevElem = NULL;
	struct listmp_elem *localNextElem = NULL;
	s32 retval = 0;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
			LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true) {
		status = -ENODEV;
		goto exit;
	}

	if (WARN_ON(elem == NULL)) {
		status = -EINVAL;
		goto exit;
	}

	handle = (listmp_sharedmemory_object *)listmp_handle;
	obj = (struct listmp_sharedmemory_obj *) handle->obj;

	if (obj->params.gate != NULL) {
		retval = gatepeterson_enter(obj->params.gate);
		if (retval < 0) {
			status = -EINVAL;
			goto exit;
		}
	}

	localPrevElem = sharedregion_get_ptr((u32 *)elem->prev);
	localNextElem = sharedregion_get_ptr((u32 *)elem->next);

	localPrevElem->next = elem->next;
	localNextElem->prev = elem->prev;

	if (obj->params.gate != NULL)
		gatepeterson_leave(obj->params.gate, 0);

exit:
	if (status < 0) {
		printk(KERN_ERR "listmp_sharedmemory_remove failed! "
			"status = 0x%x\n", status);
	}
	return status;
}

/*
 * ======== listmp_sharedmemory_next ========
 *  Purpose:
 *  Function to traverse to next element in shared memory list
 */
void *listmp_sharedmemory_next(listmp_sharedmemory_handle listmp_handle,
				struct listmp_elem *elem)
{
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	struct listmp_elem *retElem = NULL;
	struct listmp_elem *sharedNextElem = NULL;
	s32 retval = 0;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
				LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
				LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true)
		goto exit;

	handle = (listmp_sharedmemory_object *)listmp_handle;
	obj = (struct listmp_sharedmemory_obj *) handle->obj;

	if (obj->params.gate != NULL) {
		retval = gatepeterson_enter(obj->params.gate);
		if (retval < 0)
			goto exit;
	}

	/* If element is NULL start at head */
	if (elem == NULL)
		sharedNextElem = (struct listmp_elem *) obj->listmp_elem->next;
	else
		sharedNextElem = (struct listmp_elem *)elem->next;

	retElem = sharedregion_get_ptr((u32 *)sharedNextElem);

	/*! @retval NULL if list is empty */
	if (retElem == (struct listmp_elem *)obj->listmp_elem)
		retElem = NULL;
	if (obj->params.gate != NULL)
		gatepeterson_leave(obj->params.gate, 0);

exit:
	return retElem;
}

/*
 * ======== listmp_sharedmemory_prev ========
 *  Purpose:
 *  Function to traverse to prev element in shared memory list
 */
void *listmp_sharedmemory_prev(listmp_sharedmemory_handle listmp_handle,
				struct listmp_elem *elem)
{
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	struct listmp_elem *retElem = NULL;
	struct listmp_elem *sharedPrevElem = NULL;
	s32 retval = 0;

	if (atomic_cmpmask_and_lt(&(listmp_sharedmemory_state.ref_count),
				LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(0),
				LISTMPSHAREDMEMORY_MAKE_MAGICSTAMP(1)) == true)
		goto exit;

	handle = (listmp_sharedmemory_object *)listmp_handle;
	obj = (struct listmp_sharedmemory_obj *) handle->obj;

	if (obj->params.gate != NULL) {
		retval = gatepeterson_enter(obj->params.gate);
		if (retval < 0)
			goto exit;
	}

	/* If elem is NULL use head */
	if (elem == NULL)
		sharedPrevElem = (struct listmp_elem *)
						obj->listmp_elem->prev;
	else
		sharedPrevElem = (struct listmp_elem *)elem->prev;

	retElem = sharedregion_get_ptr((u32 *)sharedPrevElem);

	/*! @retval NULL if list is empty */
	if (retElem == (struct listmp_elem *)(obj->listmp_elem))
		retElem = NULL;

	if (obj->params.gate != NULL)
		gatepeterson_leave(obj->params.gate, 0);

exit:
	return retElem;
}

/*
 * ======== _listmp_sharedmemory_create ========
 *  Purpose:
 *  Creates a new instance of listmp_sharedmemory module. This is an internal
 *  function because both listmp_sharedmemory_create and
 *  listmp_sharedmemory_open call use the same functionality.
 */
listmp_sharedmemory_handle _listmp_sharedmemory_create(
		listmp_sharedmemory_params *params, u32 create_flag)
{
	int status = 0;
	listmp_sharedmemory_object *handle = NULL;
	struct listmp_sharedmemory_obj *obj = NULL;
	u32 key;
	u32 shm_index;
	u32 *shared_shm_base;

	BUG_ON(params == NULL);

	/* Allow local lock not being provided. Don't do protection if local
	* lock is not provided.
	*/
	/* Create the handle */
	handle = kzalloc(sizeof(listmp_sharedmemory_object), GFP_KERNEL);
	if (handle == NULL) {
		status = -ENOMEM;
		goto exit;
	}

	obj = kzalloc(sizeof(struct listmp_sharedmemory_obj),
			GFP_KERNEL);
	if (obj == NULL) {
		/*! @retval NULL if Memory allocation failed for
		* internal object "Memory allocation failed for handle"
		* "of type struct listmp_sharedmemory_obj"); */
		status = -ENOMEM;
		goto exit;
	}

	handle->obj = (struct listmp_sharedmemory_obj *)obj ;
	handle->empty = &listmp_sharedmemory_empty;
	handle->get_head = &listmp_sharedmemory_get_head;
	handle->get_tail = &listmp_sharedmemory_get_tail;
	handle->put_head = &listmp_sharedmemory_put_head;
	handle->put_tail = &listmp_sharedmemory_put_tail;
	handle->insert = &listmp_sharedmemory_insert;
	handle->remove = &listmp_sharedmemory_remove;
	handle->next = &listmp_sharedmemory_next;
	handle->prev = &listmp_sharedmemory_prev;

	/* Update attrs */
	obj->attrs = (struct listmp_attrs *) params->shared_addr;
	/* Assign the memory with proper cache line padding */
	obj->listmp_elem = (void *) ((u32)obj->attrs + \
			LISTMP_SHAREDMEMORY_CACHESIZE);
	obj->index = sharedregion_get_index(params->shared_addr);

	if (create_flag == true) {
		obj->attrs->shared_addr_size = params->shared_addr_size;
		obj->attrs->version = LISTMP_SHAREDMEMORY_VERSION;
		obj->listmp_elem->next = obj->listmp_elem->prev =
			(struct listmp_elem *)
			(sharedregion_get_srptr((void *)obj->listmp_elem,
							obj->index));
	}

	/* Populate the params member */
	memcpy((void *)&obj->params, (void *)params,
			sizeof(listmp_sharedmemory_params));

	if (likely(listmp_sharedmemory_state.cfg.use_name_server
							== true)) {
		if (obj->params.name != NULL) {
			/* Copy the name */
			obj->params.name = kmalloc(strlen(params->name) + 1,
								GFP_KERNEL);

			if (obj->params.name == NULL) {
				/*! @retval NULL if Memory allocation failed for
				name */
				status = -ENOMEM;
			} else {
				strncpy(obj->params.name, params->name,
						strlen(params->name) + 1);
			}
		}
	}

	/* Update processor information */
	obj->owner = kmalloc(sizeof(struct listmp_proc_attrs),
							GFP_KERNEL);
	if (obj->owner == NULL) {
		printk(KERN_ERR "_listmp_sharedmemory_create: Memory "
			"allocation failed for processor information!\n");
		status = -ENOMEM;
	} else {
		/* Update owner and opener details */
		if (create_flag == true) {
			obj->owner->creator = true;
			obj->owner->open_count = 1;
			obj->owner->proc_id = multiproc_get_id(NULL);
			obj->top = handle;
			obj->attrs->status = \
				LISTMP_SHAREDMEMORY_CREATED;
		} else {
			obj->owner->creator = false;
			obj->owner->open_count = 0;
			obj->owner->proc_id = MULTIPROC_INVALIDID;
			obj->top = handle;
		}

		/* Put in the local list */
		key = mutex_lock_interruptible(
			listmp_sharedmemory_state.local_gate);
		INIT_LIST_HEAD(&obj->list_elem);
		list_add_tail((&obj->list_elem),
			&listmp_sharedmemory_state.obj_list);
		mutex_unlock(listmp_sharedmemory_state.local_gate);

		if (create_flag == true) {

			/* We will store a shared pointer in the
			 * NameServer
			 */
			shm_index = sharedregion_get_index(params->shared_addr);
			shared_shm_base = sharedregion_get_srptr(
						params->shared_addr, shm_index);
			/* Add list instance to name server */
			if (obj->params.name != NULL) {
				obj->ns_key = nameserver_add_uint32(
					listmp_sharedmemory_state.ns_handle,
					params->name,
					(u32) (shared_shm_base));
				if (obj->ns_key == NULL)
					status = -EFAULT;
			}
		}
	}

	if (status < 0) {
		/* Remove from  the local list */
		key = mutex_lock_interruptible(
					listmp_sharedmemory_state.local_gate);
		list_del(&obj->list_elem);
		mutex_unlock(listmp_sharedmemory_state.local_gate);

		if (likely(listmp_sharedmemory_state.cfg.use_name_server
								== true)) {
			if ((obj->params.name != NULL) && (status != -EFAULT))
				nameserver_remove_entry(
					listmp_sharedmemory_state.ns_handle,
					obj->ns_key);
		}
		if (obj->owner != NULL)
			kfree(obj->owner);

		if (obj->params.name != NULL)
			kfree(obj->params.name);

		if (obj != NULL) {
			kfree(obj);
			obj = NULL;
		}
		if (handle != NULL) {
			kfree(handle);
			handle = NULL;
		}
	}

exit:
	if (status < 0) {
		printk(KERN_ERR "_listmp_sharedmemory_create failed! "
			"status = 0x%x\n", status);
	}
	return (listmp_sharedmemory_handle) handle;
}