aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gud/MobiCoreKernelApi/session.c
blob: 3c21ac8d33256d69161a65407e2fdc56d416bf8f (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
/** @addtogroup MCD_IMPL_LIB
 * @{
 * @file
 * <!-- Copyright Giesecke & Devrient GmbH 2009 - 2011 -->
 */
#include <linux/types.h>
#include <linux/slab.h>
#include "mcKernelApi.h"
#include "public/MobiCoreDriverApi.h"

#include "session.h"

//------------------------------------------------------------------------------
bulkBufferDescriptor_t* bulkBufferDescriptor_create(
    addr_t    virtAddr,
    uint32_t  len,
    uint32_t  handle,
    addr_t    physAddrWsmL2
) {
	bulkBufferDescriptor_t *desc = kzalloc(sizeof(bulkBufferDescriptor_t),
											GFP_KERNEL);
	desc->virtAddr = virtAddr;
	desc->len = len;
	desc->handle = handle;
	desc->physAddrWsmL2 = physAddrWsmL2;
	return desc;
}

//------------------------------------------------------------------------------
session_t *session_create(
    uint32_t     sessionId,
    void       *pInstance,
    connection_t *connection
) {
	session_t *session = kzalloc(sizeof(session_t), GFP_KERNEL);
	session->sessionId = sessionId;
	session->pInstance = pInstance;
	session->notificationConnection = connection;

	session->sessionInfo.lastErr = SESSION_ERR_NO;
	session->sessionInfo.state = SESSION_STATE_INITIAL;

	INIT_LIST_HEAD(&(session->bulkBufferDescriptors));
	return session;
}


//------------------------------------------------------------------------------
void session_cleanup(
    session_t *session
) {
	bulkBufferDescriptor_t  *pBlkBufDescr;
	struct list_head *pos, *q;

	// Unmap still mapped buffers
	list_for_each_safe(pos, q, &session->bulkBufferDescriptors) {
		pBlkBufDescr=list_entry(pos, bulkBufferDescriptor_t, list);

		MCDRV_DBG_VERBOSE("Physical Address of L2 Table = 0x%X, handle= %d",
				(unsigned int)pBlkBufDescr->physAddrWsmL2,
				pBlkBufDescr->handle);

		// ignore any error, as we cannot do anything in this case.
		int ret = mobicore_unmap_vmem(session->pInstance,
										pBlkBufDescr->handle);
		if (0 != ret)
		{
			MCDRV_DBG_ERROR("mobicore_unmap_vmem failed: %d",ret);
		}

		list_del(pos);
		kfree(pBlkBufDescr);
	}

	// Finally delete notification connection
	connection_cleanup(session->notificationConnection);
	kfree(session);
}


//------------------------------------------------------------------------------
void session_setErrorInfo(
    session_t *session,
    int32_t   err
) {
	session->sessionInfo.lastErr = err;
}


//------------------------------------------------------------------------------
int32_t session_getLastErr(
    session_t *session
) {
	return session->sessionInfo.lastErr;
}


//------------------------------------------------------------------------------
bulkBufferDescriptor_t* session_addBulkBuf(
    session_t	*session,
    addr_t		buf,
    uint32_t	len
) {
	bulkBufferDescriptor_t* blkBufDescr = NULL;
	bulkBufferDescriptor_t  *tmp;
	struct list_head *pos;

	// Search bulk buffer descriptors for existing vAddr
	// At the moment a virtual address can only be added one time
	list_for_each(pos, &session->bulkBufferDescriptors) {
		tmp=list_entry(pos, bulkBufferDescriptor_t, list);
		if (tmp->virtAddr == buf)
		{
			return NULL;
		}
	}

	do
	{
		// Prepare the interface structure for memory registration in
		// Kernel Module
		addr_t    pPhysWsmL2;
		uint32_t  handle;

		int ret = mobicore_map_vmem(session->pInstance,
									buf,
									len,
									&handle,
									&pPhysWsmL2);

		if (0 != ret) {
			MCDRV_DBG_ERROR("mobicore_map_vmem failed, ret=%d",ret);
			break;
		}

		MCDRV_DBG_VERBOSE("Physical Address of L2 Table = 0x%X, handle=%d",
				(unsigned int)pPhysWsmL2,
				handle);

		// Create new descriptor
		blkBufDescr = bulkBufferDescriptor_create(
							buf,
							len,
							handle,
							pPhysWsmL2);

		// Add to vector of descriptors
		list_add_tail(&(blkBufDescr->list), &(session->bulkBufferDescriptors));
	} while (0);

	return blkBufDescr;
}


//------------------------------------------------------------------------------
bool session_removeBulkBuf(
    session_t *session,
    addr_t    virtAddr
) {
	bool ret = true;
	bulkBufferDescriptor_t  *pBlkBufDescr = NULL;
	bulkBufferDescriptor_t  *tmp;
	struct list_head *pos, *q;

	MCDRV_DBG_VERBOSE("Virtual Address = 0x%X", (unsigned int) virtAddr);

	// Search and remove bulk buffer descriptor
	list_for_each_safe(pos, q, &session->bulkBufferDescriptors) {
		tmp=list_entry(pos, bulkBufferDescriptor_t, list);
		if (tmp->virtAddr == virtAddr)
		{
			pBlkBufDescr = tmp;
			list_del(pos);
			break;
		}
	}

	if (NULL == pBlkBufDescr)
	{
		MCDRV_DBG_ERROR("Virtual Address not found");
		ret = false;
	}
	else
	{
		MCDRV_DBG_VERBOSE("WsmL2 phys=0x%X, handle=%d",
				(unsigned int)pBlkBufDescr->physAddrWsmL2, pBlkBufDescr->handle);

		// ignore any error, as we cannot do anything
		int ret = mobicore_unmap_vmem(session->pInstance,
										pBlkBufDescr->handle);
		if (0 != ret)
		{
			MCDRV_DBG_ERROR("mobicore_unmap_vmem failed: %d",ret);
		}

		kfree(pBlkBufDescr);
	}

	return ret;
}

/** @} */