aboutsummaryrefslogtreecommitdiff
path: root/drivers/gud/MobiCoreKernelApi/clientlib.c
blob: 57133805eeb8755c924289f9e0abd5babe78ce89 (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
/*
 * Copyright (c) 2013-2014 TRUSTONIC LIMITED
 * All Rights Reserved.
 *
 * This program 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 program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */
#include <linux/module.h>
#include <linux/init.h>

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/netlink.h>
#include <net/sock.h>
#include <net/net_namespace.h>
#include <linux/list.h>

#include "public/mobicore_driver_api.h"
#include "public/mobicore_driver_cmd.h"
#include "include/mcinq.h"
#include "device.h"
#include "session.h"

/* device list */
LIST_HEAD(devices);
atomic_t device_usage = ATOMIC_INIT(0);

static struct mcore_device_t *resolve_device_id(uint32_t device_id)
{
	struct mcore_device_t *tmp;
	struct list_head *pos;

	/* Get mcore_device_t for device_id */
	list_for_each(pos, &devices) {
		tmp = list_entry(pos, struct mcore_device_t, list);
		if (tmp->device_id == device_id)
			return tmp;
	}
	return NULL;
}

static void add_device(struct mcore_device_t *device)
{
	list_add_tail(&(device->list), &devices);
}

static bool remove_device(uint32_t device_id)
{
	struct mcore_device_t *tmp;
	struct list_head *pos, *q;

	list_for_each_safe(pos, q, &devices) {
		tmp = list_entry(pos, struct mcore_device_t, list);
		if (tmp->device_id == device_id) {
			list_del(pos);
			mcore_device_cleanup(tmp);
			return true;
		}
	}
	return false;
}

enum mc_result mc_open_device(uint32_t device_id)
{
	enum mc_result mc_result = MC_DRV_OK;
	struct connection *dev_con = NULL;

	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		struct mcore_device_t *device = resolve_device_id(device_id);
		if (device != NULL) {
			MCDRV_DBG(mc_kapi,
				  "Device %d already opened\n", device_id);
			atomic_inc(&device_usage);
			mc_result = MC_DRV_OK;
			break;
		}

		/* Open new connection to device */
		dev_con = connection_new();
		if (dev_con == NULL) {
			mc_result = MC_DRV_ERR_NO_FREE_MEMORY;
			break;
		}

		if (!connection_connect(dev_con, MC_DAEMON_PID)) {
			MCDRV_DBG_ERROR(
				mc_kapi,
				"Could not setup netlink connection to PID %u",
				MC_DAEMON_PID);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		/* Forward device open to the daemon and read result */
		struct mc_drv_cmd_open_device_t mc_drv_cmd_open_device = {
			{
				MC_DRV_CMD_OPEN_DEVICE
			},
			{
				device_id
			}
		};

		int len = connection_write_data(
				dev_con,
				&mc_drv_cmd_open_device,
				sizeof(struct mc_drv_cmd_open_device_t));
		if (len < 0) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_OPEN_DEVICE writeCmd failed %d",
					len);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		struct mc_drv_response_header_t rsp_header;
		memset(&rsp_header, 0, sizeof(rsp_header));
		len = connection_read_datablock(
					dev_con,
					&rsp_header,
					sizeof(rsp_header));
		if (len != sizeof(rsp_header)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_OPEN_DEVICE readRsp failed %d",
					len);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}
		if (rsp_header.response_id != MC_DRV_RSP_OK) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_OPEN_DEVICE failed, respId=%d",
					rsp_header.response_id);
			switch (rsp_header.response_id) {
			case MC_DRV_RSP_PAYLOAD_LENGTH_ERROR:
				mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
				break;
			case MC_DRV_INVALID_DEVICE_NAME:
				mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
				break;
			case MC_DRV_RSP_DEVICE_ALREADY_OPENED:
			default:
				mc_result = MC_DRV_ERR_INVALID_OPERATION;
				break;
			}
			break;
		}

		/* there is no payload to read */

		device = mcore_device_create(device_id, dev_con);
		if (device == NULL) {
			mc_result = MC_DRV_ERR_NO_FREE_MEMORY;
			break;
		}
		if (!mcore_device_open(device, MC_DRV_MOD_DEVNODE_FULLPATH)) {
			mcore_device_cleanup(device);
			MCDRV_DBG_ERROR(mc_kapi,
					"could not open device file: %s",
					MC_DRV_MOD_DEVNODE_FULLPATH);
			mc_result = MC_DRV_ERR_INVALID_DEVICE_FILE;
			break;
		}

		add_device(device);
		atomic_inc(&device_usage);

	} while (false);

	if (mc_result != MC_DRV_OK)
		connection_cleanup(dev_con);

	return mc_result;
}
EXPORT_SYMBOL(mc_open_device);

