/
rmt_storage_client.c
1601 lines (1355 loc) · 41.2 KB
/
rmt_storage_client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
*/
#include <linux/miscdevice.h>
#include <linux/wait.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/err.h>
#include <linux/sched.h>
#include <linux/wakelock.h>
#include <linux/rmt_storage_client.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <asm/pgtable.h>
#include <mach/msm_rpcrouter.h>
#ifdef CONFIG_MSM_SDIO_SMEM
#include <mach/sdio_smem.h>
#endif
#include "smd_private.h"
enum {
RMT_STORAGE_EVNT_OPEN = 0,
RMT_STORAGE_EVNT_CLOSE,
RMT_STORAGE_EVNT_WRITE_BLOCK,
RMT_STORAGE_EVNT_GET_DEV_ERROR,
RMT_STORAGE_EVNT_WRITE_IOVEC,
RMT_STORAGE_EVNT_SEND_USER_DATA,
RMT_STORAGE_EVNT_READ_IOVEC,
RMT_STORAGE_EVNT_ALLOC_RMT_BUF,
} rmt_storage_event;
struct shared_ramfs_entry {
uint32_t client_id; /* Client id to uniquely identify a client */
uint32_t base_addr; /* Base address of shared RAMFS memory */
uint32_t size; /* Size of the shared RAMFS memory */
uint32_t client_sts; /* This will be initialized to 1 when
remote storage RPC client is ready
to process requests */
};
struct shared_ramfs_table {
uint32_t magic_id; /* Identify RAMFS details in SMEM */
uint32_t version; /* Version of shared_ramfs_table */
uint32_t entries; /* Total number of valid entries */
/* List all entries */
struct shared_ramfs_entry ramfs_entry[MAX_RAMFS_TBL_ENTRIES];
};
struct rmt_storage_client_info {
unsigned long cids;
struct list_head shrd_mem_list; /* List of shared memory entries */
int open_excl;
atomic_t total_events;
wait_queue_head_t event_q;
struct list_head event_list;
struct list_head client_list; /* List of remote storage clients */
/* Lock to protect lists */
spinlock_t lock;
/* Wakelock to be acquired when processing requests from modem */
struct wake_lock wlock;
atomic_t wcount;
};
struct rmt_storage_kevent {
struct list_head list;
struct rmt_storage_event event;
};
/* Remote storage server on modem */
struct rmt_storage_srv {
uint32_t prog;
int sync_token;
struct platform_driver plat_drv;
struct msm_rpc_client *rpc_client;
};
/* Remote storage client on modem */
struct rmt_storage_client {
uint32_t handle;
uint32_t sid; /* Storage ID */
char path[MAX_PATH_NAME];
struct rmt_storage_srv *srv;
struct list_head list;
};
struct rmt_shrd_mem {
struct list_head list;
struct rmt_shrd_mem_param param;
struct shared_ramfs_entry *smem_info;
struct rmt_storage_srv *srv;
};
static struct rmt_storage_srv *rmt_storage_get_srv(uint32_t prog);
static uint32_t rmt_storage_get_sid(const char *path);
static struct rmt_storage_client_info *rmc;
#ifdef CONFIG_MSM_SDIO_SMEM
static struct sdio_smem_client *sdio_smem;
#endif
#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
struct rmt_storage_op_stats {
unsigned long count;
ktime_t start;
ktime_t min;
ktime_t max;
ktime_t total;
};
struct rmt_storage_stats {
char path[MAX_PATH_NAME];
struct rmt_storage_op_stats rd_stats;
struct rmt_storage_op_stats wr_stats;
};
static struct rmt_storage_stats client_stats[MAX_NUM_CLIENTS];
static struct dentry *stats_dentry;
#endif
#define MSM_RMT_STORAGE_APIPROG 0x300000A7
#define MDM_RMT_STORAGE_APIPROG 0x300100A7
#define RMT_STORAGE_OP_FINISH_PROC 2
#define RMT_STORAGE_REGISTER_OPEN_PROC 3
#define RMT_STORAGE_REGISTER_WRITE_IOVEC_PROC 4
#define RMT_STORAGE_REGISTER_CB_PROC 5
#define RMT_STORAGE_UN_REGISTER_CB_PROC 6
#define RMT_STORAGE_FORCE_SYNC_PROC 7
#define RMT_STORAGE_GET_SYNC_STATUS_PROC 8
#define RMT_STORAGE_REGISTER_READ_IOVEC_PROC 9
#define RMT_STORAGE_REGISTER_ALLOC_RMT_BUF_PROC 10
#define RMT_STORAGE_OPEN_CB_TYPE_PROC 1
#define RMT_STORAGE_WRITE_IOVEC_CB_TYPE_PROC 2
#define RMT_STORAGE_EVENT_CB_TYPE_PROC 3
#define RMT_STORAGE_READ_IOVEC_CB_TYPE_PROC 4
#define RMT_STORAGE_ALLOC_RMT_BUF_CB_TYPE_PROC 5
#define RAMFS_INFO_MAGICNUMBER 0x654D4D43
#define RAMFS_INFO_VERSION 0x00000001
#define RAMFS_DEFAULT 0xFFFFFFFF
/* MSM EFS*/
#define RAMFS_MODEMSTORAGE_ID 0x4D454653
#define RAMFS_SHARED_EFS_RAM_BASE 0x46100000
#define RAMFS_SHARED_EFS_RAM_SIZE (3 * 1024 * 1024)
/* MDM EFS*/
#define RAMFS_MDM_STORAGE_ID 0x4D4583A1
static struct rmt_storage_client *rmt_storage_get_client(uint32_t handle)
{
struct rmt_storage_client *rs_client;
list_for_each_entry(rs_client, &rmc->client_list, list)
if (rs_client->handle == handle)
return rs_client;
return NULL;
}
static struct rmt_shrd_mem_param *rmt_storage_get_shrd_mem(uint32_t sid)
{
struct rmt_shrd_mem *shrd_mem;
struct rmt_shrd_mem_param *shrd_mem_param = NULL;
spin_lock(&rmc->lock);
list_for_each_entry(shrd_mem, &rmc->shrd_mem_list, list)
if (shrd_mem->param.sid == sid)
shrd_mem_param = &shrd_mem->param;
spin_unlock(&rmc->lock);
return shrd_mem_param;
}
static int rmt_storage_add_shrd_mem(uint32_t sid, uint32_t start,
uint32_t size, void *base,
struct shared_ramfs_entry *smem_info,
struct rmt_storage_srv *srv)
{
struct rmt_shrd_mem *shrd_mem;
shrd_mem = kzalloc(sizeof(struct rmt_shrd_mem), GFP_KERNEL);
if (!shrd_mem)
return -ENOMEM;
shrd_mem->param.sid = sid;
shrd_mem->param.start = start;
shrd_mem->param.size = size;
shrd_mem->param.base = base;
shrd_mem->smem_info = smem_info;
shrd_mem->srv = srv;
spin_lock(&rmc->lock);
list_add(&shrd_mem->list, &rmc->shrd_mem_list);
spin_unlock(&rmc->lock);
return 0;
}
static struct msm_rpc_client *rmt_storage_get_rpc_client(uint32_t handle)
{
struct rmt_storage_client *rs_client;
rs_client = rmt_storage_get_client(handle);
if (!rs_client)
return NULL;
return rs_client->srv->rpc_client;
}
static int rmt_storage_validate_iovec(uint32_t handle,
struct rmt_storage_iovec_desc *xfer)
{
struct rmt_storage_client *rs_client;
struct rmt_shrd_mem_param *shrd_mem;
rs_client = rmt_storage_get_client(handle);
if (!rs_client)
return -EINVAL;
shrd_mem = rmt_storage_get_shrd_mem(rs_client->sid);
if (!shrd_mem)
return -EINVAL;
if ((xfer->data_phy_addr < shrd_mem->start) ||
((xfer->data_phy_addr + RAMFS_BLOCK_SIZE * xfer->num_sector) >
(shrd_mem->start + shrd_mem->size)))
return -EINVAL;
return 0;
}
static int rmt_storage_send_sts_arg(struct msm_rpc_client *client,
struct msm_rpc_xdr *xdr, void *data)
{
struct rmt_storage_send_sts *args = data;
xdr_send_uint32(xdr, &args->handle);
xdr_send_uint32(xdr, &args->err_code);
xdr_send_uint32(xdr, &args->data);
return 0;
}
static void put_event(struct rmt_storage_client_info *rmc,
struct rmt_storage_kevent *kevent)
{
spin_lock(&rmc->lock);
list_add_tail(&kevent->list, &rmc->event_list);
spin_unlock(&rmc->lock);
}
static struct rmt_storage_kevent *get_event(struct rmt_storage_client_info *rmc)
{
struct rmt_storage_kevent *kevent = NULL;
spin_lock(&rmc->lock);
if (!list_empty(&rmc->event_list)) {
kevent = list_first_entry(&rmc->event_list,
struct rmt_storage_kevent, list);
list_del(&kevent->list);
}
spin_unlock(&rmc->lock);
return kevent;
}
static int rmt_storage_event_open_cb(struct rmt_storage_event *event_args,
struct msm_rpc_xdr *xdr)
{
uint32_t cid, len, event_type;
char *path;
int ret;
struct rmt_storage_srv *srv;
struct rmt_storage_client *rs_client;
#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
struct rmt_storage_stats *stats;
#endif
srv = rmt_storage_get_srv(event_args->usr_data);
if (!srv)
return -EINVAL;
xdr_recv_uint32(xdr, &event_type);
if (event_type != RMT_STORAGE_EVNT_OPEN)
return -1;
pr_info("%s: open callback received\n", __func__);
rs_client = kzalloc(sizeof(struct rmt_storage_client), GFP_KERNEL);
if (!rs_client) {
pr_err("%s: Error allocating rmt storage client\n", __func__);
return -ENOMEM;
}
ret = xdr_recv_bytes(xdr, (void **)&path, &len);
if (ret || !path) {
pr_err("%s: Invalid path\n", __func__);
if (!ret)
ret = -1;
goto free_rs_client;
}
memcpy(event_args->path, path, len);
rs_client->sid = rmt_storage_get_sid(event_args->path);
if (!rs_client->sid) {
pr_err("%s: No storage id found for %s\n", __func__,
event_args->path);
ret = -EINVAL;
goto free_path;
}
strncpy(rs_client->path, event_args->path, MAX_PATH_NAME);
cid = find_first_zero_bit(&rmc->cids, sizeof(rmc->cids) * 8);
if (cid > MAX_NUM_CLIENTS) {
pr_err("%s: Max clients are reached\n", __func__);
cid = 0;
return cid;
}
__set_bit(cid, &rmc->cids);
pr_info("open partition %s handle=%d\n", event_args->path, cid);
#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
stats = &client_stats[cid - 1];
memcpy(stats->path, event_args->path, len);
memset(stats->rd_stats, 0, sizeof(struct rmt_storage_op_stats));
memset(stats->wr_stats, 0, sizeof(struct rmt_storage_op_stats));
stats->rd_stats.min.tv64 = KTIME_MAX;
stats->wr_stats.min.tv64 = KTIME_MAX;
#endif
event_args->id = RMT_STORAGE_OPEN;
event_args->sid = rs_client->sid;
event_args->handle = cid;
rs_client->handle = event_args->handle;
rs_client->srv = srv;
INIT_LIST_HEAD(&rs_client->list);
spin_lock(&rmc->lock);
list_add_tail(&rs_client->list, &rmc->client_list);
spin_unlock(&rmc->lock);
kfree(path);
return cid;
free_path:
kfree(path);
free_rs_client:
kfree(rs_client);
return ret;
}
struct rmt_storage_close_args {
uint32_t handle;
};
struct rmt_storage_rw_block_args {
uint32_t handle;
uint32_t data_phy_addr;
uint32_t sector_addr;
uint32_t num_sector;
};
struct rmt_storage_get_err_args {
uint32_t handle;
};
struct rmt_storage_user_data_args {
uint32_t handle;
uint32_t data;
};
struct rmt_storage_event_params {
uint32_t type;
union {
struct rmt_storage_close_args close;
struct rmt_storage_rw_block_args block;
struct rmt_storage_get_err_args get_err;
struct rmt_storage_user_data_args user_data;
} params;
};
static int rmt_storage_parse_params(struct msm_rpc_xdr *xdr,
struct rmt_storage_event_params *event)
{
xdr_recv_uint32(xdr, &event->type);
switch (event->type) {
case RMT_STORAGE_EVNT_CLOSE: {
struct rmt_storage_close_args *args;
args = &event->params.close;
xdr_recv_uint32(xdr, &args->handle);
break;
}
case RMT_STORAGE_EVNT_WRITE_BLOCK: {
struct rmt_storage_rw_block_args *args;
args = &event->params.block;
xdr_recv_uint32(xdr, &args->handle);
xdr_recv_uint32(xdr, &args->data_phy_addr);
xdr_recv_uint32(xdr, &args->sector_addr);
xdr_recv_uint32(xdr, &args->num_sector);
break;
}
case RMT_STORAGE_EVNT_GET_DEV_ERROR: {
struct rmt_storage_get_err_args *args;
args = &event->params.get_err;
xdr_recv_uint32(xdr, &args->handle);
break;
}
case RMT_STORAGE_EVNT_SEND_USER_DATA: {
struct rmt_storage_user_data_args *args;
args = &event->params.user_data;
xdr_recv_uint32(xdr, &args->handle);
xdr_recv_uint32(xdr, &args->data);
break;
}
default:
pr_err("%s: unknown event %d\n", __func__, event->type);
return -1;
}
return 0;
}
static int rmt_storage_event_close_cb(struct rmt_storage_event *event_args,
struct msm_rpc_xdr *xdr)
{
struct rmt_storage_event_params *event;
struct rmt_storage_close_args *close;
struct rmt_storage_client *rs_client;
uint32_t event_type;
int ret;
xdr_recv_uint32(xdr, &event_type);
if (event_type != RMT_STORAGE_EVNT_CLOSE)
return -1;
pr_info("%s: close callback received\n", __func__);
ret = xdr_recv_pointer(xdr, (void **)&event,
sizeof(struct rmt_storage_event_params),
rmt_storage_parse_params);
if (ret || !event)
return -1;
close = &event->params.close;
event_args->handle = close->handle;
event_args->id = RMT_STORAGE_CLOSE;
__clear_bit(event_args->handle, &rmc->cids);
rs_client = rmt_storage_get_client(event_args->handle);
if (rs_client) {
list_del(&rs_client->list);
kfree(rs_client);
}
kfree(event);
return RMT_STORAGE_NO_ERROR;
}
static int rmt_storage_event_write_block_cb(
struct rmt_storage_event *event_args,
struct msm_rpc_xdr *xdr)
{
struct rmt_storage_event_params *event;
struct rmt_storage_rw_block_args *write_block;
struct rmt_storage_iovec_desc *xfer;
uint32_t event_type;
int ret;
xdr_recv_uint32(xdr, &event_type);
if (event_type != RMT_STORAGE_EVNT_WRITE_BLOCK)
return -1;
pr_info("%s: write block callback received\n", __func__);
ret = xdr_recv_pointer(xdr, (void **)&event,
sizeof(struct rmt_storage_event_params),
rmt_storage_parse_params);
if (ret || !event)
return -1;
write_block = &event->params.block;
event_args->handle = write_block->handle;
xfer = &event_args->xfer_desc[0];
xfer->sector_addr = write_block->sector_addr;
xfer->data_phy_addr = write_block->data_phy_addr;
xfer->num_sector = write_block->num_sector;
ret = rmt_storage_validate_iovec(event_args->handle, xfer);
if (ret)
return -1;
event_args->xfer_cnt = 1;
event_args->id = RMT_STORAGE_WRITE;
if (atomic_inc_return(&rmc->wcount) == 1)
wake_lock(&rmc->wlock);
pr_debug("sec_addr = %u, data_addr = %x, num_sec = %d\n\n",
xfer->sector_addr, xfer->data_phy_addr,
xfer->num_sector);
kfree(event);
return RMT_STORAGE_NO_ERROR;
}
static int rmt_storage_event_get_err_cb(struct rmt_storage_event *event_args,
struct msm_rpc_xdr *xdr)
{
struct rmt_storage_event_params *event;
struct rmt_storage_get_err_args *get_err;
uint32_t event_type;
int ret;
xdr_recv_uint32(xdr, &event_type);
if (event_type != RMT_STORAGE_EVNT_GET_DEV_ERROR)
return -1;
pr_info("%s: get err callback received\n", __func__);
ret = xdr_recv_pointer(xdr, (void **)&event,
sizeof(struct rmt_storage_event_params),
rmt_storage_parse_params);
if (ret || !event)
return -1;
get_err = &event->params.get_err;
event_args->handle = get_err->handle;
kfree(event);
/* Not implemented */
return -1;
}
static int rmt_storage_event_user_data_cb(struct rmt_storage_event *event_args,
struct msm_rpc_xdr *xdr)
{
struct rmt_storage_event_params *event;
struct rmt_storage_user_data_args *user_data;
uint32_t event_type;
int ret;
xdr_recv_uint32(xdr, &event_type);
if (event_type != RMT_STORAGE_EVNT_SEND_USER_DATA)
return -1;
pr_info("%s: send user data callback received\n", __func__);
ret = xdr_recv_pointer(xdr, (void **)&event,
sizeof(struct rmt_storage_event_params),
rmt_storage_parse_params);
if (ret || !event)
return -1;
user_data = &event->params.user_data;
event_args->handle = user_data->handle;
event_args->usr_data = user_data->data;
event_args->id = RMT_STORAGE_SEND_USER_DATA;
kfree(event);
return RMT_STORAGE_NO_ERROR;
}
static int rmt_storage_event_write_iovec_cb(
struct rmt_storage_event *event_args,
struct msm_rpc_xdr *xdr)
{
struct rmt_storage_iovec_desc *xfer;
uint32_t i, ent, event_type;
#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
struct rmt_storage_stats *stats;
#endif
xdr_recv_uint32(xdr, &event_type);
if (event_type != RMT_STORAGE_EVNT_WRITE_IOVEC)
return -EINVAL;
pr_info("%s: write iovec callback received\n", __func__);
xdr_recv_uint32(xdr, &event_args->handle);
xdr_recv_uint32(xdr, &ent);
pr_debug("handle = %d\n", event_args->handle);
#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
stats = &client_stats[event_args->handle - 1];
stats->wr_stats.start = ktime_get();
#endif
for (i = 0; i < ent; i++) {
xfer = &event_args->xfer_desc[i];
xdr_recv_uint32(xdr, &xfer->sector_addr);
xdr_recv_uint32(xdr, &xfer->data_phy_addr);
xdr_recv_uint32(xdr, &xfer->num_sector);
if (rmt_storage_validate_iovec(event_args->handle, xfer))
return -EINVAL;
pr_debug("sec_addr = %u, data_addr = %x, num_sec = %d\n",
xfer->sector_addr, xfer->data_phy_addr,
xfer->num_sector);
}
xdr_recv_uint32(xdr, &event_args->xfer_cnt);
event_args->id = RMT_STORAGE_WRITE;
if (atomic_inc_return(&rmc->wcount) == 1)
wake_lock(&rmc->wlock);
pr_debug("iovec transfer count = %d\n\n", event_args->xfer_cnt);
return RMT_STORAGE_NO_ERROR;
}
static int rmt_storage_event_read_iovec_cb(
struct rmt_storage_event *event_args,
struct msm_rpc_xdr *xdr)
{
struct rmt_storage_iovec_desc *xfer;
uint32_t i, ent, event_type;
#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
struct rmt_storage_stats *stats;
#endif
xdr_recv_uint32(xdr, &event_type);
if (event_type != RMT_STORAGE_EVNT_READ_IOVEC)
return -EINVAL;
pr_info("%s: read iovec callback received\n", __func__);
xdr_recv_uint32(xdr, &event_args->handle);
xdr_recv_uint32(xdr, &ent);
pr_debug("handle = %d\n", event_args->handle);
#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
stats = &client_stats[event_args->handle - 1];
stats->rd_stats.start = ktime_get();
#endif
for (i = 0; i < ent; i++) {
xfer = &event_args->xfer_desc[i];
xdr_recv_uint32(xdr, &xfer->sector_addr);
xdr_recv_uint32(xdr, &xfer->data_phy_addr);
xdr_recv_uint32(xdr, &xfer->num_sector);
if (rmt_storage_validate_iovec(event_args->handle, xfer))
return -EINVAL;
pr_debug("sec_addr = %u, data_addr = %x, num_sec = %d\n",
xfer->sector_addr, xfer->data_phy_addr,
xfer->num_sector);
}
xdr_recv_uint32(xdr, &event_args->xfer_cnt);
event_args->id = RMT_STORAGE_READ;
if (atomic_inc_return(&rmc->wcount) == 1)
wake_lock(&rmc->wlock);
pr_debug("iovec transfer count = %d\n\n", event_args->xfer_cnt);
return RMT_STORAGE_NO_ERROR;
}
#ifdef CONFIG_MSM_SDIO_SMEM
static int sdio_smem_cb(int event)
{
pr_debug("%s: Received event %d\n", __func__, event);
switch (event) {
case SDIO_SMEM_EVENT_READ_DONE:
pr_debug("Read done\n");
break;
case SDIO_SMEM_EVENT_READ_ERR:
pr_err("Read overflow\n");
return -EIO;
default:
pr_err("Unhandled event\n");
}
return 0;
}
static int sdio_smem_probe(struct platform_device *pdev)
{
int ret = 0;
struct rmt_shrd_mem_param *shrd_mem;
sdio_smem = container_of(pdev, struct sdio_smem_client, plat_dev);
/* SDIO SMEM is supported only for MDM */
shrd_mem = rmt_storage_get_shrd_mem(RAMFS_MDM_STORAGE_ID);
if (!shrd_mem) {
pr_err("%s: No shared mem entry for sid=0x%08x\n",
__func__, (uint32_t)RAMFS_MDM_STORAGE_ID);
return -ENOMEM;
}
sdio_smem->buf = __va(shrd_mem->start);
sdio_smem->size = shrd_mem->size;
sdio_smem->cb_func = sdio_smem_cb;
ret = sdio_smem_register_client();
if (ret)
pr_info("%s: Error (%d) registering sdio_smem client\n",
__func__, ret);
return ret;
}
static struct platform_driver sdio_smem_drv = {
.probe = sdio_smem_probe,
.driver = {
.name = "SDIO_SMEM_CLIENT",
.owner = THIS_MODULE,
},
};
#endif
static int rmt_storage_event_alloc_rmt_buf_cb(
struct rmt_storage_event *event_args,
struct msm_rpc_xdr *xdr)
{
struct rmt_storage_client *rs_client;
struct rmt_shrd_mem_param *shrd_mem;
uint32_t event_type, handle, size, start, vstart;
int ret;
xdr_recv_uint32(xdr, &event_type);
if (event_type != RMT_STORAGE_EVNT_ALLOC_RMT_BUF)
return -EINVAL;
pr_info("%s: Alloc rmt buf callback received\n", __func__);
xdr_recv_uint32(xdr, &handle);
xdr_recv_uint32(xdr, &size);
pr_debug("%s: handle=0x%x size=0x%x\n", __func__, handle, size);
rs_client = rmt_storage_get_client(handle);
if (!rs_client) {
pr_err("%s: Unable to find client for handle=%d\n",
__func__, handle);
return -EINVAL;
}
rs_client->sid = rmt_storage_get_sid(rs_client->path);
if (!rs_client->sid) {
pr_err("%s: No storage id found for %s\n",
__func__, rs_client->path);
return -EINVAL;
}
/* Check if another client has already allocated memory
for this sid */
shrd_mem = rmt_storage_get_shrd_mem(rs_client->sid);
if (shrd_mem)
return (int) shrd_mem->start;
/* Allocate memory from heap for MDM only */
if (rs_client->srv->prog != MDM_RMT_STORAGE_APIPROG)
return -EINVAL;
vstart = (uint32_t)kzalloc(size, GFP_KERNEL);
if (!vstart)
return -ENOMEM;
start = __pa(vstart);
ret = rmt_storage_add_shrd_mem(rs_client->sid, start, size,
NULL, NULL, rs_client->srv);
if (ret < 0)
return ret;
pr_debug("%s: Allocated %d bytes at phys_addr=0x%x for handle=%d\n",
__func__, size, start, rs_client->handle);
#ifdef CONFIG_MSM_SDIO_SMEM
ret = platform_driver_register(&sdio_smem_drv);
if (ret)
pr_err("%s: Unable to register sdio smem client\n", __func__);
#endif
return (int)start;
}
static int handle_rmt_storage_call(struct msm_rpc_client *client,
struct rpc_request_hdr *req,
struct msm_rpc_xdr *xdr)
{
int rc;
uint32_t result = RMT_STORAGE_NO_ERROR;
uint32_t rpc_status = RPC_ACCEPTSTAT_SUCCESS;
struct rmt_storage_event *event_args;
struct rmt_storage_kevent *kevent;
kevent = kzalloc(sizeof(struct rmt_storage_kevent), GFP_KERNEL);
if (!kevent) {
rpc_status = RPC_ACCEPTSTAT_SYSTEM_ERR;
goto out;
}
event_args = &kevent->event;
switch (req->procedure) {
case RMT_STORAGE_OPEN_CB_TYPE_PROC:
/* client created in cb needs a ref. to its server */
event_args->usr_data = client->prog;
/* fall through */
case RMT_STORAGE_WRITE_IOVEC_CB_TYPE_PROC:
/* fall through */
case RMT_STORAGE_READ_IOVEC_CB_TYPE_PROC:
/* fall through */
case RMT_STORAGE_ALLOC_RMT_BUF_CB_TYPE_PROC:
/* fall through */
case RMT_STORAGE_EVENT_CB_TYPE_PROC: {
uint32_t cb_id;
int (*cb_func)(struct rmt_storage_event *event_args,
struct msm_rpc_xdr *xdr);
xdr_recv_uint32(xdr, &cb_id);
cb_func = msm_rpc_get_cb_func(client, cb_id);
if (!cb_func) {
rpc_status = RPC_ACCEPTSTAT_GARBAGE_ARGS;
kfree(kevent);
goto out;
}
rc = cb_func(event_args, xdr);
if (IS_ERR_VALUE(rc)) {
pr_err("%s: Invalid parameters received\n", __func__);
result = RMT_STORAGE_ERROR_PARAM;
kfree(kevent);
goto out;
}
result = (uint32_t) rc;
break;
}
default:
kfree(kevent);
pr_err("%s: unknown procedure %d\n", __func__, req->procedure);
rpc_status = RPC_ACCEPTSTAT_PROC_UNAVAIL;
goto out;
}
if (req->procedure != RMT_STORAGE_ALLOC_RMT_BUF_CB_TYPE_PROC) {
put_event(rmc, kevent);
atomic_inc(&rmc->total_events);
wake_up(&rmc->event_q);
} else
kfree(kevent);
out:
pr_debug("%s: Sending result=0x%x\n", __func__, result);
xdr_start_accepted_reply(xdr, rpc_status);
xdr_send_uint32(xdr, &result);
rc = xdr_send_msg(xdr);
if (rc)
pr_err("%s: send accepted reply failed: %d\n", __func__, rc);
return rc;
}
static int rmt_storage_open(struct inode *ip, struct file *fp)
{
int ret = 0;
spin_lock(&rmc->lock);
if (!rmc->open_excl)
rmc->open_excl = 1;
else
ret = -EBUSY;
spin_unlock(&rmc->lock);
return ret;
}
static int rmt_storage_release(struct inode *ip, struct file *fp)
{
spin_lock(&rmc->lock);
rmc->open_excl = 0;
spin_unlock(&rmc->lock);
return 0;
}
static long rmt_storage_ioctl(struct file *fp, unsigned int cmd,
unsigned long arg)
{
int ret = 0;
struct rmt_storage_kevent *kevent;
struct rmt_storage_send_sts status;
static struct msm_rpc_client *rpc_client;
struct rmt_shrd_mem_param usr_shrd_mem, *shrd_mem;
#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
struct rmt_storage_stats *stats;
struct rmt_storage_op_stats *op_stats;
ktime_t curr_stat;
#endif
switch (cmd) {
case RMT_STORAGE_SHRD_MEM_PARAM:
pr_info("%s: get shared memory parameters ioctl\n", __func__);
if (copy_from_user(&usr_shrd_mem, (void __user *)arg,
sizeof(struct rmt_shrd_mem_param))) {
pr_err("%s: copy from user failed\n\n", __func__);
ret = -EFAULT;
break;
}
shrd_mem = rmt_storage_get_shrd_mem(usr_shrd_mem.sid);
if (!shrd_mem) {
pr_err("%s: invalid sid (0x%x)\n", __func__,
usr_shrd_mem.sid);
ret = -EFAULT;
break;
}
if (copy_to_user((void __user *)arg, shrd_mem,
sizeof(struct rmt_shrd_mem_param))) {
pr_err("%s: copy to user failed\n\n", __func__);
ret = -EFAULT;
}
break;
case RMT_STORAGE_WAIT_FOR_REQ:
pr_info("%s: wait for request ioctl\n", __func__);
if (atomic_read(&rmc->total_events) == 0) {
ret = wait_event_interruptible(rmc->event_q,
atomic_read(&rmc->total_events) != 0);
}
if (ret < 0)
break;
atomic_dec(&rmc->total_events);
kevent = get_event(rmc);
WARN_ON(kevent == NULL);
if (copy_to_user((void __user *)arg, &kevent->event,
sizeof(struct rmt_storage_event))) {
pr_err("%s: copy to user failed\n\n", __func__);
ret = -EFAULT;
}
kfree(kevent);
break;
case RMT_STORAGE_SEND_STATUS:
pr_info("%s: send status ioctl\n", __func__);
if (copy_from_user(&status, (void __user *)arg,
sizeof(struct rmt_storage_send_sts))) {
pr_err("%s: copy from user failed\n\n", __func__);
ret = -EFAULT;
if (atomic_dec_return(&rmc->wcount) == 0)
wake_unlock(&rmc->wlock);
break;
}
#ifdef CONFIG_MSM_RMT_STORAGE_CLIENT_STATS
stats = &client_stats[status.handle - 1];
if (status.xfer_dir == RMT_STORAGE_WRITE)
op_stats = &stats->wr_stats;
else
op_stats = &stats->rd_stats;
curr_stat = ktime_sub(ktime_get(), op_stats->start);
op_stats->total = ktime_add(op_stats->total, curr_stat);
op_stats->count++;
if (curr_stat.tv64 < stats->min.tv64)
op_stats->min = curr_stat;
if (curr_stat.tv64 > stats->max.tv64)
op_stats->max = curr_stat;
#endif
pr_debug("%s: \thandle=%d err_code=%d data=0x%x\n", __func__,
status.handle, status.err_code, status.data);
rpc_client = rmt_storage_get_rpc_client(status.handle);
if (rpc_client)
ret = msm_rpc_client_req2(rpc_client,
RMT_STORAGE_OP_FINISH_PROC,
rmt_storage_send_sts_arg,
&status, NULL, NULL, -1);
else
ret = -EINVAL;
if (ret < 0)
pr_err("%s: send status failed with ret val = %d\n",
__func__, ret);
if (atomic_dec_return(&rmc->wcount) == 0)
wake_unlock(&rmc->wlock);
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
struct rmt_storage_sync_recv_arg {
int data;
};