summaryrefslogtreecommitdiff
path: root/include/linux/ipc_router.h
blob: 04a06df66d4b8e8cf13d309d9141c2d2d98c32ef (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
/* Copyright (c) 2012-2015,2017 The Linux Foundation. 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 and
 * only 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.
 */

#ifndef _IPC_ROUTER_H
#define _IPC_ROUTER_H

#include <linux/types.h>
#include <linux/socket.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/list.h>
#include <linux/pm.h>
#include <linux/msm_ipc.h>
#include <linux/device.h>
#include <linux/kref.h>

/* Maximum Wakeup Source Name Size */
#define MAX_WS_NAME_SZ 32

#define IPC_RTR_ERR(buf, ...) \
	pr_err("IPC_RTR: " buf, __VA_ARGS__)

/**
 * enum msm_ipc_router_event - Events that will be generated by IPC Router
 */
enum msm_ipc_router_event {
	IPC_ROUTER_CTRL_CMD_DATA = 1,
	IPC_ROUTER_CTRL_CMD_HELLO,
	IPC_ROUTER_CTRL_CMD_BYE,
	IPC_ROUTER_CTRL_CMD_NEW_SERVER,
	IPC_ROUTER_CTRL_CMD_REMOVE_SERVER,
	IPC_ROUTER_CTRL_CMD_REMOVE_CLIENT,
	IPC_ROUTER_CTRL_CMD_RESUME_TX,
};

/**
 * rr_control_msg - Control message structure
 * @cmd: Command identifier for HELLO message in Version 1.
 * @hello: Message structure for HELLO message in Version 2.
 * @srv: Message structure for NEW_SERVER/REMOVE_SERVER events.
 * @cli: Message structure for REMOVE_CLIENT event.
 */
union rr_control_msg {
	uint32_t cmd;
	struct {
		uint32_t cmd;
		uint32_t checksum;
		uint32_t versions;
		uint32_t capability;
		uint32_t reserved;
	} hello;
	struct {
		uint32_t cmd;
		uint32_t service;
		uint32_t instance;
		uint32_t node_id;
		uint32_t port_id;
	} srv;
	struct {
		uint32_t cmd;
		uint32_t node_id;
		uint32_t port_id;
	} cli;
};

struct comm_mode_info {
	int mode;
	void *xprt_info;
};

enum ipc_rtr_af_event_type {
	IPCRTR_AF_INIT = 1,
	IPCRTR_AF_DEINIT,
};

/**
 * msm_ipc_port - Definition of IPC Router port
 * @list: List(local/control ports) in which this port is present.
 * @ref: Reference count for this port.
 * @this_port: Contains port's node_id and port_id information.
 * @port_name: Contains service & instance info if the port hosts a service.
 * @type: Type of the port - Client, Service, Control or Security Config.
 * @flags: Flags to identify the port state.
 * @port_lock_lhc3: Lock to protect access to the port information.
 * @mode_info: Communication mode of the port owner.
 * @port_rx_q: Receive queue where incoming messages are queued.
 * @port_rx_q_lock_lhc3: Lock to protect access to the port's rx_q.
 * @rx_ws_name: Name of the receive wakeup source.
 * @port_rx_ws: Wakeup source to prevent suspend until the rx_q is empty.
 * @port_rx_wait_q: Wait queue to wait for the incoming messages.
 * @restart_state: Flag to hold the restart state information.
 * @restart_lock: Lock to protect access to the restart_state.
 * @restart_wait: Wait Queue to wait for any restart events.
 * @endpoint: Contains the information related to user-space interface.
 * @notify: Function to notify the incoming events on the port.
 * @check_send_permissions: Function to check access control from this port.
 * @num_tx: Number of packets transmitted.
 * @num_rx: Number of packets received.
 * @num_tx_bytes: Number of bytes transmitted.
 * @num_rx_bytes: Number of bytes received.
 * @priv: Private information registered by the port owner.
 */
struct msm_ipc_port {
	struct list_head list;
	struct kref ref;

	struct msm_ipc_port_addr this_port;
	struct msm_ipc_port_name port_name;
	uint32_t type;
	unsigned flags;
	struct mutex port_lock_lhc3;
	struct comm_mode_info mode_info;

	struct msm_ipc_port_addr dest_addr;
	int conn_status;

	struct list_head port_rx_q;
	struct mutex port_rx_q_lock_lhc3;
	char rx_ws_name[MAX_WS_NAME_SZ];
	struct wakeup_source *port_rx_ws;
	wait_queue_head_t port_rx_wait_q;
	wait_queue_head_t port_tx_wait_q;

	int restart_state;
	spinlock_t restart_lock;
	wait_queue_head_t restart_wait;

	void *rport_info;
	void *endpoint;
	void (*notify)(unsigned event, void *oob_data,
		       size_t oob_data_len, void *priv);
	int (*check_send_permissions)(void *data);

	uint32_t num_tx;
	uint32_t num_rx;
	unsigned long num_tx_bytes;
	unsigned long num_rx_bytes;
	void *priv;
};

#ifdef CONFIG_IPC_ROUTER
/**
 * msm_ipc_router_create_port() - Create a IPC Router port/endpoint
 * @notify: Callback function to notify any event on the port.
 *   @event: Event ID to be handled.
 *   @oob_data: Any out-of-band data associated with the event.
 *   @oob_data_len: Size of the out-of-band data, if valid.
 *   @priv: Private data registered during the port creation.
 * @priv: Private info to be passed while the notification is generated.
 *
 * @return: Pointer to the port on success, NULL on error.
 */
struct msm_ipc_port *msm_ipc_router_create_port(
	void (*notify)(unsigned event, void *oob_data,
		       size_t oob_data_len, void *priv),
	void *priv);

/**
 * msm_ipc_router_bind_control_port() - Bind a port as a control port
 * @port_ptr: Port which needs to be marked as a control port.
 *
 * @return: 0 on success, standard Linux error codes on error.
 */
int msm_ipc_router_bind_control_port(struct msm_ipc_port *port_ptr);

/**
 * msm_ipc_router_lookup_server_name() - Resolve server address
 * @srv_name: Name<service:instance> of the server to be resolved.
 * @srv_info: Buffer to hold the resolved address.
 * @num_entries_in_array: Number of server info the buffer can hold.
 * @lookup_mask: Mask to specify the range of instances to be resolved.
 *
 * @return: Number of server addresses resolved on success, < 0 on error.
 */
int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name,
				      struct msm_ipc_server_info *srv_info,
				      int num_entries_in_array,
				      uint32_t lookup_mask);