enum mc_result mc_close_device(uint32_t device_id)
{
	enum mc_result mc_result = MC_DRV_OK;

	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		struct mcore_device_t *device = resolve_device_id(device_id);
		if (device == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Device not found");
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}
		/* Check if it's not used by other modules */
		if (!atomic_dec_and_test(&device_usage)) {
			mc_result = MC_DRV_OK;
			break;
		}

		struct connection *dev_con = device->connection;

		/* Return if not all sessions have been closed */
		if (mcore_device_has_sessions(device)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"cannot close with sessions pending");
			mc_result = MC_DRV_ERR_SESSION_PENDING;
			break;
		}

		struct mc_drv_cmd_close_device_t mc_drv_cmd_close_device = {
			{
				MC_DRV_CMD_CLOSE_DEVICE
			}
		};
		int len = connection_write_data(
				dev_con,
				&mc_drv_cmd_close_device,
				sizeof(struct mc_drv_cmd_close_device_t));
		/* ignore error, but log details */
		if (len < 0) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_CLOSE_DEVICE writeCmd failed %d",
					len);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
		}

		struct mc_drv_response_header_t rsp_header;
		memset(&rsp_header, 0, sizeof(rsp_header));
		len = connection_read_datablock(
					dev_con,
					&rsp_header,
					sizeof(rsp_header));
		if (len != sizeof(rsp_header)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_CLOSE_DEVICE readResp failed %d",
					len);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		if (rsp_header.response_id != MC_DRV_RSP_OK) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_CLOSE_DEVICE failed, respId=%d",
					rsp_header.response_id);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		remove_device(device_id);

	} while (false);

	return mc_result;
}
EXPORT_SYMBOL(mc_close_device);

