PageRenderTime 93ms CodeModel.GetById 13ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 1ms

/arch/arm/mach-msm/rmt_storage_client.c

https://bitbucket.org/sammyz/iscream_thunderc-2.6.35-rebase
C | 1606 lines | 1318 code | 239 blank | 49 comment | 165 complexity | 64b1982690190e8b51cae9d47de52a34 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
   1/* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
   2 *
   3 * This program is free software; you can redistribute it and/or modify
   4 * it under the terms of the GNU General Public License version 2 and
   5 * only version 2 as published by the Free Software Foundation.
   6 *
   7 * This program is distributed in the hope that it will be useful,
   8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
   9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10 * GNU General Public License for more details.
  11 *
  12 * You should have received a copy of the GNU General Public License
  13 * along with this program; if not, write to the Free Software
  14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15 * 02110-1301, USA.
  16 *
  17 */
  18
  19#include <linux/miscdevice.h>
  20#include <linux/wait.h>
  21#include <linux/module.h>
  22#include <linux/kernel.h>
  23#include <linux/string.h>
  24#include <linux/errno.h>
  25#include <linux/init.h>
  26#include <linux/types.h>
  27#include <linux/mm.h>
  28#include <linux/fs.h>
  29#include <linux/err.h>
  30#include <linux/sched.h>
  31#include <linux/wakelock.h>
  32#include <linux/rmt_storage_client.h>
  33#include <linux/debugfs.h>
  34#include <linux/slab.h>
  35#include <asm/uaccess.h>
  36#include <asm/pgtable.h>
  37#include <mach/msm_rpcrouter.h>
  38#ifdef CONFIG_MSM_SDIO_SMEM
  39#include <mach/sdio_smem.h>
  40#endif
  41#include "smd_private.h"
  42
  43enum {
  44	RMT_STORAGE_EVNT_OPEN = 0,
  45	RMT_STORAGE_EVNT_CLOSE,
  46	RMT_STORAGE_EVNT_WRITE_BLOCK,
  47	RMT_STORAGE_EVNT_GET_DEV_ERROR,
  48	RMT_STORAGE_EVNT_WRITE_IOVEC,
  49	RMT_STORAGE_EVNT_SEND_USER_DATA,
  50	RMT_STORAGE_EVNT_READ_IOVEC,
  51	RMT_STORAGE_EVNT_ALLOC_RMT_BUF,
  52} rmt_storage_event;
  53
  54struct shared_ramfs_entry {
  55	uint32_t client_id;	/* Client id to uniquely identify a client */
  56	uint32_t base_addr;	/* Base address of shared RAMFS memory */
  57	uint32_t size;		/* Size of the shared RAMFS memory */
  58	uint32_t client_sts;	/* This will be initialized to 1 when
  59				   remote storage RPC client is ready
  60				   to process requests */
  61};
  62struct shared_ramfs_table {
  63	uint32_t magic_id;	/* Identify RAMFS details in SMEM */
  64	uint32_t version;	/* Version of shared_ramfs_table */
  65	uint32_t entries;	/* Total number of valid entries   */
  66	/* List all entries */
  67	struct shared_ramfs_entry ramfs_entry[MAX_RAMFS_TBL_ENTRIES];
  68};
  69
  70struct rmt_storage_client_info {
  71	unsigned long cids;
  72	/* Shared memory parameters from RAMFS table and alloc_rmt_buf */
  73	struct rmt_shrd_mem_param rmt_shrd_mem[MAX_SHRD_MEM_ENTRIES];
  74	int open_excl;
  75	atomic_t total_events;
  76	wait_queue_head_t event_q;
  77	struct list_head event_list;
  78	struct list_head client_list;	/* List of remote storage clients */
  79	/* Lock to protect event and client lists */
  80	spinlock_t lock;
  81	/* Wakelock to be acquired when processing requests from modem */
  82	struct wake_lock wlock;
  83	atomic_t wcount;
  84	/* Shared memory parameters from RAMFS table only*/
  85	struct shared_ramfs_entry *smem_info[MAX_RAMFS_TBL_ENTRIES];
  86};
  87
  88struct rmt_storage_kevent {
  89	struct list_head list;
  90	struct rmt_storage_event event;
  91};
  92
  93/* Remote storage server on modem */
  94struct rmt_storage_srv {
  95	uint32_t prog;
  96	int sync_token;
  97	struct platform_driver plat_drv;
  98	struct msm_rpc_client *rpc_client;
  99};
 100
 101/* Remote storage client on modem */
 102struct rmt_storage_client {
 103	uint32_t handle;
 104	uint32_t sid;			/* Storage ID */
 105	char path[MAX_PATH_NAME];
 106	struct rmt_storage_srv *srv;
 107	struct list_head list;
 108};
 109
 110static struct rmt_storage_srv *rmt_storage_get_srv(uint32_t prog);
 111static uint32_t rmt_storage_get_sid(const char *path);
 112
 113static struct rmt_storage_client_info *rmc;
 114
 115#ifdef CONFIG_MSM_SDIO_SMEM
 116static struct sdio_smem_client *sdio_smem;
 117#endif
 118
 119#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
 120struct rmt_storage_op_stats {
 121	unsigned long count;
 122	ktime_t start;
 123	ktime_t min;
 124	ktime_t max;
 125	ktime_t total;
 126};
 127struct rmt_storage_stats {
 128       char path[MAX_PATH_NAME];
 129       struct rmt_storage_op_stats rd_stats;
 130       struct rmt_storage_op_stats wr_stats;
 131};
 132static struct rmt_storage_stats client_stats[MAX_NUM_CLIENTS];
 133static struct dentry *stats_dentry;
 134#endif
 135
 136#define MSM_RMT_STORAGE_APIPROG	0x300000A7
 137#define MDM_RMT_STORAGE_APIPROG	0x300100A7
 138
 139#define RMT_STORAGE_OP_FINISH_PROC              2
 140#define RMT_STORAGE_REGISTER_OPEN_PROC          3
 141#define RMT_STORAGE_REGISTER_WRITE_IOVEC_PROC   4
 142#define RMT_STORAGE_REGISTER_CB_PROC            5
 143#define RMT_STORAGE_UN_REGISTER_CB_PROC         6
 144#define RMT_STORAGE_FORCE_SYNC_PROC             7
 145#define RMT_STORAGE_GET_SYNC_STATUS_PROC        8
 146#define RMT_STORAGE_REGISTER_READ_IOVEC_PROC    9
 147#define RMT_STORAGE_REGISTER_ALLOC_RMT_BUF_PROC 10
 148
 149#define RMT_STORAGE_OPEN_CB_TYPE_PROC           1
 150#define RMT_STORAGE_WRITE_IOVEC_CB_TYPE_PROC    2
 151#define RMT_STORAGE_EVENT_CB_TYPE_PROC          3
 152#define RMT_STORAGE_READ_IOVEC_CB_TYPE_PROC     4
 153#define RMT_STORAGE_ALLOC_RMT_BUF_CB_TYPE_PROC  5
 154
 155#define RAMFS_INFO_MAGICNUMBER		0x654D4D43
 156#define RAMFS_INFO_VERSION		0x00000001
 157#define RAMFS_DEFAULT			0xFFFFFFFF
 158
 159/* MSM EFS*/
 160#define RAMFS_MODEMSTORAGE_ID		0x4D454653
 161#define RAMFS_SHARED_EFS_RAM_BASE	0x46100000
 162#define RAMFS_SHARED_EFS_RAM_SIZE	(3 * 1024 * 1024)
 163
 164/* MDM EFS*/
 165#define RAMFS_MDM_STORAGE_ID		0x4D4583A1
 166
 167#define for_each_smem_info(s, i)	\
 168		for (i = 0; \
 169		     (i < MAX_RAMFS_TBL_ENTRIES) && (s = rmc->smem_info[i]); \
 170		     i++)
 171
 172static struct rmt_storage_client *rmt_storage_get_client(uint32_t handle)
 173{
 174	struct rmt_storage_client *rs_client;
 175	list_for_each_entry(rs_client, &rmc->client_list, list)
 176		if (rs_client->handle == handle)
 177			return rs_client;
 178	return NULL;
 179}
 180
 181static struct rmt_shrd_mem_param *rmt_storage_get_shrd_mem(uint32_t sid)
 182{
 183	int i;
 184
 185	for (i = 0; i < MAX_SHRD_MEM_ENTRIES; i++)
 186		if (rmc->rmt_shrd_mem[i].sid == sid)
 187			return &rmc->rmt_shrd_mem[i];
 188	return NULL;
 189}
 190
 191static struct shared_ramfs_entry *rmt_storage_get_smem_info(uint32_t client_id)
 192{
 193	int i;
 194	struct shared_ramfs_entry *smem_info;
 195
 196	for_each_smem_info(smem_info, i)
 197		if (smem_info->client_id == client_id)
 198			return smem_info;
 199	return NULL;
 200}
 201
 202static struct msm_rpc_client *rmt_storage_get_rpc_client(uint32_t handle)
 203{
 204	struct rmt_storage_client *rs_client;
 205
 206	rs_client = rmt_storage_get_client(handle);
 207	if (!rs_client)
 208		return NULL;
 209	return rs_client->srv->rpc_client;
 210}
 211
 212static int rmt_storage_validate_iovec(uint32_t handle,
 213				      struct rmt_storage_iovec_desc *xfer)
 214{
 215	struct rmt_storage_client *rs_client;
 216	struct rmt_shrd_mem_param *shrd_mem;
 217
 218	rs_client = rmt_storage_get_client(handle);
 219	if (!rs_client)
 220		return -EINVAL;
 221	shrd_mem = rmt_storage_get_shrd_mem(rs_client->sid);
 222	if (!shrd_mem)
 223		return -EINVAL;
 224
 225	if ((xfer->data_phy_addr < shrd_mem->start) ||
 226	    ((xfer->data_phy_addr + RAMFS_BLOCK_SIZE * xfer->num_sector) >
 227	     (shrd_mem->start + shrd_mem->size)))
 228		return -EINVAL;
 229	return 0;
 230}
 231
 232static int rmt_storage_send_sts_arg(struct msm_rpc_client *client,
 233				struct msm_rpc_xdr *xdr, void *data)
 234{
 235	struct rmt_storage_send_sts *args = data;
 236
 237	xdr_send_uint32(xdr, &args->handle);
 238	xdr_send_uint32(xdr, &args->err_code);
 239	xdr_send_uint32(xdr, &args->data);
 240	return 0;
 241}
 242
 243static void put_event(struct rmt_storage_client_info *rmc,
 244			struct rmt_storage_kevent *kevent)
 245{
 246	spin_lock(&rmc->lock);
 247	list_add_tail(&kevent->list, &rmc->event_list);
 248	spin_unlock(&rmc->lock);
 249}
 250
 251static struct rmt_storage_kevent *get_event(struct rmt_storage_client_info *rmc)
 252{
 253	struct rmt_storage_kevent *kevent = NULL;
 254
 255	spin_lock(&rmc->lock);
 256	if (!list_empty(&rmc->event_list)) {
 257		kevent = list_first_entry(&rmc->event_list,
 258			struct rmt_storage_kevent, list);
 259		list_del(&kevent->list);
 260	}
 261	spin_unlock(&rmc->lock);
 262	return kevent;
 263}
 264
 265static int rmt_storage_event_open_cb(struct rmt_storage_event *event_args,
 266		struct msm_rpc_xdr *xdr)
 267{
 268	uint32_t cid, len, event_type;
 269	char *path;
 270	int ret;
 271	struct rmt_storage_srv *srv;
 272	struct rmt_storage_client *rs_client;
 273#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
 274	struct rmt_storage_stats *stats;
 275#endif
 276
 277	srv = rmt_storage_get_srv(event_args->usr_data);
 278	if (!srv)
 279		return -EINVAL;
 280
 281	xdr_recv_uint32(xdr, &event_type);
 282	if (event_type != RMT_STORAGE_EVNT_OPEN)
 283		return -1;
 284
 285	pr_info("%s: open callback received\n", __func__);
 286	rs_client = kzalloc(sizeof(struct rmt_storage_client), GFP_KERNEL);
 287	if (!rs_client) {
 288		pr_err("%s: Error allocating rmt storage client\n", __func__);
 289		return -ENOMEM;
 290	}
 291
 292	ret = xdr_recv_bytes(xdr, (void **)&path, &len);
 293	if (ret || !path) {
 294		pr_err("%s: Invalid path\n", __func__);
 295		if (!ret)
 296			ret = -1;
 297		goto free_rs_client;
 298	}
 299	memcpy(event_args->path, path, len);
 300	pr_info("open partition %s\n", event_args->path);
 301
 302	rs_client->sid = rmt_storage_get_sid(event_args->path);
 303	if (!rs_client->sid) {
 304		pr_err("%s: No storage id found for %s\n", __func__,
 305		       event_args->path);
 306		ret = -EINVAL;
 307		goto free_path;
 308	}
 309	strncpy(rs_client->path, event_args->path, MAX_PATH_NAME);
 310
 311	cid = find_first_zero_bit(&rmc->cids, sizeof(rmc->cids));
 312	if (cid > MAX_NUM_CLIENTS) {
 313		pr_err("%s: Max clients are reached\n", __func__);
 314		cid = 0;
 315		return cid;
 316	}
 317	__set_bit(cid, &rmc->cids);
 318
 319#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
 320	stats = &client_stats[cid - 1];
 321	memcpy(stats->path, event_args->path, len);
 322	memset(stats->rd_stats, 0, sizeof(struct rmt_storage_op_stats));
 323	memset(stats->wr_stats, 0, sizeof(struct rmt_storage_op_stats));
 324	stats->rd_stats.min.tv64 = KTIME_MAX;
 325	stats->wr_stats.min.tv64 = KTIME_MAX;
 326#endif
 327	event_args->id = RMT_STORAGE_OPEN;
 328	event_args->sid = rs_client->sid;
 329	event_args->handle = cid;
 330
 331	rs_client->handle = event_args->handle;
 332	rs_client->srv = srv;
 333	INIT_LIST_HEAD(&rs_client->list);
 334	spin_lock(&rmc->lock);
 335	list_add_tail(&rs_client->list, &rmc->client_list);
 336	spin_unlock(&rmc->lock);
 337
 338	kfree(path);
 339	return cid;
 340
 341free_path:
 342	kfree(path);
 343free_rs_client:
 344	kfree(rs_client);
 345	return ret;
 346}
 347
 348struct rmt_storage_close_args {
 349	uint32_t handle;
 350};
 351
 352struct rmt_storage_rw_block_args {
 353	uint32_t handle;
 354	uint32_t data_phy_addr;
 355	uint32_t sector_addr;
 356	uint32_t num_sector;
 357};
 358
 359struct rmt_storage_get_err_args {
 360	uint32_t handle;
 361};
 362
 363struct rmt_storage_user_data_args {
 364	uint32_t handle;
 365	uint32_t data;
 366};
 367
 368struct rmt_storage_event_params {
 369	uint32_t type;
 370	union {
 371		struct rmt_storage_close_args close;
 372		struct rmt_storage_rw_block_args block;
 373		struct rmt_storage_get_err_args get_err;
 374		struct rmt_storage_user_data_args user_data;
 375	} params;
 376};
 377
 378static int rmt_storage_parse_params(struct msm_rpc_xdr *xdr,
 379		struct rmt_storage_event_params *event)
 380{
 381	xdr_recv_uint32(xdr, &event->type);
 382
 383	switch (event->type) {
 384	case RMT_STORAGE_EVNT_CLOSE: {
 385		struct rmt_storage_close_args *args;
 386		args = &event->params.close;
 387
 388		xdr_recv_uint32(xdr, &args->handle);
 389		break;
 390	}
 391
 392	case RMT_STORAGE_EVNT_WRITE_BLOCK: {
 393		struct rmt_storage_rw_block_args *args;
 394		args = &event->params.block;
 395
 396		xdr_recv_uint32(xdr, &args->handle);
 397		xdr_recv_uint32(xdr, &args->data_phy_addr);
 398		xdr_recv_uint32(xdr, &args->sector_addr);
 399		xdr_recv_uint32(xdr, &args->num_sector);
 400		break;
 401	}
 402
 403	case RMT_STORAGE_EVNT_GET_DEV_ERROR: {
 404		struct rmt_storage_get_err_args *args;
 405		args = &event->params.get_err;
 406
 407		xdr_recv_uint32(xdr, &args->handle);
 408		break;
 409	}
 410
 411	case RMT_STORAGE_EVNT_SEND_USER_DATA: {
 412		struct rmt_storage_user_data_args *args;
 413		args = &event->params.user_data;
 414
 415		xdr_recv_uint32(xdr, &args->handle);
 416		xdr_recv_uint32(xdr, &args->data);
 417		break;
 418	}
 419
 420	default:
 421		pr_err("%s: unknown event %d\n", __func__, event->type);
 422		return -1;
 423	}
 424	return 0;
 425}
 426
 427static int rmt_storage_event_close_cb(struct rmt_storage_event *event_args,
 428		struct msm_rpc_xdr *xdr)
 429{
 430	struct rmt_storage_event_params *event;
 431	struct rmt_storage_close_args *close;
 432	struct rmt_storage_client *rs_client;
 433	uint32_t event_type;
 434	int ret;
 435
 436	xdr_recv_uint32(xdr, &event_type);
 437	if (event_type != RMT_STORAGE_EVNT_CLOSE)
 438		return -1;
 439
 440	pr_info("%s: close callback received\n", __func__);
 441	ret = xdr_recv_pointer(xdr, (void **)&event,
 442			sizeof(struct rmt_storage_event_params),
 443			rmt_storage_parse_params);
 444
 445	if (ret || !event)
 446		return -1;
 447
 448	close = &event->params.close;
 449	event_args->handle = close->handle;
 450	event_args->id = RMT_STORAGE_CLOSE;
 451	__clear_bit(event_args->handle, &rmc->cids);
 452	rs_client = rmt_storage_get_client(event_args->handle);
 453	if (rs_client) {
 454		list_del(&rs_client->list);
 455		kfree(rs_client);
 456	}
 457	kfree(event);
 458	return RMT_STORAGE_NO_ERROR;
 459}
 460
 461static int rmt_storage_event_write_block_cb(
 462		struct rmt_storage_event *event_args,
 463		struct msm_rpc_xdr *xdr)
 464{
 465	struct rmt_storage_event_params *event;
 466	struct rmt_storage_rw_block_args *write_block;
 467	struct rmt_storage_iovec_desc *xfer;
 468	uint32_t event_type;
 469	int ret;
 470
 471	xdr_recv_uint32(xdr, &event_type);
 472	if (event_type != RMT_STORAGE_EVNT_WRITE_BLOCK)
 473		return -1;
 474
 475	pr_info("%s: write block callback received\n", __func__);
 476	ret = xdr_recv_pointer(xdr, (void **)&event,
 477			sizeof(struct rmt_storage_event_params),
 478			rmt_storage_parse_params);
 479
 480	if (ret || !event)
 481		return -1;
 482
 483	write_block = &event->params.block;
 484	event_args->handle = write_block->handle;
 485	xfer = &event_args->xfer_desc[0];
 486	xfer->sector_addr = write_block->sector_addr;
 487	xfer->data_phy_addr = write_block->data_phy_addr;
 488	xfer->num_sector = write_block->num_sector;
 489
 490	ret = rmt_storage_validate_iovec(event_args->handle, xfer);
 491	if (ret)
 492		return -1;
 493	event_args->xfer_cnt = 1;
 494	event_args->id = RMT_STORAGE_WRITE;
 495
 496	if (atomic_inc_return(&rmc->wcount) == 1)
 497		wake_lock(&rmc->wlock);
 498
 499	pr_debug("sec_addr = %u, data_addr = %x, num_sec = %d\n\n",
 500		xfer->sector_addr, xfer->data_phy_addr,
 501		xfer->num_sector);
 502
 503	kfree(event);
 504	return RMT_STORAGE_NO_ERROR;
 505}
 506
 507static int rmt_storage_event_get_err_cb(struct rmt_storage_event *event_args,
 508		struct msm_rpc_xdr *xdr)
 509{
 510	struct rmt_storage_event_params *event;
 511	struct rmt_storage_get_err_args *get_err;
 512	uint32_t event_type;
 513	int ret;
 514
 515	xdr_recv_uint32(xdr, &event_type);
 516	if (event_type != RMT_STORAGE_EVNT_GET_DEV_ERROR)
 517		return -1;
 518
 519	pr_info("%s: get err callback received\n", __func__);
 520	ret = xdr_recv_pointer(xdr, (void **)&event,
 521			sizeof(struct rmt_storage_event_params),
 522			rmt_storage_parse_params);
 523
 524	if (ret || !event)
 525		return -1;
 526
 527	get_err = &event->params.get_err;
 528	event_args->handle = get_err->handle;
 529	kfree(event);
 530	/* Not implemented */
 531	return -1;
 532
 533}
 534
 535static int rmt_storage_event_user_data_cb(struct rmt_storage_event *event_args,
 536		struct msm_rpc_xdr *xdr)
 537{
 538	struct rmt_storage_event_params *event;
 539	struct rmt_storage_user_data_args *user_data;
 540	uint32_t event_type;
 541	int ret;
 542
 543	xdr_recv_uint32(xdr, &event_type);
 544	if (event_type != RMT_STORAGE_EVNT_SEND_USER_DATA)
 545		return -1;
 546
 547	pr_info("%s: send user data callback received\n", __func__);
 548	ret = xdr_recv_pointer(xdr, (void **)&event,
 549			sizeof(struct rmt_storage_event_params),
 550			rmt_storage_parse_params);
 551
 552	if (ret || !event)
 553		return -1;
 554
 555	user_data = &event->params.user_data;
 556	event_args->handle = user_data->handle;
 557	event_args->usr_data = user_data->data;
 558	event_args->id = RMT_STORAGE_SEND_USER_DATA;
 559
 560	kfree(event);
 561	return RMT_STORAGE_NO_ERROR;
 562}
 563
 564static int rmt_storage_event_write_iovec_cb(
 565		struct rmt_storage_event *event_args,
 566		struct msm_rpc_xdr *xdr)
 567{
 568	struct rmt_storage_iovec_desc *xfer;
 569	uint32_t i, ent, event_type;
 570#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
 571	struct rmt_storage_stats *stats;
 572#endif
 573
 574	xdr_recv_uint32(xdr, &event_type);
 575	if (event_type != RMT_STORAGE_EVNT_WRITE_IOVEC)
 576		return -EINVAL;
 577
 578	pr_info("%s: write iovec callback received\n", __func__);
 579	xdr_recv_uint32(xdr, &event_args->handle);
 580	xdr_recv_uint32(xdr, &ent);
 581	pr_debug("handle = %d\n", event_args->handle);
 582
 583#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
 584	stats = &client_stats[event_args->handle - 1];
 585	stats->wr_stats.start = ktime_get();
 586#endif
 587	for (i = 0; i < ent; i++) {
 588		xfer = &event_args->xfer_desc[i];
 589		xdr_recv_uint32(xdr, &xfer->sector_addr);
 590		xdr_recv_uint32(xdr, &xfer->data_phy_addr);
 591		xdr_recv_uint32(xdr, &xfer->num_sector);
 592
 593		if (rmt_storage_validate_iovec(event_args->handle, xfer))
 594			return -EINVAL;
 595
 596		pr_debug("sec_addr = %u, data_addr = %x, num_sec = %d\n",
 597			xfer->sector_addr, xfer->data_phy_addr,
 598			xfer->num_sector);
 599	}
 600	xdr_recv_uint32(xdr, &event_args->xfer_cnt);
 601	event_args->id = RMT_STORAGE_WRITE;
 602	if (atomic_inc_return(&rmc->wcount) == 1)
 603		wake_lock(&rmc->wlock);
 604
 605	pr_debug("iovec transfer count = %d\n\n", event_args->xfer_cnt);
 606	return RMT_STORAGE_NO_ERROR;
 607}
 608
 609static int rmt_storage_event_read_iovec_cb(
 610		struct rmt_storage_event *event_args,
 611		struct msm_rpc_xdr *xdr)
 612{
 613	struct rmt_storage_iovec_desc *xfer;
 614	uint32_t i, ent, event_type;
 615#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
 616	struct rmt_storage_stats *stats;
 617#endif
 618
 619	xdr_recv_uint32(xdr, &event_type);
 620	if (event_type != RMT_STORAGE_EVNT_READ_IOVEC)
 621		return -EINVAL;
 622
 623	pr_info("%s: read iovec callback received\n", __func__);
 624	xdr_recv_uint32(xdr, &event_args->handle);
 625	xdr_recv_uint32(xdr, &ent);
 626	pr_debug("handle = %d\n", event_args->handle);
 627
 628#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
 629	stats = &client_stats[event_args->handle - 1];
 630	stats->rd_stats.start = ktime_get();
 631#endif
 632	for (i = 0; i < ent; i++) {
 633		xfer = &event_args->xfer_desc[i];
 634		xdr_recv_uint32(xdr, &xfer->sector_addr);
 635		xdr_recv_uint32(xdr, &xfer->data_phy_addr);
 636		xdr_recv_uint32(xdr, &xfer->num_sector);
 637
 638		if (rmt_storage_validate_iovec(event_args->handle, xfer))
 639			return -EINVAL;
 640
 641		pr_debug("sec_addr = %u, data_addr = %x, num_sec = %d\n",
 642			xfer->sector_addr, xfer->data_phy_addr,
 643			xfer->num_sector);
 644	}
 645	xdr_recv_uint32(xdr, &event_args->xfer_cnt);
 646	event_args->id = RMT_STORAGE_READ;
 647	if (atomic_inc_return(&rmc->wcount) == 1)
 648		wake_lock(&rmc->wlock);
 649
 650	pr_debug("iovec transfer count = %d\n\n", event_args->xfer_cnt);
 651	return RMT_STORAGE_NO_ERROR;
 652}
 653
 654#ifdef CONFIG_MSM_SDIO_SMEM
 655static int sdio_smem_cb(int event)
 656{
 657	pr_debug("%s: Received event %d\n", __func__, event);
 658
 659	switch (event) {
 660	case SDIO_SMEM_EVENT_READ_DONE:
 661		pr_debug("Read done\n");
 662		break;
 663	case SDIO_SMEM_EVENT_READ_ERR:
 664		pr_err("Read overflow\n");
 665		return -EIO;
 666	default:
 667		pr_err("Unhandled event\n");
 668	}
 669	return 0;
 670}
 671
 672static int sdio_smem_probe(struct platform_device *pdev)
 673{
 674	int ret = 0;
 675	struct rmt_shrd_mem_param *shrd_mem;
 676
 677	sdio_smem = container_of(pdev, struct sdio_smem_client, plat_dev);
 678
 679	/* SDIO SMEM is supported only for MDM */
 680	shrd_mem = rmt_storage_get_shrd_mem(RAMFS_MDM_STORAGE_ID);
 681	if (!shrd_mem) {
 682		pr_err("%s: No shared mem entry for sid=0x%08x\n",
 683		       __func__, (uint32_t)RAMFS_MDM_STORAGE_ID);
 684		return -ENOMEM;
 685	}
 686	sdio_smem->buf = __va(shrd_mem->start);
 687	sdio_smem->size = shrd_mem->size;
 688	sdio_smem->cb_func = sdio_smem_cb;
 689	ret = sdio_smem_register_client();
 690	if (ret)
 691		pr_info("%s: Error (%d) registering sdio_smem client\n",
 692			__func__, ret);
 693	return ret;
 694}
 695
 696static struct platform_driver sdio_smem_drv = {
 697	.probe		= sdio_smem_probe,
 698	.driver		= {
 699		.name	= "SDIO_SMEM_CLIENT",
 700		.owner	= THIS_MODULE,
 701	},
 702};
 703#endif
 704
 705static int rmt_storage_event_alloc_rmt_buf_cb(
 706		struct rmt_storage_event *event_args,
 707		struct msm_rpc_xdr *xdr)
 708{
 709	struct rmt_storage_client *rs_client;
 710	struct rmt_shrd_mem_param *shrd_mem;
 711	uint32_t event_type, handle, size;
 712	int i;
 713#ifdef CONFIG_MSM_SDIO_SMEM
 714	int ret;
 715#endif
 716	xdr_recv_uint32(xdr, &event_type);
 717	if (event_type != RMT_STORAGE_EVNT_ALLOC_RMT_BUF)
 718		return -EINVAL;
 719
 720	pr_info("%s: Alloc rmt buf callback received\n", __func__);
 721	xdr_recv_uint32(xdr, &handle);
 722	xdr_recv_uint32(xdr, &size);
 723
 724	pr_debug("%s: handle=0x%x size=0x%x\n", __func__, handle, size);
 725
 726	rs_client = rmt_storage_get_client(handle);
 727	if (!rs_client) {
 728		pr_err("%s: Unable to find client for handle=%d\n",
 729		       __func__, handle);
 730		return -EINVAL;
 731	}
 732
 733	rs_client->sid = rmt_storage_get_sid(rs_client->path);
 734	if (!rs_client->sid) {
 735		pr_err("%s: No storage id found for %s\n",
 736		       __func__, rs_client->path);
 737		return -EINVAL;
 738	}
 739
 740	/* Check if another client has already allocated memory
 741	   for this sid */
 742	shrd_mem = rmt_storage_get_shrd_mem(rs_client->sid);
 743	if (shrd_mem)
 744		return (int) shrd_mem->start;
 745
 746	spin_lock(&rmc->lock);
 747	for (i = 0; i < MAX_SHRD_MEM_ENTRIES; i++)
 748		if (!rmc->rmt_shrd_mem[i].sid) {
 749			shrd_mem = &rmc->rmt_shrd_mem[i];
 750			shrd_mem->sid = 1; /* in-use marker */
 751			break;
 752		}
 753	spin_unlock(&rmc->lock);
 754
 755	if (i == MAX_SHRD_MEM_ENTRIES) {
 756		pr_err("%s: Unable to find shared mem entry\n", __func__);
 757		return -EINVAL;
 758	}
 759
 760	if (rs_client->srv->prog != MDM_RMT_STORAGE_APIPROG)
 761		shrd_mem->start = 0;
 762	else {
 763		shrd_mem->start = (uint32_t)kzalloc(size, GFP_KERNEL);
 764		if (!shrd_mem->start) {
 765			pr_err("%s: Unable to allocate memory\n", __func__);
 766			spin_lock(&rmc->lock);
 767			shrd_mem->sid = 0;
 768			spin_unlock(&rmc->lock);
 769			return -EINVAL;
 770		}
 771		shrd_mem->start = __pa(shrd_mem->start);
 772	}
 773	shrd_mem->sid = rs_client->sid;
 774	shrd_mem->size = size;
 775	pr_debug("%s: Allocated %d bytes at phys_addr=0x%x for handle=%d\n",
 776		__func__, shrd_mem->size, shrd_mem->start, rs_client->handle);
 777
 778#ifdef CONFIG_MSM_SDIO_SMEM
 779	ret = platform_driver_register(&sdio_smem_drv);
 780	if (ret)
 781		pr_err("%s: Unable to register sdio smem client\n", __func__);
 782#endif
 783	return (int) shrd_mem->start;
 784}
 785
 786static int handle_rmt_storage_call(struct msm_rpc_client *client,
 787				struct rpc_request_hdr *req,
 788				struct msm_rpc_xdr *xdr)
 789{
 790	int rc;
 791	uint32_t result = RMT_STORAGE_NO_ERROR;
 792	uint32_t rpc_status = RPC_ACCEPTSTAT_SUCCESS;
 793	struct rmt_storage_event *event_args;
 794	struct rmt_storage_kevent *kevent;
 795
 796	kevent = kzalloc(sizeof(struct rmt_storage_kevent), GFP_KERNEL);
 797	if (!kevent) {
 798		rpc_status = RPC_ACCEPTSTAT_SYSTEM_ERR;
 799		goto out;
 800	}
 801	event_args = &kevent->event;
 802
 803	switch (req->procedure) {
 804	case RMT_STORAGE_OPEN_CB_TYPE_PROC:
 805		/* client created in cb needs a ref. to its server */
 806		event_args->usr_data = client->prog;
 807		/* fall through */
 808
 809	case RMT_STORAGE_WRITE_IOVEC_CB_TYPE_PROC:
 810		/* fall through */
 811
 812	case RMT_STORAGE_READ_IOVEC_CB_TYPE_PROC:
 813		/* fall through */
 814
 815	case RMT_STORAGE_ALLOC_RMT_BUF_CB_TYPE_PROC:
 816		/* fall through */
 817
 818	case RMT_STORAGE_EVENT_CB_TYPE_PROC: {
 819		uint32_t cb_id;
 820		int (*cb_func)(struct rmt_storage_event *event_args,
 821				struct msm_rpc_xdr *xdr);
 822
 823		xdr_recv_uint32(xdr, &cb_id);
 824		cb_func = msm_rpc_get_cb_func(client, cb_id);
 825
 826		if (!cb_func) {
 827			rpc_status = RPC_ACCEPTSTAT_GARBAGE_ARGS;
 828			kfree(kevent);
 829			goto out;
 830		}
 831
 832		rc = cb_func(event_args, xdr);
 833		/* ALLOC_RMT_BUF returns an address, which when casted
 834		   to int could be negative */
 835		if (rc < 0) {
 836			if (req->procedure ==
 837			    RMT_STORAGE_ALLOC_RMT_BUF_CB_TYPE_PROC)
 838				break;
 839			pr_err("%s: Invalid parameters received\n", __func__);
 840			result = RMT_STORAGE_ERROR_PARAM;
 841			kfree(kevent);
 842			goto out;
 843		}
 844		result = (uint32_t) rc;
 845		break;
 846	}
 847
 848	default:
 849		kfree(kevent);
 850		pr_err("%s: unknown procedure %d\n", __func__, req->procedure);
 851		rpc_status = RPC_ACCEPTSTAT_PROC_UNAVAIL;
 852		goto out;
 853	}
 854
 855	if (req->procedure != RMT_STORAGE_ALLOC_RMT_BUF_CB_TYPE_PROC) {
 856		put_event(rmc, kevent);
 857		atomic_inc(&rmc->total_events);
 858		wake_up(&rmc->event_q);
 859	} else
 860		kfree(kevent);
 861
 862out:
 863	pr_debug("%s: Sending result=0x%x\n", __func__, result);
 864	xdr_start_accepted_reply(xdr, rpc_status);
 865	xdr_send_uint32(xdr, &result);
 866	rc = xdr_send_msg(xdr);
 867	if (rc)
 868		pr_err("%s: send accepted reply failed: %d\n", __func__, rc);
 869
 870	return rc;
 871}
 872
 873static int rmt_storage_open(struct inode *ip, struct file *fp)
 874{
 875	int i, ret = 0;
 876	struct shared_ramfs_entry *smem_info;
 877
 878	spin_lock(&rmc->lock);
 879	if (!rmc->open_excl) {
 880		rmc->open_excl = 1;
 881		for_each_smem_info(smem_info, i)
 882			smem_info->client_sts = 1;
 883	} else
 884		ret = -EBUSY;
 885	spin_unlock(&rmc->lock);
 886
 887	return ret;
 888}
 889
 890static int rmt_storage_release(struct inode *ip, struct file *fp)
 891{
 892	int i;
 893	struct shared_ramfs_entry *smem_info;
 894
 895	spin_lock(&rmc->lock);
 896	rmc->open_excl = 0;
 897	for_each_smem_info(smem_info, i)
 898			smem_info->client_sts = 0;
 899	spin_unlock(&rmc->lock);
 900
 901	return 0;
 902}
 903
 904static long rmt_storage_ioctl(struct file *fp, unsigned int cmd,
 905			    unsigned long arg)
 906{
 907	int ret = 0;
 908	struct rmt_storage_kevent *kevent;
 909	struct rmt_storage_send_sts status;
 910	static struct msm_rpc_client *rpc_client;
 911	struct rmt_shrd_mem_param usr_shrd_mem, *shrd_mem;
 912
 913#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
 914	struct rmt_storage_stats *stats;
 915	struct rmt_storage_op_stats *op_stats;
 916	ktime_t curr_stat;
 917#endif
 918
 919	switch (cmd) {
 920
 921	case RMT_STORAGE_SHRD_MEM_PARAM:
 922		pr_info("%s: get shared memory parameters ioctl\n", __func__);
 923		if (copy_from_user(&usr_shrd_mem, (void __user *)arg,
 924				sizeof(struct rmt_shrd_mem_param))) {
 925			pr_err("%s: copy from user failed\n\n", __func__);
 926			ret = -EFAULT;
 927			break;
 928		}
 929
 930		shrd_mem = rmt_storage_get_shrd_mem(usr_shrd_mem.sid);
 931		if (!shrd_mem) {
 932			pr_err("%s: invalid sid (0x%x)\n", __func__,
 933			       usr_shrd_mem.sid);
 934			ret = -EFAULT;
 935			break;
 936		}
 937
 938		if (copy_to_user((void __user *)arg, shrd_mem,
 939			sizeof(struct rmt_shrd_mem_param))) {
 940			pr_err("%s: copy to user failed\n\n", __func__);
 941			ret = -EFAULT;
 942		}
 943		break;
 944
 945	case RMT_STORAGE_WAIT_FOR_REQ:
 946		pr_info("%s: wait for request ioctl\n", __func__);
 947		if (atomic_read(&rmc->total_events) == 0) {
 948			ret = wait_event_interruptible(rmc->event_q,
 949				atomic_read(&rmc->total_events) != 0);
 950		}
 951		if (ret < 0)
 952			break;
 953		atomic_dec(&rmc->total_events);
 954
 955		kevent = get_event(rmc);
 956		WARN_ON(kevent == NULL);
 957		if (copy_to_user((void __user *)arg, &kevent->event,
 958			sizeof(struct rmt_storage_event))) {
 959			pr_err("%s: copy to user failed\n\n", __func__);
 960			ret = -EFAULT;
 961		}
 962		kfree(kevent);
 963		break;
 964
 965	case RMT_STORAGE_SEND_STATUS:
 966		pr_info("%s: send status ioctl\n", __func__);
 967		if (copy_from_user(&status, (void __user *)arg,
 968				sizeof(struct rmt_storage_send_sts))) {
 969			pr_err("%s: copy from user failed\n\n", __func__);
 970			ret = -EFAULT;
 971			if (atomic_dec_return(&rmc->wcount) == 0)
 972				wake_unlock(&rmc->wlock);
 973			break;
 974		}
 975#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
 976		stats = &client_stats[status.handle - 1];
 977		if (status.xfer_dir == RMT_STORAGE_WRITE)
 978			op_stats = &stats->wr_stats;
 979		else
 980			op_stats = &stats->rd_stats;
 981		curr_stat = ktime_sub(ktime_get(), op_stats->start);
 982		op_stats->total = ktime_add(op_stats->total, curr_stat);
 983		op_stats->count++;
 984		if (curr_stat.tv64 < stats->min.tv64)
 985			op_stats->min = curr_stat;
 986		if (curr_stat.tv64 > stats->max.tv64)
 987			op_stats->max = curr_stat;
 988#endif
 989		pr_debug("%s: \thandle=%d err_code=%d data=0x%x\n", __func__,
 990			status.handle, status.err_code, status.data);
 991		rpc_client = rmt_storage_get_rpc_client(status.handle);
 992		if (rpc_client)
 993			ret = msm_rpc_client_req2(rpc_client,
 994				RMT_STORAGE_OP_FINISH_PROC,
 995				rmt_storage_send_sts_arg,
 996				&status, NULL, NULL, -1);
 997		else
 998			ret = -EINVAL;
 999		if (ret < 0)
1000			pr_err("%s: send status failed with ret val = %d\n",
1001				__func__, ret);
1002		if (atomic_dec_return(&rmc->wcount) == 0)
1003			wake_unlock(&rmc->wlock);
1004		break;
1005
1006	default:
1007		ret = -EINVAL;
1008		break;
1009	}
1010
1011	return ret;
1012}
1013
1014struct rmt_storage_sync_recv_arg {
1015	int data;
1016};
1017
1018static int rmt_storage_receive_sync_arg(struct msm_rpc_client *client,
1019				struct msm_rpc_xdr *xdr, void *data)
1020{
1021	struct rmt_storage_sync_recv_arg *args = data;
1022	struct rmt_storage_srv *srv;
1023
1024	srv = rmt_storage_get_srv(client->prog);
1025	if (!srv)
1026		return -EINVAL;
1027	xdr_recv_int32(xdr, &args->data);
1028	srv->sync_token = args->data;
1029	return 0;
1030}
1031
1032static int rmt_storage_force_sync(struct msm_rpc_client *client)
1033{
1034	struct rmt_storage_sync_recv_arg args;
1035	int rc;
1036	rc = msm_rpc_client_req2(client,
1037			RMT_STORAGE_FORCE_SYNC_PROC, NULL, NULL,
1038			rmt_storage_receive_sync_arg, &args, -1);
1039	if (rc) {
1040		pr_err("%s: force sync RPC req failed: %d\n", __func__, rc);
1041		return rc;
1042	}
1043	return 0;
1044}
1045
1046struct rmt_storage_sync_sts_arg {
1047	int token;
1048};
1049
1050static int rmt_storage_send_sync_sts_arg(struct msm_rpc_client *client,
1051				struct msm_rpc_xdr *xdr, void *data)
1052{
1053	struct rmt_storage_sync_sts_arg *req = data;
1054
1055	xdr_send_int32(xdr, &req->token);
1056	return 0;
1057}
1058
1059static int rmt_storage_receive_sync_sts_arg(struct msm_rpc_client *client,
1060				struct msm_rpc_xdr *xdr, void *data)
1061{
1062	struct rmt_storage_sync_recv_arg *args = data;
1063
1064	xdr_recv_int32(xdr, &args->data);
1065	return 0;
1066}
1067
1068static int rmt_storage_get_sync_status(struct msm_rpc_client *client)
1069{
1070	struct rmt_storage_sync_recv_arg recv_args;
1071	struct rmt_storage_sync_sts_arg send_args;
1072	struct rmt_storage_srv *srv;
1073	int rc;
1074
1075	srv = rmt_storage_get_srv(client->prog);
1076	if (!srv)
1077		return -EINVAL;
1078
1079	if (srv->sync_token < 0)
1080		return -EINVAL;
1081
1082	send_args.token = srv->sync_token;
1083	rc = msm_rpc_client_req2(client,
1084			RMT_STORAGE_GET_SYNC_STATUS_PROC,
1085			rmt_storage_send_sync_sts_arg, &send_args,
1086			rmt_storage_receive_sync_sts_arg, &recv_args, -1);
1087	if (rc) {
1088		pr_err("%s: sync status RPC req failed: %d\n", __func__, rc);
1089		return rc;
1090	}
1091	return recv_args.data;
1092}
1093
1094static int rmt_storage_mmap(struct file *file, struct vm_area_struct *vma)
1095{
1096	unsigned long vsize = vma->vm_end - vma->vm_start;
1097	int ret = -EINVAL;
1098
1099	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1100
1101	ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1102				 vsize, vma->vm_page_prot);
1103	if (ret < 0)
1104		pr_err("%s: failed with return val %d\n", __func__, ret);
1105	return ret;
1106}
1107
1108struct rmt_storage_reg_cb_args {
1109	uint32_t event;
1110	uint32_t cb_id;
1111};
1112
1113static int rmt_storage_arg_cb(struct msm_rpc_client *client,
1114		struct msm_rpc_xdr *xdr, void *data)
1115{
1116	struct rmt_storage_reg_cb_args *args = data;
1117
1118	xdr_send_uint32(xdr, &args->event);
1119	xdr_send_uint32(xdr, &args->cb_id);
1120	return 0;
1121}
1122
1123static int rmt_storage_reg_cb(struct msm_rpc_client *client,
1124			      uint32_t proc, uint32_t event, void *callback)
1125{
1126	struct rmt_storage_reg_cb_args args;
1127	int rc, cb_id;
1128
1129	cb_id = msm_rpc_add_cb_func(client, callback);
1130	if ((cb_id < 0) && (cb_id != MSM_RPC_CLIENT_NULL_CB_ID))
1131		return cb_id;
1132
1133	args.event = event;
1134	args.cb_id = cb_id;
1135
1136	rc = msm_rpc_client_req2(client, proc, rmt_storage_arg_cb,
1137			&args, NULL, NULL, -1);
1138	if (rc)
1139		pr_err("%s: Failed to register callback for event %d\n",
1140				__func__, event);
1141	return rc;
1142}
1143
1144#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
1145static int rmt_storage_stats_open(struct inode *inode, struct file *file)
1146{
1147	return 0;
1148}
1149
1150static ssize_t rmt_storage_stats_read(struct file *file, char __user *ubuf,
1151		size_t count, loff_t *ppos)
1152{
1153	uint32_t tot_clients;
1154	char buf[512];
1155	int max, j, i = 0;
1156	struct rmt_storage_stats *stats;
1157
1158	max = sizeof(buf) - 1;
1159	tot_clients = find_first_zero_bit(&rmc->cids, sizeof(rmc->cids)) - 1;
1160
1161	for (j = 0; j < tot_clients; j++) {
1162		stats = &client_stats[j];
1163		i += scnprintf(buf + i, max - i, "stats for partition %s:\n",
1164				stats->path);
1165		i += scnprintf(buf + i, max - i, "Min read time: %lld us\n",
1166				ktime_to_us(stats->rd_stats.min));
1167		i += scnprintf(buf + i, max - i, "Max read time: %lld us\n",
1168				ktime_to_us(stats->rd_stats.max));
1169		i += scnprintf(buf + i, max - i, "Total read time: %lld us\n",
1170				ktime_to_us(stats->rd_stats.total));
1171		i += scnprintf(buf + i, max - i, "Total read requests: %ld\n",
1172				stats->rd_stats.count);
1173		if (stats->count)
1174			i += scnprintf(buf + i, max - i,
1175				"Avg read time: %lld us\n",
1176				div_s64(ktime_to_us(stats->total),
1177				stats->rd_stats.count));
1178
1179		i += scnprintf(buf + i, max - i, "Min write time: %lld us\n",
1180				ktime_to_us(stats->wr_stats.min));
1181		i += scnprintf(buf + i, max - i, "Max write time: %lld us\n",
1182				ktime_to_us(stats->wr_stats.max));
1183		i += scnprintf(buf + i, max - i, "Total write time: %lld us\n",
1184				ktime_to_us(stats->wr_stats.total));
1185		i += scnprintf(buf + i, max - i, "Total read requests: %ld\n",
1186				stats->wr_stats.count);
1187		if (stats->count)
1188			i += scnprintf(buf + i, max - i,
1189				"Avg write time: %lld us\n",
1190				div_s64(ktime_to_us(stats->total),
1191				stats->wr_stats.count));
1192	}
1193	return simple_read_from_buffer(ubuf, count, ppos, buf, i);
1194}
1195
1196static const struct file_operations debug_ops = {
1197	.owner = THIS_MODULE,
1198	.open = rmt_storage_stats_open,
1199	.read = rmt_storage_stats_read,
1200};
1201#endif
1202
1203const struct file_operations rmt_storage_fops = {
1204	.owner = THIS_MODULE,
1205	.open = rmt_storage_open,
1206	.unlocked_ioctl	 = rmt_storage_ioctl,
1207	.mmap = rmt_storage_mmap,
1208	.release = rmt_storage_release,
1209};
1210
1211static struct miscdevice rmt_storage_device = {
1212	.minor = MISC_DYNAMIC_MINOR,
1213	.name = "rmt_storage",
1214	.fops = &rmt_storage_fops,
1215};
1216
1217static int rmt_storage_get_ramfs(void)
1218{
1219	struct shared_ramfs_table *ramfs_table;
1220	struct shared_ramfs_entry *ramfs_entry;
1221	int index;
1222
1223	ramfs_table = smem_alloc(SMEM_SEFS_INFO,
1224			sizeof(struct shared_ramfs_table));
1225
1226	if (!ramfs_table) {
1227		pr_err("%s: No RAMFS table in SMEM\n", __func__);
1228		return -ENOENT;
1229	}
1230
1231	if ((ramfs_table->magic_id != (u32) RAMFS_INFO_MAGICNUMBER) ||
1232	    (ramfs_table->version != (u32) RAMFS_INFO_VERSION)) {
1233		pr_err("%s: Magic / Version mismatch:, "
1234		       "magic_id=%#x, format_version=%#x\n", __func__,
1235		       ramfs_table->magic_id, ramfs_table->version);
1236		return -ENOENT;
1237	}
1238
1239	for (index = 0; index < ramfs_table->entries; index++) {
1240		if (index == MAX_RAMFS_TBL_ENTRIES) {
1241			pr_err("%s: Unable to store more than %d entries\n",
1242				__func__, MAX_RAMFS_TBL_ENTRIES);
1243			break;
1244		}
1245		ramfs_entry = &ramfs_table->ramfs_entry[index];
1246		if (!ramfs_entry->client_id ||
1247		    ramfs_entry->client_id == (u32) RAMFS_DEFAULT)
1248			break;
1249		pr_info("%s: RAMFS entry: addr = 0x%08x, size = 0x%08x\n",
1250			__func__, ramfs_entry->base_addr, ramfs_entry->size);
1251
1252		rmc->rmt_shrd_mem[index].sid = ramfs_entry->client_id;
1253		rmc->rmt_shrd_mem[index].start = ramfs_entry->base_addr;
1254		rmc->rmt_shrd_mem[index].size = ramfs_entry->size;
1255		rmc->smem_info[index] = ramfs_entry;
1256
1257		if (rmc->open_excl == 1) {
1258			rmc->smem_info[index]->client_sts = 1;
1259			pr_debug("%s: setting client_sts to 1\n", __func__);
1260		}
1261	}
1262	return 0;
1263}
1264
1265static ssize_t
1266set_force_sync(struct device *dev, struct device_attribute *attr,
1267		const char *buf, size_t count)
1268{
1269	struct platform_device *pdev;
1270	struct rpcsvr_platform_device *rpc_pdev;
1271	struct rmt_storage_srv *srv;
1272	int value, rc;
1273
1274	pdev = container_of(dev, struct platform_device, dev);
1275	rpc_pdev = container_of(pdev, struct rpcsvr_platform_device, base);
1276	srv = rmt_storage_get_srv(rpc_pdev->prog);
1277	if (!srv) {
1278		pr_err("%s: Unable to find prog=0x%x\n", __func__,
1279		       rpc_pdev->prog);
1280		return -EINVAL;
1281	}
1282
1283	sscanf(buf, "%d", &value);
1284	if (!!value) {
1285		rc = rmt_storage_force_sync(srv->rpc_client);
1286		if (rc)
1287			return rc;
1288	}
1289	return count;
1290}
1291
1292/* Returns -EINVAL for invalid sync token and an error value for any failure
1293 * in RPC call. Upon success, it returns a sync status of 1 (sync done)
1294 * or 0 (sync still pending).
1295 */
1296static ssize_t
1297show_sync_sts(struct device *dev, struct device_attribute *attr, char *buf)
1298{
1299	struct platform_device *pdev;
1300	struct rpcsvr_platform_device *rpc_pdev;
1301	struct rmt_storage_srv *srv;
1302
1303	pdev = container_of(dev, struct platform_device, dev);
1304	rpc_pdev = container_of(pdev, struct rpcsvr_platform_device, base);
1305	srv = rmt_storage_get_srv(rpc_pdev->prog);
1306	if (!srv) {
1307		pr_err("%s: Unable to find prog=0x%x\n", __func__,
1308		       rpc_pdev->prog);
1309		return -EINVAL;
1310	}
1311	return snprintf(buf, PAGE_SIZE, "%d\n",
1312			rmt_storage_get_sync_status(srv->rpc_client));
1313}
1314
1315static int rmt_storage_init_ramfs(void)
1316{
1317	struct shared_ramfs_table *ramfs_table;
1318
1319	ramfs_table = smem_alloc(SMEM_SEFS_INFO,
1320				 sizeof(struct shared_ramfs_table));
1321
1322	if (!ramfs_table) {
1323		pr_err("%s: No RAMFS table in SMEM\n", __func__);
1324		return -ENOENT;
1325	}
1326
1327	if (ramfs_table->magic_id == RAMFS_INFO_MAGICNUMBER) {
1328		pr_debug("RAMFS table already filled... skipping %s", \
1329			__func__);
1330		return 0;
1331	}
1332
1333	ramfs_table->ramfs_entry[0].client_id  = RAMFS_MODEMSTORAGE_ID;
1334	ramfs_table->ramfs_entry[0].base_addr  = RAMFS_SHARED_EFS_RAM_BASE;
1335	ramfs_table->ramfs_entry[0].size       = RAMFS_SHARED_EFS_RAM_SIZE;
1336	ramfs_table->ramfs_entry[0].client_sts = RAMFS_DEFAULT;
1337
1338	ramfs_table->entries  = 1;
1339	ramfs_table->version  = RAMFS_INFO_VERSION;
1340	ramfs_table->magic_id = RAMFS_INFO_MAGICNUMBER;
1341
1342	return 0;
1343}
1344
1345static DEVICE_ATTR(force_sync, S_IRUGO | S_IWUSR, NULL, set_force_sync);
1346static DEVICE_ATTR(sync_sts, S_IRUGO | S_IWUSR, show_sync_sts, NULL);
1347static struct attribute *dev_attrs[] = {
1348	&dev_attr_force_sync.attr,
1349	&dev_attr_sync_sts.attr,
1350	NULL,
1351};
1352static struct attribute_group dev_attr_grp = {
1353	.attrs = dev_attrs,
1354};
1355
1356static int rmt_storage_probe(struct platform_device *pdev)
1357{
1358	struct rpcsvr_platform_device *dev;
1359	struct rmt_storage_srv *srv;
1360	int ret;
1361
1362	dev = container_of(pdev, struct rpcsvr_platform_device, base);
1363	srv = rmt_storage_get_srv(dev->prog);
1364	if (!srv) {
1365		pr_err("%s: Invalid prog = %#x\n", __func__, dev->prog);
1366		return -ENXIO;
1367	}
1368
1369	/* MDM will use ALLOC_RMT_BUR RPC call only */
1370	if (dev->prog == MSM_RMT_STORAGE_APIPROG) {
1371		rmt_storage_init_ramfs(); /* 8660 helper function */
1372		rmt_storage_get_ramfs();
1373	}
1374
1375	/* Client Registration */
1376	srv->rpc_client = msm_rpc_register_client2("rmt_storage",
1377						   dev->prog, dev->vers, 1,
1378						   handle_rmt_storage_call);
1379	if (IS_ERR(srv->rpc_client)) {
1380		pr_err("%s: Unable to register client (prog %.8x vers %.8x)\n",
1381				__func__, dev->prog, dev->vers);
1382		ret = PTR_ERR(srv->rpc_client);
1383		return ret;
1384	}
1385
1386	pr_info("%s: Remote storage RPC client (0x%x)initialized\n",
1387		__func__, dev->prog);
1388
1389	/* Register a callback for each event */
1390	ret = rmt_storage_reg_cb(srv->rpc_client,
1391				 RMT_STORAGE_REGISTER_OPEN_PROC,
1392				 RMT_STORAGE_EVNT_OPEN,
1393				 rmt_storage_event_open_cb);
1394
1395	if (ret)
1396		goto unregister_client;
1397
1398	ret = rmt_storage_reg_cb(srv->rpc_client,
1399				 RMT_STORAGE_REGISTER_CB_PROC,
1400				 RMT_STORAGE_EVNT_CLOSE,
1401				 rmt_storage_event_close_cb);
1402
1403	if (ret)
1404		goto unregister_client;
1405
1406	ret = rmt_storage_reg_cb(srv->rpc_client,
1407				 RMT_STORAGE_REGISTER_CB_PROC,
1408				 RMT_STORAGE_EVNT_WRITE_BLOCK,
1409				 rmt_storage_event_write_block_cb);
1410
1411	if (ret)
1412		goto unregister_client;
1413
1414	ret = rmt_storage_reg_cb(srv->rpc_client,
1415				 RMT_STORAGE_REGISTER_CB_PROC,
1416				 RMT_STORAGE_EVNT_GET_DEV_ERROR,
1417				 rmt_storage_event_get_err_cb);
1418
1419	if (ret)
1420		goto unregister_client;
1421
1422	ret = rmt_storage_reg_cb(srv->rpc_client,
1423				 RMT_STORAGE_REGISTER_WRITE_IOVEC_PROC,
1424				 RMT_STORAGE_EVNT_WRITE_IOVEC,
1425				 rmt_storage_event_write_iovec_cb);
1426
1427	if (ret)
1428		goto unregister_client;
1429
1430	ret = rmt_storage_reg_cb(srv->rpc_client,
1431				 RMT_STORAGE_REGISTER_READ_IOVEC_PROC,
1432				 RMT_STORAGE_EVNT_READ_IOVEC,
1433				 rmt_storage_event_read_iovec_cb);
1434
1435	if (ret)
1436		pr_err("%s: unable to register read iovec callback %d\n",
1437			__func__, ret);
1438
1439	ret = rmt_storage_reg_cb(srv->rpc_client,
1440				 RMT_STORAGE_REGISTER_CB_PROC,
1441				 RMT_STORAGE_EVNT_SEND_USER_DATA,
1442				 rmt_storage_event_user_data_cb);
1443
1444	if (ret)
1445		goto unregister_client;
1446
1447	ret = rmt_storage_reg_cb(srv->rpc_client,
1448				 RMT_STORAGE_REGISTER_ALLOC_RMT_BUF_PROC,
1449				 RMT_STORAGE_EVNT_ALLOC_RMT_BUF,
1450				 rmt_storage_event_alloc_rmt_buf_cb);
1451
1452	if (ret)
1453		pr_info("%s: unable to register alloc rmt buf callback %d\n",
1454			__func__, ret);
1455
1456	ret = sysfs_create_group(&pdev->dev.kobj, &dev_attr_grp);
1457	if (ret)
1458		pr_err("%s: Failed to create sysfs node: %d\n", __func__, ret);
1459
1460	return 0;
1461
1462unregister_client:
1463	msm_rpc_unregister_client(srv->rpc_client);
1464	return ret;
1465}
1466
1467static void rmt_storage_client_shutdown(struct platform_device *pdev)
1468{
1469	struct rpcsvr_platform_device *dev;
1470	struct rmt_storage_client *rs_client;
1471	struct shared_ramfs_entry *smem_info;
1472
1473	dev = container_of(pdev, struct rpcsvr_platform_device, base);
1474
1475	spin_lock(&rmc->lock);
1476	list_for_each_entry(rs_client, &rmc->client_list, list)
1477		if (rs_client->srv->prog == dev->prog) {
1478			smem_info = rmt_storage_get_smem_info(rs_client->sid);
1479			if (smem_info)
1480				smem_info->client_sts = 0;
1481		}
1482	spin_unlock(&rmc->lock);
1483}
1484
1485static void rmt_storage_destroy_rmc(void)
1486{
1487	wake_lock_destroy(&rmc->wlock);
1488}
1489
1490static void __init rmt_storage_init_client_info(void)
1491{
1492	/* Initialization */
1493	init_waitqueue_head(&rmc->event_q);
1494	spin_lock_init(&rmc->lock);
1495	atomic_set(&rmc->total_events, 0);
1496	INIT_LIST_HEAD(&rmc->event_list);
1497	INIT_LIST_HEAD(&rmc->client_list);
1498	/* The client expects a non-zero return value for
1499	 * its open requests. Hence reserve 0 bit.  */
1500	__set_bit(0, &rmc->cids);
1501	atomic_set(&rmc->wcount, 0);
1502	wake_lock_init(&rmc->wlock, WAKE_LOCK_SUSPEND, "rmt_storage");
1503}
1504
1505static struct rmt_storage_srv msm_srv = {
1506	.prog = MSM_RMT_STORAGE_APIPROG,
1507	.plat_drv = {
1508		.probe	  = rmt_storage_probe,
1509		.shutdown = rmt_storage_client_shutdown,
1510		.driver	  = {
1511			.name	= "rs300000a7",
1512			.owner	= THIS_MODULE,
1513		},
1514	},
1515};
1516
1517static struct rmt_storage_srv mdm_srv = {
1518	.prog = MDM_RMT_STORAGE_APIPROG,
1519	.plat_drv = {
1520		.probe	  = rmt_storage_probe,
1521		.shutdown = rmt_storage_client_shutdown,
1522		.driver	  = {
1523			.name	= "rs300100a7",
1524			.owner	= THIS_MODULE,
1525		},
1526	},
1527};
1528
1529static struct rmt_storage_srv *rmt_storage_get_srv(uint32_t prog)
1530{
1531	if (prog == MSM_RMT_STORAGE_APIPROG)
1532		return &msm_srv;
1533	if (prog == MDM_RMT_STORAGE_APIPROG)
1534		return &mdm_srv;
1535	return NULL;
1536}
1537
1538
1539static uint32_t rmt_storage_get_sid(const char *path)
1540{
1541	if (!strncmp(path, "/boot/modem_fs1", MAX_PATH_NAME))
1542		return RAMFS_MODEMSTORAGE_ID;
1543	if (!strncmp(path, "/boot/modem_fs2", MAX_PATH_NAME))
1544		return RAMFS_MODEMSTORAGE_ID;
1545	if (!strncmp(path, "/boot/modem_fsg", MAX_PATH_NAME))
1546		return RAMFS_MODEMSTORAGE_ID;
1547	if (!strncmp(path, "/q6_fs1_parti_id_0x59", MAX_PATH_NAME))
1548		return RAMFS_MDM_STORAGE_ID;
1549	if (!strncmp(path, "/q6_fs2_parti_id_0x5A", MAX_PATH_NAME))
1550		return RAMFS_MDM_STORAGE_ID;
1551	if (!strncmp(path, "/q6_fsg_parti_id_0x5B", MAX_PATH_NAME))
1552		return RAMFS_MDM_STORAGE_ID;
1553	return 0;
1554}
1555
1556static int __init rmt_storage_init(void)
1557{
1558	int ret = 0;
1559
1560	rmc = kzalloc(sizeof(struct rmt_storage_client_info), GFP_KERNEL);
1561	if (!rmc) {
1562		pr_err("%s: Unable to allocate memory\n", __func__);
1563		return  -ENOMEM;
1564	}
1565	rmt_storage_init_client_info();
1566
1567	ret = platform_driver_register(&msm_srv.plat_drv);
1568	if (ret) {
1569		pr_err("%s: Unable to register MSM RPC driver\n", __func__);
1570		goto rmc_free;
1571	}
1572
1573	ret = platform_driver_register(&mdm_srv.plat_drv);
1574	if (ret) {
1575		pr_err("%s: Unable to register MDM RPC driver\n", __func__);
1576		goto unreg_msm_rpc;
1577	}
1578
1579	ret = misc_register(&rmt_storage_device);
1580	if (ret) {
1581		pr_err("%s: Unable to register misc device %d\n", __func__,
1582				MISC_DYNAMIC_MINOR);
1583		goto unreg_mdm_rpc;
1584	}
1585
1586#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
1587	stats_dentry = debugfs_create_file("rmt_storage_stats", 0444, 0,
1588					NULL, &debug_ops);
1589	if (!stats_dentry)
1590		pr_err("%s: Failed to create stats debugfs file\n", __func__);
1591#endif
1592	return 0;
1593
1594unreg_mdm_rpc:
1595	platform_driver_unregister(&mdm_srv.plat_drv);
1596unreg_msm_rpc:
1597	platform_driver_unregister(&msm_srv.plat_drv);
1598rmc_free:
1599	rmt_storage_destroy_rmc();
1600	kfree(rmc);
1601	return ret;
1602}
1603
1604module_init(rmt_storage_init);
1605MODULE_DESCRIPTION("Remote Storage RPC Client");
1606MODULE_LICENSE("GPL v2");