/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

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