enum mc_result mc_open_session(struct mc_session_handle *session,
			       const struct mc_uuid_t *uuid,
			       uint8_t *tci, uint32_t tci_len)
{
	enum mc_result mc_result = MC_DRV_OK;

	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		if (session == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Session is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}
		if (uuid == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "UUID is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}
		if (tci == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "TCI is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}
		if (tci_len > MC_MAX_TCI_LEN) {
			MCDRV_DBG_ERROR(mc_kapi, "TCI length is longer than %d",
					MC_MAX_TCI_LEN);
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		/* Get the device associated with the given session */
		struct mcore_device_t *device =
				resolve_device_id(session->device_id);
		if (device == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Device not found");
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}
		struct connection *dev_con = device->connection;

		/* Get the wsm of the given TCI */
		struct wsm *wsm =
			mcore_device_find_contiguous_wsm(device, tci);
		if (wsm == NULL) {
			MCDRV_DBG_ERROR(mc_kapi,
					"Could not resolve TCI address ");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		if (wsm->len < tci_len) {
			MCDRV_DBG_ERROR(mc_kapi,
					"length is more than allocated TCI");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		/* Prepare open session command */
		struct mc_drv_cmd_open_session_t cmd_open_session = {
			{
				MC_DRV_CMD_OPEN_SESSION
			},
			{
				session->device_id,
				*uuid,
				(uint32_t)((uintptr_t)(wsm->virt_addr) & 0xFFF),
				wsm->handle,
				tci_len
			}
		};

		/* Transmit command data */
		int len = connection_write_data(dev_con,
						&cmd_open_session,
						sizeof(cmd_open_session));
		if (len != sizeof(cmd_open_session)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_OPEN_SESSION writeData failed %d",
					len);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		/* Read command response */

		/* read header first */
		struct mc_drv_response_header_t rsp_header;
		memset(&rsp_header, 0, sizeof(rsp_header));
		len = connection_read_datablock(dev_con,
						&rsp_header,
						sizeof(rsp_header));
		if (len != sizeof(rsp_header)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_OPEN_SESSION readResp failed %d",
					len);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		if (rsp_header.response_id != MC_DRV_RSP_OK) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_OPEN_SESSION failed, respId=%d",
					rsp_header.response_id);
			switch (rsp_header.response_id) {
			case MC_DRV_RSP_TRUSTLET_NOT_FOUND:
				mc_result = MC_DRV_ERR_INVALID_DEVICE_FILE;
				break;
			case MC_DRV_RSP_PAYLOAD_LENGTH_ERROR:
			case MC_DRV_RSP_DEVICE_NOT_OPENED:
			case MC_DRV_RSP_FAILED:
			default:
				mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
				break;
			}
			break;
		}

		/* read payload */
		struct mc_drv_rsp_open_session_payload_t
					rsp_open_session_payload;
		memset(&rsp_open_session_payload, 0,
		       sizeof(rsp_open_session_payload));
		len = connection_read_datablock(
					dev_con,
					&rsp_open_session_payload,
					sizeof(rsp_open_session_payload));
		if (len != sizeof(rsp_open_session_payload)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_OPEN_SESSION readPayload fail %d",
					len);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		/* Register session with handle */
		session->session_id = rsp_open_session_payload.session_id;

		/* Set up second channel for notifications */
		struct connection *session_connection = connection_new();
		if (session_connection == NULL) {
			mc_result = MC_DRV_ERR_NO_FREE_MEMORY;
			break;
		}

		if (!connection_connect(session_connection, MC_DAEMON_PID)) {
			MCDRV_DBG_ERROR(
				mc_kapi,
				"Could not setup netlink connection to PID %u",
				MC_DAEMON_PID);
			connection_cleanup(session_connection);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		/* Write command to use channel for notifications */
		struct mc_drv_cmd_nqconnect_t cmd_nqconnect = {
			{
				MC_DRV_CMD_NQ_CONNECT
			},
			{
				session->device_id,
				session->session_id,
				rsp_open_session_payload.device_session_id,
				rsp_open_session_payload.session_magic
			}
		};
		connection_write_data(session_connection,
				      &cmd_nqconnect,
				      sizeof(cmd_nqconnect));

		/* Read command response, header first */
		len = connection_read_datablock(session_connection,
						&rsp_header,
						sizeof(rsp_header));
		if (len != sizeof(rsp_header)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_NQ_CONNECT readRsp failed %d",
					len);
			connection_cleanup(session_connection);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		if (rsp_header.response_id != MC_DRV_RSP_OK) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_NQ_CONNECT failed, respId=%d",
					rsp_header.response_id);
			connection_cleanup(session_connection);
			mc_result = MC_DRV_ERR_NQ_FAILED;
			break;
		}

		/* there is no payload. */

		/* Session established, new session object must be created */
		if (!mcore_device_create_new_session(device,
						     session->session_id,
						     session_connection)) {
			connection_cleanup(session_connection);
			mc_result = MC_DRV_ERR_NO_FREE_MEMORY;
			break;
		}

	} while (false);

	return mc_result;
}
EXPORT_SYMBOL(mc_open_session);

enum mc_result mc_close_session(struct mc_session_handle *session)
{
	enum mc_result mc_result = MC_DRV_OK;

	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		if (session == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Session is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		struct mcore_device_t *device =
					resolve_device_id(session->device_id);
		if (device == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Device not found");
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}
		struct connection *dev_con = device->connection;

		struct session *nq_session =
			mcore_device_resolve_session_id(device,
							session->session_id);

		if (nq_session == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Session not found");
			mc_result = MC_DRV_ERR_UNKNOWN_SESSION;
			break;
		}

		/* Write close session command */
		struct mc_drv_cmd_close_session_t cmd_close_session = {
			{
				MC_DRV_CMD_CLOSE_SESSION
			},
			{
				session->session_id,
			}
		};
		connection_write_data(dev_con,
				      &cmd_close_session,
				      sizeof(cmd_close_session));

		/* Read command response */
		struct mc_drv_response_header_t rsp_header;
		memset(&rsp_header, 0, sizeof(rsp_header));
		int len = connection_read_datablock(dev_con,
						    &rsp_header,
						    sizeof(rsp_header));
		if (len != sizeof(rsp_header)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_CLOSE_SESSION readRsp failed %d",
					len);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		if (rsp_header.response_id != MC_DRV_RSP_OK) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_CLOSE_SESSION failed, respId=%d",
					rsp_header.response_id);
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}

		mcore_device_remove_session(device, session->session_id);
		mc_result = MC_DRV_OK;

	} while (false);

	return mc_result;
}
EXPORT_SYMBOL(mc_close_session);

