summaryrefslogtreecommitdiff
path: root/arch/arm/mach-msm/smd_rpcrouter_servers.c
blob: eb74f50c79b44edf24133a19459c960a6fd34413 (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
/* arch/arm/mach-msm/rpc_servers.c
 *
 * Copyright (C) 2007 Google, Inc.
 * Copyright (c) 2009, Code Aurora Forum. All rights reserved.
 * Author: Iliyan Malchev <ibm@android.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/cdev.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/platform_device.h>

#include <linux/uaccess.h>

#include <mach/msm_rpcrouter.h>
#include "smd_rpcrouter.h"

static struct msm_rpc_endpoint *endpoint;

#define FLAG_REGISTERED 0x0001

static LIST_HEAD(rpc_server_list);
static DEFINE_MUTEX(rpc_server_list_lock);
static int rpc_servers_active;
static uint32_t current_xid;

static void rpc_server_register(struct msm_rpc_server *server)
{
	int rc;
	rc = msm_rpc_register_server(endpoint, server->prog, server->vers);
	if (rc < 0)
		printk(KERN_ERR "[rpcserver] error registering %p @ %08x:%d\n",
		       server, server->prog, server->vers);
}

static struct msm_rpc_server *rpc_server_find(uint32_t prog, uint32_t vers)
{
	struct msm_rpc_server *server;

	mutex_lock(&rpc_server_list_lock);
	list_for_each_entry(server, &rpc_server_list, list) {
		if ((server->prog == prog) &&
		    msm_rpc_is_compatible_version(server->vers, vers)) {
			mutex_unlock(&rpc_server_list_lock);
			return server;
		}
	}
	mutex_unlock(&rpc_server_list_lock);
	return NULL;
}

static void rpc_server_register_all(void)
{
	struct msm_rpc_server *server;

	mutex_lock(&rpc_server_list_lock);
	list_for_each_entry(server, &rpc_server_list, list) {
		if (!(server->flags & FLAG_REGISTERED)) {
			rpc_server_register(server);
			server->flags |= FLAG_REGISTERED;
		}
	}
	mutex_unlock(&rpc_server_list_lock);
}

int msm_rpc_create_server(struct msm_rpc_server *server)
{
	/* make sure we're in a sane state first */
	server->flags = 0;
	INIT_LIST_HEAD(&server->list);
	mutex_init(&server->cb_req_lock);
	mutex_init(&server->reply_lock);

	server->reply = kmalloc(MSM_RPC_MSGSIZE_MAX, GFP_KERNEL);
	if (!server->reply)
		return -ENOMEM;

	server->cb_req = kmalloc(MSM_RPC_MSGSIZE_MAX, GFP_KERNEL);
	if (!server->cb_req) {
		kfree(server->reply);
		return -ENOMEM;
	}

	server->cb_ept = msm_rpc_open();
	if (IS_ERR(server->cb_ept)) {
		kfree(server->reply);
		kfree(server->cb_req);
		return PTR_ERR(server->cb_ept);
	}

	server->cb_ept->flags = MSM_RPC_UNINTERRUPTIBLE;
	server->cb_ept->dst_prog = cpu_to_be32(server->prog | 0x01000000);
	server->cb_ept->dst_vers = cpu_to_be32(server->vers);

	mutex_lock(&rpc_server_list_lock);
	list_add(&server->list, &rpc_server_list);
	if (rpc_servers_active) {
		rpc_server_register(server);
		server->flags |= FLAG_REGISTERED;
	}
	mutex_unlock(&rpc_server_list_lock);

	return 0;
}
EXPORT_SYMBOL(msm_rpc_create_server);

static int rpc_send_accepted_void_reply(struct msm_rpc_endpoint *client,
					uint32_t xid, uint32_t accept_status)
{
	int rc = 0;
	uint8_t reply_buf[sizeof(struct rpc_reply_hdr)];
	struct rpc_reply_hdr *reply = (struct rpc_reply_hdr *)reply_buf;

	reply->xid = cpu_to_be32(xid);
	reply->type = cpu_to_be32(1); /* reply */
	reply->reply_stat = cpu_to_be32(RPCMSG_REPLYSTAT_ACCEPTED);

	reply->data.acc_hdr.accept_stat = cpu_to_be32(accept_status);
	reply->data.acc_hdr.verf_flavor = 0;
	reply->data.acc_hdr.verf_length = 0;

	rc = msm_rpc_write(client, reply_buf, sizeof(reply_buf));
	if (rc ==  -ENETRESET) {
		/* Modem restarted, drop reply, clear state */
		msm_rpc_clear_netreset(client);
	}
	if (rc < 0)
		printk(KERN_ERR
		       "%s: could not write response: %d\n",
		       __FUNCTION__, rc);

	return rc;
}