/**
 * msm_ipc_router_send_msg() - Send a message/packet
 * @src: Sender's address/port.
 * @dest: Destination address.
 * @data: Pointer to the data to be sent.
 * @data_len: Length of the data to be sent.
 *
 * @return: 0 on success, < 0 on error.
 */
int msm_ipc_router_send_msg(struct msm_ipc_port *src,
			    struct msm_ipc_addr *dest,
			    void *data, unsigned int data_len);

/**
 * msm_ipc_router_get_curr_pkt_size() - Get the packet size of the first
 *                                      packet in the rx queue
 * @port_ptr: Port which owns the rx queue.
 *
 * @return: Returns the size of the first packet, if available.
 *          0 if no packets available, < 0 on error.
 */
int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr);

/**
 * msm_ipc_router_read_msg() - Read a message/packet
 * @port_ptr: Receiver's port/address.
 * @data: Pointer containing the address of the received data.
 * @src: Address of the sender/source.
 * @len: Length of the data being read.
 *
 * @return: 0 on success, < 0 on error.
 */
int msm_ipc_router_read_msg(struct msm_ipc_port *port_ptr,
			    struct msm_ipc_addr *src,
			    unsigned char **data,
			    unsigned int *len);

/**
 * msm_ipc_router_close_port() - Close the port
 * @port_ptr: Pointer to the port to be closed.
 *
 * @return: 0 on success, < 0 on error.
 */
int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr);

/**
 * msm_ipc_router_register_server() - Register a service on a port
 * @server_port: IPC Router port with which a service is registered.
 * @name: Service name <service_id:instance_id> that gets registered.
 *
 * @return: 0 on success, standard Linux error codes on error.
 */
int msm_ipc_router_register_server(struct msm_ipc_port *server_port,
				   struct msm_ipc_addr *name);

/**
 * msm_ipc_router_unregister_server() - Unregister a service from a port
 * @server_port: Port with with a service is already registered.
 *
 * @return: 0 on success, standard Linux error codes on error.
 */
int msm_ipc_router_unregister_server(struct msm_ipc_port *server_port);

/**
 * register_ipcrtr_af_init_notifier() - Register for ipc router socket
 *				address family initialization callback
 * @nb: Notifier block which will be notified once address family is
 *	initialized.
 *
 * Return: 0 on success, standard error code otherwise.
 */
int register_ipcrtr_af_init_notifier(struct notifier_block *nb);

/**
 * unregister_ipcrtr_af_init_notifier() - Unregister for ipc router socket
 *					address family initialization callback
 * @nb: Notifier block which will be notified once address family is
 *	initialized.
 *
 * Return: 0 on success, standard error code otherwise.
 */
int unregister_ipcrtr_af_init_notifier(struct notifier_block *nb);

/**
 * msm_ipc_router_set_ws_allowed() - To Enable/disable the wakeup source allowed
 *					flag
 * @flag: Flag to set/clear the wakeup soruce allowed
 *
 */
void msm_ipc_router_set_ws_allowed(bool flag);

#else

struct msm_ipc_port *msm_ipc_router_create_port(
	void (*notify)(unsigned event, void *oob_data,
		       size_t oob_data_len, void *priv),
	void *priv)
{
	return NULL;
}

static inline int msm_ipc_router_bind_control_port(
		struct msm_ipc_port *port_ptr)
{
	return -ENODEV;
}

int msm_ipc_router_lookup_server_name(struct msm_ipc_port_name *srv_name,
				      struct msm_ipc_server_info *srv_info,
				      int num_entries_in_array,
				      uint32_t lookup_mask)
{
	return -ENODEV;
}

int msm_ipc_router_send_msg(struct msm_ipc_port *src,
			    struct msm_ipc_addr *dest,
			    void *data, unsigned int data_len)
{
	return -ENODEV;
}

int msm_ipc_router_get_curr_pkt_size(struct msm_ipc_port *port_ptr)
{
	return -ENODEV;
}

int msm_ipc_router_read_msg(struct msm_ipc_port *port_ptr,
			    struct msm_ipc_addr *src,
			    unsigned char **data,
			    unsigned int *len)
{
	return -ENODEV;
}

int msm_ipc_router_close_port(struct msm_ipc_port *port_ptr)
{
	return -ENODEV;
}

static inline int msm_ipc_router_register_server(
			struct msm_ipc_port *server_port,
			struct msm_ipc_addr *name)
{
	return -ENODEV;
}

static inline int msm_ipc_router_unregister_server(
			struct msm_ipc_port *server_port)
{
	return -ENODEV;
}

int register_ipcrtr_af_init_notifier(struct notifier_block *nb)
{
	return -ENODEV;
}

int unregister_ipcrtr_af_init_notifier(struct notifier_block *nb)
{
	return -ENODEV;
}

void msm_ipc_router_set_ws_allowed(bool flag) { }

#endif

#endif