enum mc_result mc_notify(struct mc_session_handle *session)
{
	enum mc_result mc_result = MC_DRV_OK;

	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		if (session == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Session is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		struct mcore_device_t *device =
					resolve_device_id(session->device_id);
		if (device == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Device not found");
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}
		struct connection *dev_con = device->connection;

		struct session  *nqsession =
		 mcore_device_resolve_session_id(device, session->session_id);
		if (nqsession == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Session not found");
			mc_result = MC_DRV_ERR_UNKNOWN_SESSION;
			break;
		}

		struct mc_drv_cmd_notify_t cmd_notify = {
			{
				MC_DRV_CMD_NOTIFY
			},
			{
				session->session_id
			}
		};

		connection_write_data(dev_con,
				      &cmd_notify,
				      sizeof(cmd_notify));

		/* Daemon will not return a response */

	} while (false);

	return mc_result;
}
EXPORT_SYMBOL(mc_notify);

enum mc_result mc_wait_notification(struct mc_session_handle *session,
				    int32_t timeout)
{
	enum mc_result mc_result = MC_DRV_OK;

	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		if (session == NULL) {
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		struct mcore_device_t *device =
					resolve_device_id(session->device_id);
		if (device == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Device not found");
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}

		struct session *nq_session =
			mcore_device_resolve_session_id(device,
							session->session_id);
		if (nq_session == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Session not found");
			mc_result = MC_DRV_ERR_UNKNOWN_SESSION;
			break;
		}

		struct connection *nqconnection =
					nq_session->notification_connection;
		uint32_t count = 0;

		/* Read notification queue till it's empty */
		for (;;) {
			struct notification notification;
			memset(&notification, 0, sizeof(notification));
			ssize_t num_read =
				connection_read_data(nqconnection,
						     &notification,
						     sizeof(notification),
						     timeout);
			/*
			 * Exit on timeout in first run. Later runs have
			 * timeout set to 0.
			 * -2 means, there is no more data.
			 */
			if (count == 0 && num_read == -2) {
				MCDRV_DBG_ERROR(mc_kapi, "read timeout");
				mc_result = MC_DRV_ERR_TIMEOUT;
				break;
			}
			/*
			 * After first notification the queue will be
			 * drained, Thus we set no timeout for the
			 * following reads
			 */
			timeout = 0;

			if (num_read != sizeof(struct notification)) {
				if (count == 0) {
					/* failure in first read, notify it */
					mc_result = MC_DRV_ERR_NOTIFICATION;
					MCDRV_DBG_ERROR(
					mc_kapi,
					"read notification failed, "
					"%i bytes received", (int)num_read);
					break;
				} else {
					/*
					 * Read of the n-th notification
					 * failed/timeout. We don't tell the
					 * caller, as we got valid notifications
					 * before.
					 */
					mc_result = MC_DRV_OK;
					break;
				}
			}

			count++;
			MCDRV_DBG_VERBOSE(mc_kapi,
					  "count=%d, SessionID=%d, Payload=%d",
					  count,
					  notification.session_id,
					  notification.payload);

			if (notification.payload != 0) {
				/* Session end point died -> store exit code */
				session_set_error_info(nq_session,
						       notification.payload);

				mc_result = MC_DRV_INFO_NOTIFICATION;
				break;
			}
		} /* for(;;) */

	} while (false);

	return mc_result;
}
EXPORT_SYMBOL(mc_wait_notification);

enum mc_result mc_malloc_wsm(uint32_t device_id, uint32_t align, uint32_t len,
			     uint8_t **wsm, uint32_t wsm_flags)
{
	enum mc_result mc_result = MC_DRV_ERR_UNKNOWN;

	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		struct mcore_device_t *device = resolve_device_id(device_id);
		if (device == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Device not found");
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}
		if (wsm == NULL) {
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		struct wsm *wsm_stack =
			mcore_device_allocate_contiguous_wsm(device, len);
		if (wsm_stack == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Allocation of WSM failed");
			mc_result = MC_DRV_ERR_NO_FREE_MEMORY;
			break;
		}

		*wsm = (uint8_t *)wsm_stack->virt_addr;
		mc_result = MC_DRV_OK;

	} while (false);

	return mc_result;
}
EXPORT_SYMBOL(mc_malloc_wsm);