/*
 * Interface to be used to start accepted reply message for a
 * request.  Returns the buffer pointer to attach any payload.
 * Should call msm_rpc_server_send_accepted_reply to complete sending
 * reply.  Marshaling should be handled by user for the payload.
 *
 * server: pointer to server data structure
 *
 * xid: transaction id. Has to be same as the one in request.
 *
 * accept_status: acceptance status
 *
 * Return Value:
 *        pointer to buffer to attach the payload.
 */
void *msm_rpc_server_start_accepted_reply(struct msm_rpc_server *server,
					  uint32_t xid, uint32_t accept_status)
{
	struct rpc_reply_hdr *reply;

	mutex_lock(&server->reply_lock);

	reply = (struct rpc_reply_hdr *)server->reply;

	reply->xid = cpu_to_be32(xid);
	reply->type = cpu_to_be32(1); /* reply */
	reply->reply_stat = cpu_to_be32(RPCMSG_REPLYSTAT_ACCEPTED);

	reply->data.acc_hdr.accept_stat = cpu_to_be32(accept_status);
	reply->data.acc_hdr.verf_flavor = 0;
	reply->data.acc_hdr.verf_length = 0;

	return reply + 1;
}
EXPORT_SYMBOL(msm_rpc_server_start_accepted_reply);

/*
 * Interface to be used to send accepted reply for a request.
 * msm_rpc_server_start_accepted_reply should have been called before.
 * Marshaling should be handled by user for the payload.
 *
 * server: pointer to server data structure
 *
 * size: additional payload size
 *
 * Return Value:
 *        0 on success, otherwise returns an error code.
 */
int msm_rpc_server_send_accepted_reply(struct msm_rpc_server *server,
				       uint32_t size)
{
	int rc = 0;

	size += sizeof(struct rpc_reply_hdr);
	rc = msm_rpc_write(endpoint, server->reply, size);
	if (rc > 0)
		rc = 0;

	mutex_unlock(&server->reply_lock);
	return rc;
}
EXPORT_SYMBOL(msm_rpc_server_send_accepted_reply);

/*
 * Interface to be used to send a server callback request.
 * If the request takes any arguments or expects any return, the user
 * should handle it in 'arg_func' and 'ret_func' respectively.
 * Marshaling and Unmarshaling should be handled by the user in argument
 * and return functions.
 *
 * server: pointer to server data sturcture
 *
 * clnt_info: pointer to client information data structure.
 *            callback will be sent to this client.
 *
 * cb_proc: callback procedure being requested
 *
 * arg_func: argument function pointer.  'buf' is where arguments needs to
 *   be filled. 'data' is arg_data.
 *
 * ret_func: return function pointer.  'buf' is where returned data should
 *   be read from. 'data' is ret_data.
 *
 * arg_data: passed as an input parameter to argument function.
 *
 * ret_data: passed as an input parameter to return function.
 *
 * timeout: timeout for reply wait in jiffies.  If negative timeout is
 *   specified a default timeout of 10s is used.
 *
 * Return Value:
 *        0 on success, otherwise an error code is returned.
 */
int msm_rpc_server_cb_req(struct msm_rpc_server *server,
			  struct msm_rpc_client_info *clnt_info,
			  uint32_t cb_proc,
			  int (*arg_func)(struct msm_rpc_server *server,
					  void *buf, void *data),
			  void *arg_data,
			  int (*ret_func)(struct msm_rpc_server *server,
					  void *buf, void *data),
			  void *ret_data, long timeout)
{
	int size = 0;
	struct rpc_reply_hdr *rpc_rsp;
	void *buffer;
	int rc = 0;

	if (!clnt_info)
		return -EINVAL;

	mutex_lock(&server->cb_req_lock);

	msm_rpc_setup_req((struct rpc_request_hdr *)server->cb_req,
			  (server->prog | 0x01000000),
			  be32_to_cpu(clnt_info->vers), cb_proc);
	size = sizeof(struct rpc_request_hdr);

	if (arg_func) {
		rc = arg_func(server, (void *)((struct rpc_request_hdr *)
					       server->cb_req + 1), arg_data);
		if (rc < 0)
			goto release_locks;
		else
			size += rc;
	}

	server->cb_ept->dst_pid = clnt_info->pid;
	server->cb_ept->dst_cid = clnt_info->cid;
	rc = msm_rpc_write(server->cb_ept, server->cb_req, size);
	if (rc < 0) {
		pr_err("%s: couldn't send RPC CB request:%d\n", __func__, rc);
		goto release_locks;
	} else
		rc = 0;

	if (timeout < 0)
		timeout = msecs_to_jiffies(10000);

	rc = msm_rpc_read(server->cb_ept, &buffer, -1, timeout);
	if ((rc < ((int)(sizeof(uint32_t) * 2))) ||
	    (be32_to_cpu(*((uint32_t *)buffer + 1)) != 1)) {
		printk(KERN_ERR "%s: could not read: %d\n", __func__, rc);
		kfree(buffer);
		goto free_and_release;
	} else
		rc = 0;

	rpc_rsp = (struct rpc_reply_hdr *)buffer;

	if (be32_to_cpu(rpc_rsp->reply_stat) != RPCMSG_REPLYSTAT_ACCEPTED) {
		pr_err("%s: RPC cb req was denied! %d\n", __func__,
		       be32_to_cpu(rpc_rsp->reply_stat));
		rc = -EPERM;
		goto free_and_release;
	}

	if (be32_to_cpu(rpc_rsp->data.acc_hdr.accept_stat) !=
	    RPC_ACCEPTSTAT_SUCCESS) {
		pr_err("%s: RPC cb req was not successful (%d)\n", __func__,
		       be32_to_cpu(rpc_rsp->data.acc_hdr.accept_stat));
		rc = -EINVAL;
		goto free_and_release;
	}

	if (ret_func)
		rc = ret_func(server, (void *)(rpc_rsp + 1), ret_data);

free_and_release:
	kfree(buffer);
release_locks:
	mutex_unlock(&server->cb_req_lock);
	return rc;
}
EXPORT_SYMBOL(msm_rpc_server_cb_req);

