aboutsummaryrefslogtreecommitdiff
path: root/drivers/net/danipc/ipc.c
blob: abf6c6301893f2d62c161e282446f7f46ad79047 (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
/*
	All files except if stated otherwise in the begining of the file
	are under the ISC license:
	----------------------------------------------------------------------

	Copyright (c) 2010-2012 Design Art Networks Ltd.

	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.
*/


#include <linux/string.h>

#include "ipc_reg.h"
#include "ipc_api.h"

#include "danipc_lowlevel.h"


/* -----------------------------------------------------------
 * MACRO (define) section
 * -----------------------------------------------------------
 */
/* max. number of local agents per one Node */
#define MAX_LOCAL_ID     (MAX_LOCAL_AGENT-1)

static uint8_t	ipc_req_sn;	/* Maintain node related sequence number */

uint8_t		ipc_own_node;


/* ===========================================================================
 * ipc_appl_init
 * ===========================================================================
 * Description:	This function initializes software/HW during startup
 *
 * Parameters:		def_trns_funcs	- pointer to default transport layer
 *					  function vector
 *
 * Returns: n/a
 *
 */
static inline void ipc_appl_init(struct ipc_trns_func const *def_trns_funcs)
{
	ipc_own_node = ipc_get_own_node();
	ipc_agent_table_clean();

	ipc_trns_fifo_buf_init(ipc_own_node);

	/* Initialize IPC routing table (of CPU#) */
	ipc_route_table_init(def_trns_funcs);
}


unsigned ipc_init(void)
{
	ipc_appl_init(&ipc_fifo_utils);
	return 0;
}



/* ===========================================================================
 * ipc_buf_alloc
 * ===========================================================================
 * Description:	buffer allocation API, should be called before building
 *		new message
 *
 * Parameters:		dest_aid	- Message destination AgentId
 *			prio		- Transport priority level
 *
 *
 * Returns: Pointer to a 128 Byte buffer
 *
*/
char *ipc_buf_alloc(uint8_t dest_aid, enum ipc_trns_prio prio)
{
	char			*ptr = NULL;
	struct ipc_trns_func const *trns_funcs;
	ipc_trns_alloc_t	alloc_func;
	const uint8_t		cpuid = ipc_get_node(dest_aid);

	/* Allocate buffer of 128 Bytes using the allocation function */
	/* associated with the given destination agentId */
	trns_funcs = (void *)get_trns_funcs(cpuid);
	if (likely(trns_funcs)) {
		alloc_func = trns_funcs->trns_alloc;
		if (likely(alloc_func)) {
			ptr = alloc_func(cpuid, prio);

			/* Clear the 'Next buffer' field. */
			if (likely(ptr))
				((struct ipc_buf_hdr *)ptr)->next = 0;
		}
	}

	return ptr;
}

/* ===========================================================================
 * ipc_buf_free
 * ===========================================================================
 * Description:  Free the buffer, could be called on IPC message receiving node
 *		or on sending node when need to free previously allocated
 *		buffers
 *
 * Parameters:		buf_first	- Pointer to first message buffer
 *			prio		- Transport priority level
 *
 *
 * Returns: Result code
 *
 */
int32_t ipc_buf_free(char *buf_first, enum ipc_trns_prio prio)
{
	struct ipc_buf_hdr		*cur_buf;
	struct ipc_buf_hdr		*next_buf;
	struct ipc_trns_func const	*trns_funcs;
	ipc_trns_free_t			free_func;
	uint8_t				dest_aid;
	uint8_t				cpuid;
	int32_t				res = IPC_GENERIC_ERROR;

	if (likely(buf_first)) {
		dest_aid  = (((struct ipc_first_buf *)buf_first)->
							msg_hdr).dest_aid;
		cur_buf = (struct ipc_buf_hdr *)buf_first;
		cpuid = ipc_get_node(dest_aid);
		trns_funcs = get_trns_funcs(cpuid);
		if (likely(trns_funcs)) {
			free_func = trns_funcs->trns_free;
			if (likely(free_func)) {
				/* Now loop all allocated buffers and free them.
				 * Last buffer is either a single (type = 0)
				 * or the buffer marked as the last (type = 2)
				 * all other buffers have their LSB set
				 * (type = 1 or 3).
				 */
				do {
					next_buf = ((struct ipc_msg_hdr *)
					 IPC_NEXT_PTR_PART(cur_buf))->next;
					free_func(IPC_NEXT_PTR_PART(cur_buf),
							cpuid, prio);
					cur_buf = next_buf;
				} while ((uint32_t)cur_buf & IPC_BUF_TYPE_MTC);
				res = IPC_SUCCESS;
			}
		}
	}
	return res;
}

/* ===========================================================================
 * ipc_buf_link
 * ===========================================================================
 * Description:	Link two buffers, should be called when message does not fit
 *		the single buffer
 *
 * Parameters:		buf_prev	- Pointer to a message buffer
 *			buf_next	- Pointer to the next message buffer
 *					  (to be linked to)
 *
 * Returns: Result code
 *
 */
static int32_t ipc_buf_link(char *buf_prev, char *buf_next)
{
	if (buf_prev == NULL || buf_next == NULL)
		return IPC_GENERIC_ERROR;

	/* Set the next buffer pointer in place */
	*(uint32_t *)buf_prev |= (uint32_t)buf_next & ~IPC_BUF_TYPE_BITS;
	/* Set the LSB of the prev buffer to signal there are more to come */
	*(uint32_t *)buf_prev |= IPC_BUF_TYPE_MTC;
	/* Mark the next buffer as the last one */
	*(uint32_t *)buf_next |= IPC_BUF_TYPE_END;
	return IPC_SUCCESS;
}

/* ===========================================================================
 * ipc_msg_set_len
 * ===========================================================================
 * Description:	sets message length, first buffer of the message
 *		should be provided
 *
 * Parameters:		buf_first	- Pointer to the first message buffer
 *			len		- Message length (bytes)
 *
 *
 * Returns: Result code
 *
 */
static int32_t ipc_msg_set_len(char *buf_first, size_t len)
{
	if (buf_first == NULL)
		return IPC_GENERIC_ERROR;
	(((struct ipc_first_buf *)buf_first)->msg_hdr).msg_len = len;
	return IPC_SUCCESS;
}

/* ===========================================================================
 * ipc_msg_set_type
 * ===========================================================================
 * Description:  sets message type, first buffer of the message
 *		should be provided
 *
 * Parameters:		buf_first	- Pointer to the first message buffer
 *			type		- Message type
 *
 *
 * Returns: Result code
 *
 */
static int32_t ipc_msg_set_type(char *buf_first, uint8_t type)
{
	if (buf_first == NULL)
		return IPC_GENERIC_ERROR;
	(((struct ipc_first_buf *)buf_first)->msg_hdr).msg_type = type;
	return IPC_SUCCESS;
}

/* ===========================================================================
 * ipc_msg_set_reply_ptr
 * ===========================================================================
 * Description:  sets message reply buffer pointer
 *
 * Parameters:		buf_first	- Pointer to the first message buffer
 *			buf_rep		- Pointer to the expected replay message
 *
 *
 * Returns: Result code
 *
 */
static int32_t ipc_msg_set_reply_ptr(
	char			*buf_first,
	char			*buf_rep
)
{
	if (buf_first == NULL)
		return IPC_GENERIC_ERROR;
	(((struct ipc_first_buf *)buf_first)->msg_hdr).reply = buf_rep;
	return IPC_SUCCESS;
}


/* ===========================================================================
 * ipc_msg_alloc
 * ===========================================================================
 * Description:  Allocate message buffer[s] and set the type and length.
 *		Copy message data into allocated buffers.
 *
 *
 * Parameters:		src_aid	- Message source AgentId
 *			dest_aid	- Message destination AgentId
 *			msg		- Pointer to message data
 *			msg_len		- Message length
 *			msg_type	- Message type
 *			prio		- Transport priority level
 *
 *
 * Returns: Pointer to the message first buffer
 *
 */
char *ipc_msg_alloc(
	uint8_t			src_aid,
	uint8_t			dest_aid,
	char			*msg,
	size_t			msg_len,
	uint8_t			msg_type,
	enum ipc_trns_prio	prio
)
{
	char			*first_buf = NULL;
	char			*prev_buf = NULL;
	char			*next_buf = NULL;
	unsigned		buf;
	unsigned		next_bufs_num = 0;
	size_t			tmp_size, reminder;
	char			*last_data;

	if ((msg_len > IPC_MAX_MESSAGE_SIZE) || (msg_len == 0))
		return NULL;

	/* Calculate number of 'next' buffers required */
	/* (i.e. buffers additional to the first buffer) */
	if (msg_len > IPC_FIRST_BUF_DATA_SIZE_MAX) {
		next_bufs_num = (msg_len - IPC_FIRST_BUF_DATA_SIZE_MAX) /
					IPC_NEXT_BUF_DATA_SIZE_MAX;
		if ((msg_len - IPC_FIRST_BUF_DATA_SIZE_MAX) %
					IPC_NEXT_BUF_DATA_SIZE_MAX)
			next_bufs_num++;
	}

	first_buf = prev_buf = ipc_buf_alloc(dest_aid, prio);
	for (buf = 0; buf < next_bufs_num; buf++) {
		if (prev_buf == NULL)
			break;
		next_buf = ipc_buf_alloc(dest_aid, prio);
		if (next_buf != NULL)
			ipc_buf_link(prev_buf, next_buf);
		prev_buf = next_buf;
	}

	/* If buffer allocation failed free the entire buffers */
	if ((prev_buf == NULL) && (first_buf != NULL)) {
		ipc_buf_free(first_buf, prio);
		first_buf = NULL;
	} else if (first_buf) {
		ipc_msg_set_type(first_buf, msg_type);
		ipc_msg_set_len(first_buf, msg_len);
		ipc_msg_set_reply_ptr(first_buf, NULL);
		((struct ipc_msg_hdr *)first_buf)->dest_aid = dest_aid;
		((struct ipc_msg_hdr *)first_buf)->src_aid = src_aid;
		((struct ipc_msg_hdr *)first_buf)->request_num = ipc_req_sn;
		ipc_req_sn++;

		if (msg) {
			last_data = msg + msg_len;

			/* Now copy the Data */
			reminder = msg_len;
			tmp_size = min_t(size_t, reminder,
					IPC_FIRST_BUF_DATA_SIZE_MAX);

			memcpy(((struct ipc_first_buf *)first_buf)->body,
					last_data - reminder, tmp_size);

			reminder -= tmp_size;
			prev_buf = first_buf;

			while (reminder > 0) {
				next_buf = IPC_NEXT_PTR_PART(
					((struct ipc_msg_hdr *)prev_buf)->next);
				tmp_size = min_t(size_t, reminder,
						IPC_NEXT_BUF_DATA_SIZE_MAX);

				memcpy(((struct ipc_next_buf *)next_buf)->body,
						last_data - reminder, tmp_size);

				reminder -= tmp_size;
				prev_buf = next_buf;
			}
		}
	}

	return first_buf;
}

/* ===========================================================================
 * ipc_msg_send
 * ===========================================================================
 * Description:  Message send, first buffer of the message should be provided,
 *
 * Parameters:		buf_first	- Pointer to the first message buffer
 *			prio		- Transport priority level
 *
 *
 * Returns: Result code
 *
 */
int32_t ipc_msg_send(char *buf_first, enum ipc_trns_prio prio)
{
	struct ipc_next_buf		*buf;
	struct ipc_trns_func const	*trns_funcs;
	ipc_trns_send_t			send_func;
	uint8_t				dest_aid;
	uint8_t				cpuid;
	int32_t				res = IPC_GENERIC_ERROR;

	if (likely(buf_first)) {
		dest_aid	= (((struct ipc_first_buf *)buf_first)->
							msg_hdr).dest_aid;
		cpuid		= ipc_get_node(dest_aid);
		buf		= (struct ipc_next_buf *)buf_first;
		trns_funcs	= get_trns_funcs(cpuid);
		if (likely(trns_funcs)) {
			send_func = trns_funcs->trns_send;
			if (send_func)
				res = send_func((char *)buf, cpuid, prio);
		}
	}
	return res;
}

/* -----------------------------------------------------------
 * Function:	ipc_recv
 * Description:	Processing IPC messages
 * Input:		max_msg_count	- max number processed messages per call
 *			prio		- transport priority level
 * Output:		number of processed messages
 * -----------------------------------------------------------
 */
uint32_t ipc_recv(uint32_t max_msg_count, enum ipc_trns_prio prio)
{
	unsigned		ix;
	char			*ipc_data;

	for (ix = 0; ix < max_msg_count; ix++) {
		ipc_data = ipc_trns_fifo_buf_read(prio);

		if (ipc_data) {
			/* IPC_msg_handler(ipc_data); */
			handle_incoming_packet(ipc_data, ipc_own_node, prio);
		} else
			break; /* no more messages, queue empty */
	}
	return ix;
}