enum mc_result mc_free_wsm(uint32_t device_id, uint8_t *wsm)
{
	enum mc_result mc_result = MC_DRV_ERR_UNKNOWN;
	struct mcore_device_t *device;


	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		/* Get the device associated wit the given session */
		device = resolve_device_id(device_id);
		if (device == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Device not found");
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}

		/* find WSM object */
		struct wsm *wsm_stack =
			mcore_device_find_contiguous_wsm(device, wsm);
		if (wsm_stack == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "unknown address");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		/* Free the given virtual address */
		if (!mcore_device_free_contiguous_wsm(device, wsm_stack)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"Free of virtual address failed");
			mc_result = MC_DRV_ERR_FREE_MEMORY_FAILED;
			break;
		}
		mc_result = MC_DRV_OK;

	} while (false);

	return mc_result;
}
EXPORT_SYMBOL(mc_free_wsm);

enum mc_result mc_map(struct mc_session_handle *session_handle, void *buf,
		      uint32_t buf_len, struct mc_bulk_map *map_info)
{
	enum mc_result mc_result = MC_DRV_ERR_UNKNOWN;

	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		if (session_handle == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "session_handle is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}
		if (map_info == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "map_info is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}
		if (buf == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "buf is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		/* Determine device the session belongs to */
		struct mcore_device_t *device =
				resolve_device_id(session_handle->device_id);

		if (device == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Device not found");
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}
		struct connection *dev_con = device->connection;

		/* Get session */
		uint32_t session_id = session_handle->session_id;
		struct session *session =
				mcore_device_resolve_session_id(device,
								session_id);
		if (session == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Session not found");
			mc_result = MC_DRV_ERR_UNKNOWN_SESSION;
			break;
		}

		/*
		 * Register mapped bulk buffer to Kernel Module and keep mapped
		 * bulk buffer in mind
		 */
		struct bulk_buffer_descriptor *bulk_buf =
				session_add_bulk_buf(session, buf, buf_len);
		if (bulk_buf == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Error mapping bulk buffer");
			mc_result = MC_DRV_ERR_BULK_MAPPING;
			break;
		}

		/* Prepare map command */
		uintptr_t offset = (uintptr_t)(bulk_buf->virt_addr) & 0xFFF;
		struct mc_drv_cmd_map_bulk_mem_t mc_drv_cmd_map_bulk_mem = {
			{
				MC_DRV_CMD_MAP_BULK_BUF
			},
			{
				session->session_id,
				bulk_buf->handle,
				0,
				(uint32_t)offset,
				bulk_buf->len
			}
		};

		/* Transmit map command to MobiCore device */
		connection_write_data(dev_con,
				      &mc_drv_cmd_map_bulk_mem,
				      sizeof(mc_drv_cmd_map_bulk_mem));

		/* Read command response */
		struct mc_drv_response_header_t rsp_header;
		memset(&rsp_header, 0, sizeof(rsp_header));
		int len = connection_read_datablock(dev_con,
							&rsp_header,
							sizeof(rsp_header));
		if (len != sizeof(rsp_header)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_MAP_BULK_BUF readRsp failed %d",
					len);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		if (rsp_header.response_id != MC_DRV_RSP_OK) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_MAP_BULK_BUF failed, respId=%d",
					rsp_header.response_id);

			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;

			/*
			 * Unregister mapped bulk buffer from Kernel Module and
			 * remove mapped bulk buffer from session maintenance
			 */
			if (!session_remove_bulk_buf(session, buf)) {
				/* Removing of bulk buffer not possible */
				MCDRV_DBG_ERROR(mc_kapi,
						"Unreg of bulk memory failed");
			}
			break;
		}

		struct mc_drv_rsp_map_bulk_mem_payload_t
						rsp_map_bulk_mem_payload;
		memset(&rsp_map_bulk_mem_payload, 0,
		       sizeof(rsp_map_bulk_mem_payload));
		connection_read_datablock(dev_con,
					  &rsp_map_bulk_mem_payload,
					  sizeof(rsp_map_bulk_mem_payload));

		/* Set mapping info for Trustlet */
		map_info->secure_virt_addr =
			rsp_map_bulk_mem_payload.secure_virtual_adr;
		map_info->secure_virt_len = buf_len;
		mc_result = MC_DRV_OK;

	} while (false);

	return mc_result;
}
EXPORT_SYMBOL(mc_map);