void msm_rpc_server_get_requesting_client(struct msm_rpc_client_info *clnt_info)
{
	if (!clnt_info)
		return;

	get_requesting_client(endpoint, current_xid, clnt_info);
}

static int rpc_servers_thread(void *data)
{
	void *buffer;
	struct rpc_request_hdr *req;
	struct msm_rpc_server *server;
	int rc;

	for (;;) {
		rc = wait_event_interruptible(endpoint->wait_q,
					      !list_empty(&endpoint->read_q));
		rc = msm_rpc_read(endpoint, &buffer, -1, -1);
		if (rc < 0) {
			printk(KERN_ERR "%s: could not read: %d\n",
			       __FUNCTION__, rc);
			break;
		}

		req = (struct rpc_request_hdr *)buffer;

		current_xid = req->xid;

		req->type = be32_to_cpu(req->type);
		req->xid = be32_to_cpu(req->xid);
		req->rpc_vers = be32_to_cpu(req->rpc_vers);
		req->prog = be32_to_cpu(req->prog);
		req->vers = be32_to_cpu(req->vers);
		req->procedure = be32_to_cpu(req->procedure);

		server = rpc_server_find(req->prog, req->vers);

		if (req->rpc_vers != 2)
			continue;
		if (req->type != 0)
			continue;
		if (!server) {
			rpc_send_accepted_void_reply(
				endpoint, req->xid,
				RPC_ACCEPTSTAT_PROG_UNAVAIL);
			continue;
		}

		rc = server->rpc_call(server, req, rc);

		if (rc == 0) {
			msm_rpc_server_start_accepted_reply(
				server, req->xid,
				RPC_ACCEPTSTAT_SUCCESS);
			msm_rpc_server_send_accepted_reply(server, 0);
		} else if (rc < 0) {
			msm_rpc_server_start_accepted_reply(
				server, req->xid,
				RPC_ACCEPTSTAT_PROC_UNAVAIL);
			msm_rpc_server_send_accepted_reply(server, 0);
		}
		kfree(buffer);
	}
	do_exit(0);
}

static int rpcservers_probe(struct platform_device *pdev)
{
	struct task_struct *server_thread;

	endpoint = msm_rpc_open();
	if (IS_ERR(endpoint))
		return PTR_ERR(endpoint);

	/* we're online -- register any servers installed beforehand */
	rpc_servers_active = 1;
	current_xid = 0;
	rpc_server_register_all();

	/* start the kernel thread */
	server_thread = kthread_run(rpc_servers_thread, NULL, "krpcserversd");
	if (IS_ERR(server_thread))
		return PTR_ERR(server_thread);

	return 0;
}

static struct platform_driver rpcservers_driver = {
	.probe	= rpcservers_probe,
	.driver	= {
		.name	= "oncrpc_router",
		.owner	= THIS_MODULE,
	},
};

static int __init rpc_servers_init(void)
{
	return platform_driver_register(&rpcservers_driver);
}

module_init(rpc_servers_init);

MODULE_DESCRIPTION("MSM RPC Servers");
MODULE_AUTHOR("Iliyan Malchev <ibm@android.com>");
MODULE_LICENSE("GPL");