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