enum mc_result mc_unmap(struct mc_session_handle *session_handle, void *buf,
			struct mc_bulk_map *map_info)
{
	enum mc_result mc_result = MC_DRV_ERR_UNKNOWN;

	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		if (session_handle == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "session_handle is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}
		if (map_info == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "map_info is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}
		if (buf == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "buf is null");
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		/* Determine device the session belongs to */
		struct mcore_device_t  *device =
			resolve_device_id(session_handle->device_id);
		if (device == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Device not found");
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}
		struct connection *dev_con = device->connection;

		/* Get session */
		uint32_t session_id = session_handle->session_id;
		struct session  *session =
			mcore_device_resolve_session_id(device,
							session_id);
		if (session == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Session not found");
			mc_result = MC_DRV_ERR_UNKNOWN_SESSION;
			break;
		}

		uint32_t handle = session_find_bulk_buf(session, buf);
		if (handle == 0) {
			MCDRV_DBG_ERROR(mc_kapi, "Buffer not found");
			mc_result = MC_DRV_ERR_BULK_UNMAPPING;
			break;
		}


		/* Prepare unmap command */
		struct mc_drv_cmd_unmap_bulk_mem_t cmd_unmap_bulk_mem = {
				{
					MC_DRV_CMD_UNMAP_BULK_BUF
				},
				{
					session->session_id,
					handle,
					map_info->secure_virt_addr,
					map_info->secure_virt_len
				}
			};

		connection_write_data(dev_con,
				      &cmd_unmap_bulk_mem,
				      sizeof(cmd_unmap_bulk_mem));

		/* Read command response */
		struct mc_drv_response_header_t rsp_header;
		memset(&rsp_header, 0, sizeof(rsp_header));
		int len = connection_read_datablock(dev_con,
						    &rsp_header,
						    sizeof(rsp_header));
		if (len != sizeof(rsp_header)) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_UNMAP_BULK_BUF readRsp failed %d",
					len);
			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		if (rsp_header.response_id != MC_DRV_RSP_OK) {
			MCDRV_DBG_ERROR(mc_kapi,
					"CMD_UNMAP_BULK_BUF failed, respId=%d",
					rsp_header.response_id);

			mc_result = MC_DRV_ERR_DAEMON_UNREACHABLE;
			break;
		}

		/*struct mc_drv_rsp_unmap_bulk_mem_payload_t
						rsp_unmap_bulk_mem_payload;
		connection_read_datablock(dev_con,
					  &rsp_unmap_bulk_mem_payload,
					  sizeof(rsp_unmap_bulk_mem_payload));*/

		/*
		 * Unregister mapped bulk buffer from Kernel Module and
		 * remove mapped bulk buffer from session maintenance
		 */
		if (!session_remove_bulk_buf(session, buf)) {
			/* Removing of bulk buffer not possible */
			MCDRV_DBG_ERROR(mc_kapi,
					"Unregistering of bulk memory failed");
			mc_result = MC_DRV_ERR_BULK_UNMAPPING;
			break;
		}

		mc_result = MC_DRV_OK;

	} while (false);

	return mc_result;
}
EXPORT_SYMBOL(mc_unmap);

enum mc_result mc_get_session_error_code(struct mc_session_handle *session,
					 int32_t *last_error)
{
	enum mc_result mc_result = MC_DRV_OK;

	MCDRV_DBG_VERBOSE(mc_kapi, "===%s()===", __func__);

	do {
		if (session == NULL || last_error == NULL) {
			mc_result = MC_DRV_ERR_INVALID_PARAMETER;
			break;
		}

		/* Get device */
		struct mcore_device_t *device =
				resolve_device_id(session->device_id);
		if (device == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Device not found");
			mc_result = MC_DRV_ERR_UNKNOWN_DEVICE;
			break;
		}

		/* Get session */
		uint32_t session_id = session->session_id;
		struct session *nqsession =
				mcore_device_resolve_session_id(device,
								session_id);
		if (nqsession == NULL) {
			MCDRV_DBG_ERROR(mc_kapi, "Session not found");
			mc_result = MC_DRV_ERR_UNKNOWN_SESSION;
			break;
		}

		*last_error = session_get_last_err(nqsession);

	} while (false);

	return mc_result;
}
EXPORT_SYMBOL(mc_get_session_error_code);