forked from clearlinux-pkgs/linux-iot-lts2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0033-mei-spd-storage-proxy-driver.patch
1435 lines (1428 loc) · 34.9 KB
/
0033-mei-spd-storage-proxy-driver.patch
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
From 2eb395137cba00503441b1c2d3c83a13eaea9025 Mon Sep 17 00:00:00 2001
From: Alexander Usyskin <[email protected]>
Date: Tue, 23 Dec 2014 17:18:37 +0200
Subject: [PATCH 18/22] mei: spd: storage proxy driver
Host storage proxy driver enables ME FW to store its data on a storage
devices such as eMMC or UFS via host interface on a dedicated partition.
This patch implements storage over an eMMC GPP partition and UFS LUN.
A GPP partition is required for pre manufacturing operation since RPMB
partition key can be written only once.
V9: 1. Add SPXD Identifiers
Change-Id: I524c237d240d93accc9ce071e92744191f23ce87
Signed-off-by: Alexander Usyskin <[email protected]>
Signed-off-by: Tomas Winkler <[email protected]>
---
drivers/misc/mei/Kconfig | 2 +
drivers/misc/mei/Makefile | 2 +
drivers/misc/mei/spd/Kconfig | 12 +
drivers/misc/mei/spd/Makefile | 11 +
drivers/misc/mei/spd/cmd.c | 485 +++++++++++++++++++++++++++++++++
drivers/misc/mei/spd/cmd.h | 230 ++++++++++++++++
drivers/misc/mei/spd/debugfs.c | 79 ++++++
drivers/misc/mei/spd/gpp.c | 299 ++++++++++++++++++++
drivers/misc/mei/spd/main.c | 118 ++++++++
drivers/misc/mei/spd/spd.h | 93 +++++++
10 files changed, 1331 insertions(+)
create mode 100644 drivers/misc/mei/spd/Kconfig
create mode 100644 drivers/misc/mei/spd/Makefile
create mode 100644 drivers/misc/mei/spd/cmd.c
create mode 100644 drivers/misc/mei/spd/cmd.h
create mode 100644 drivers/misc/mei/spd/debugfs.c
create mode 100644 drivers/misc/mei/spd/gpp.c
create mode 100644 drivers/misc/mei/spd/main.c
create mode 100644 drivers/misc/mei/spd/spd.h
diff --git a/drivers/misc/mei/Kconfig b/drivers/misc/mei/Kconfig
index c49e1d2269af..2a82520f3e4e 100644
--- a/drivers/misc/mei/Kconfig
+++ b/drivers/misc/mei/Kconfig
@@ -43,3 +43,5 @@ config INTEL_MEI_TXE
Supported SoCs:
Intel Bay Trail
+
+source "drivers/misc/mei/spd/Kconfig"
diff --git a/drivers/misc/mei/Makefile b/drivers/misc/mei/Makefile
index cd6825afa8e1..f83e2fdc01d3 100644
--- a/drivers/misc/mei/Makefile
+++ b/drivers/misc/mei/Makefile
@@ -23,3 +23,5 @@ mei-txe-objs += hw-txe.o
mei-$(CONFIG_EVENT_TRACING) += mei-trace.o
CFLAGS_mei-trace.o = -I$(src)
+
+obj-$(CONFIG_INTEL_MEI_SPD) += spd/
diff --git a/drivers/misc/mei/spd/Kconfig b/drivers/misc/mei/spd/Kconfig
new file mode 100644
index 000000000000..8347fbc9fbe0
--- /dev/null
+++ b/drivers/misc/mei/spd/Kconfig
@@ -0,0 +1,12 @@
+#
+# Storage proxy device configuration
+#
+config INTEL_MEI_SPD
+ tristate "Intel MEI Host Storage Proxy Driver"
+ depends on INTEL_MEI && BLOCK
+ help
+ A driver for the host storage proxy ME client
+ The driver enables ME FW to store data on a storage devices
+ that are accessible only from the host.
+
+ To compile this driver as a module, choose M here.
diff --git a/drivers/misc/mei/spd/Makefile b/drivers/misc/mei/spd/Makefile
new file mode 100644
index 000000000000..8e8aba94b9ef
--- /dev/null
+++ b/drivers/misc/mei/spd/Makefile
@@ -0,0 +1,11 @@
+#
+# Makefile for the Storage Proxy device driver.
+#
+
+obj-$(CONFIG_INTEL_MEI_SPD) += mei_spd.o
+mei_spd-objs := main.o
+mei_spd-objs += cmd.o
+mei_spd-objs += gpp.o
+mei_spd-$(CONFIG_DEBUG_FS) += debugfs.o
+
+ccflags-y += -D__CHECK_ENDIAN__
diff --git a/drivers/misc/mei/spd/cmd.c b/drivers/misc/mei/spd/cmd.c
new file mode 100644
index 000000000000..25d682d7eb09
--- /dev/null
+++ b/drivers/misc/mei/spd/cmd.c
@@ -0,0 +1,485 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/*
+ * Copyright(c) 2015 - 2018 Intel Corporation. All rights reserved.
+ */
+#include <linux/slab.h>
+#include <linux/sched.h>
+#include <linux/workqueue.h>
+
+#include "cmd.h"
+#include "spd.h"
+
+#define spd_cmd_size(_cmd) \
+ (sizeof(struct spd_cmd_hdr) + \
+ sizeof(struct spd_cmd_##_cmd))
+#define to_spd_hdr(_buf) (struct spd_cmd_hdr *)(_buf)
+#define to_spd_cmd(_cmd, _buf) \
+ (struct spd_cmd_##_cmd *)((_buf) + sizeof(struct spd_cmd_hdr))
+
+const char *spd_cmd_str(enum spd_cmd_type cmd)
+{
+#define __SPD_CMD(_cmd) SPD_##_cmd##_CMD
+#define SPD_CMD(cmd) case __SPD_CMD(cmd): return #cmd
+ switch (cmd) {
+ SPD_CMD(NONE);
+ SPD_CMD(START_STOP);
+ SPD_CMD(RPMB_WRITE);
+ SPD_CMD(RPMB_READ);
+ SPD_CMD(RPMB_GET_COUNTER);
+ SPD_CMD(GPP_WRITE);
+ SPD_CMD(GPP_READ);
+ SPD_CMD(TRIM);
+ SPD_CMD(INIT);
+ SPD_CMD(STORAGE_STATUS);
+ SPD_CMD(MAX);
+ default:
+ return "unknown";
+ }
+#undef SPD_CMD
+#undef __SPD_CMD
+}
+
+const char *mei_spd_dev_str(enum spd_storage_type type)
+{
+#define SPD_TYPE(type) case SPD_TYPE_##type: return #type
+ switch (type) {
+ SPD_TYPE(UNDEF);
+ SPD_TYPE(EMMC);
+ SPD_TYPE(UFS);
+ default:
+ return "unknown";
+ }
+#undef SPD_TYPE
+}
+
+const char *mei_spd_state_str(enum mei_spd_state state)
+{
+#define SPD_STATE(state) case MEI_SPD_STATE_##state: return #state
+ switch (state) {
+ SPD_STATE(INIT);
+ SPD_STATE(INIT_WAIT);
+ SPD_STATE(INIT_DONE);
+ SPD_STATE(RUNNING);
+ SPD_STATE(STOPPING);
+ default:
+ return "unknown";
+ }
+#undef SPD_STATE
+}
+
+/**
+ * mei_spd_init_req - send init request
+ *
+ * @spd: spd device
+ *
+ * Return: 0 on success
+ * -EPROTO if called in wrong state
+ * < 0 on write error
+ */
+int mei_spd_cmd_init_req(struct mei_spd *spd)
+{
+ const int req_len = sizeof(struct spd_cmd_hdr);
+ struct spd_cmd_hdr *hdr;
+ u32 cmd_type = SPD_INIT_CMD;
+ ssize_t ret;
+
+ spd_dbg(spd, "cmd [%d] %s : state [%d] %s\n",
+ cmd_type, spd_cmd_str(cmd_type),
+ spd->state, mei_spd_state_str(spd->state));
+
+ if (spd->state != MEI_SPD_STATE_INIT)
+ return -EPROTO;
+
+ memset(spd->buf, 0, req_len);
+ hdr = to_spd_hdr(spd->buf);
+
+ hdr->command_type = cmd_type;
+ hdr->is_response = 0;
+ hdr->len = req_len;
+
+ spd->state = MEI_SPD_STATE_INIT_WAIT;
+ ret = mei_cldev_send(spd->cldev, spd->buf, req_len);
+ if (ret != req_len) {
+ spd_err(spd, "start send failed ret = %zd\n", ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+/**
+ * mei_spd_cmd_init_rsp - handle init response message
+ *
+ * @spd: spd device
+ * @cmd: received spd command
+ * @cmd_sz: received command size
+ *
+ * Return: 0 on success; < 0 otherwise
+ */
+static int mei_spd_cmd_init_rsp(struct mei_spd *spd, struct spd_cmd *cmd,
+ ssize_t cmd_sz)
+{
+ int type;
+ int gpp_id;
+ int i;
+
+ if (cmd_sz < spd_cmd_size(init_resp)) {
+ spd_err(spd, "Wrong init response size\n");
+ return -EINVAL;
+ }
+
+ if (spd->state != MEI_SPD_STATE_INIT_WAIT)
+ return -EPROTO;
+
+ type = cmd->init_rsp.type;
+ gpp_id = cmd->init_rsp.gpp_partition_id;
+
+ switch (type) {
+ case SPD_TYPE_EMMC:
+ if (gpp_id < 1 || gpp_id > 4) {
+ spd_err(spd, "%s unsupported gpp id %d\n",
+ mei_spd_dev_str(type), gpp_id);
+ return -EINVAL;
+ }
+ break;
+
+ case SPD_TYPE_UFS:
+ if (gpp_id < 1 || gpp_id > 6) {
+ spd_err(spd, "%s unsupported gpp id %d\n",
+ mei_spd_dev_str(type), gpp_id);
+ return -EINVAL;
+ }
+ break;
+
+ default:
+ spd_err(spd, "unsupported storage type %d\n",
+ cmd->init_rsp.type);
+ return -EINVAL;
+ }
+
+ spd->dev_type = type;
+ spd->gpp_partition_id = gpp_id;
+
+ if (cmd->init_rsp.serial_no_sz != 0) {
+ if (cmd->init_rsp.serial_no_sz !=
+ cmd_sz - spd_cmd_size(init_resp)) {
+ spd_err(spd, "wrong serial no size %u?=%zu\n",
+ cmd->init_rsp.serial_no_sz,
+ cmd_sz - spd_cmd_size(init_resp));
+ return -EMSGSIZE;
+ }
+
+ if (cmd->init_rsp.serial_no_sz > 256) {
+ spd_err(spd, "serial no is too large %u\n",
+ cmd->init_rsp.serial_no_sz);
+ return -EMSGSIZE;
+ }
+
+ spd->dev_id = kzalloc(cmd->init_rsp.serial_no_sz, GFP_KERNEL);
+ if (!spd->dev_id)
+ return -ENOMEM;
+
+ spd->dev_id_sz = cmd->init_rsp.serial_no_sz;
+ if (type == SPD_TYPE_EMMC) {
+ /* FW have this in be32 format */
+ __be32 *sno = (__be32 *)cmd->init_rsp.serial_no;
+ u32 *dev_id = (u32 *)spd->dev_id;
+
+ for (i = 0; i < spd->dev_id_sz / sizeof(u32); i++)
+ dev_id[i] = be32_to_cpu(sno[i]);
+ } else {
+ memcpy(spd->dev_id, &cmd->init_rsp.serial_no,
+ cmd->init_rsp.serial_no_sz);
+ }
+ }
+
+ spd->state = MEI_SPD_STATE_INIT_DONE;
+
+ return 0;
+}
+
+/**
+ * mei_spd_cmd_storage_status_req - send storage status message
+ *
+ * @spd: spd device
+ *
+ * Return: 0 on success
+ * -EPROTO if called in wrong state
+ * < 0 on write error
+ */
+int mei_spd_cmd_storage_status_req(struct mei_spd *spd)
+{
+ struct spd_cmd_hdr *hdr;
+ struct spd_cmd_storage_status_req *req;
+ const int req_len = spd_cmd_size(storage_status_req);
+ u32 cmd_type = SPD_STORAGE_STATUS_CMD;
+ ssize_t ret;
+
+ spd_dbg(spd, "cmd [%d] %s : state [%d] %s\n",
+ cmd_type, spd_cmd_str(cmd_type),
+ spd->state, mei_spd_state_str(spd->state));
+
+ if (spd->state < MEI_SPD_STATE_INIT_DONE)
+ return -EPROTO;
+
+ memset(spd->buf, 0, req_len);
+ hdr = to_spd_hdr(spd->buf);
+
+ hdr->command_type = cmd_type;
+ hdr->is_response = 0;
+ hdr->len = req_len;
+
+ req = to_spd_cmd(storage_status_req, spd->buf);
+ req->gpp_on = mei_spd_gpp_is_open(spd);
+ req->rpmb_on = 0;
+
+ ret = mei_cldev_send(spd->cldev, spd->buf, req_len);
+ if (ret != req_len) {
+ spd_err(spd, "send storage status failed ret = %zd\n", ret);
+ return ret;
+ }
+
+ if (req->gpp_on || req->rpmb_on)
+ spd->state = MEI_SPD_STATE_RUNNING;
+ else
+ spd->state = MEI_SPD_STATE_INIT_DONE;
+
+ spd_dbg(spd, "cmd [%d] %s : state [%d] %s\n",
+ cmd_type, spd_cmd_str(cmd_type),
+ spd->state, mei_spd_state_str(spd->state));
+
+ return 0;
+}
+
+static int mei_spd_cmd_gpp_write(struct mei_spd *spd, struct spd_cmd *cmd,
+ ssize_t out_buf_sz)
+{
+ size_t len = SPD_GPP_WRITE_DATA_LEN(*cmd);
+ int ret;
+
+ if (out_buf_sz < spd_cmd_size(gpp_write_req)) {
+ spd_err(spd, "Wrong request size\n");
+ return SPD_STATUS_INVALID_COMMAND;
+ }
+
+ ret = mei_spd_gpp_write(spd, cmd->gpp_write_req.offset,
+ cmd->gpp_write_req.data, len);
+ if (ret) {
+ spd_err(spd, "Failed to write to gpp ret = %d\n", ret);
+ return SPD_STATUS_GENERAL_FAILURE;
+ }
+
+ spd_dbg(spd, "wrote %zd bytes of data\n", len);
+
+ cmd->header.len = spd_cmd_size(gpp_write_rsp);
+
+ return SPD_STATUS_SUCCESS;
+}
+
+static int mei_spd_cmd_gpp_read(struct mei_spd *spd, struct spd_cmd *cmd,
+ ssize_t out_buf_sz)
+{
+ size_t len;
+ int ret;
+
+ if (out_buf_sz < spd_cmd_size(gpp_read_req)) {
+ spd_err(spd, "Wrong request size\n");
+ return SPD_STATUS_INVALID_COMMAND;
+ }
+
+ len = cmd->gpp_read_req.size_to_read;
+ if (len > SPD_CLIENT_GPP_DATA_MAX_SIZE) {
+ spd_err(spd, "Block is to large to read\n");
+ return SPD_STATUS_INVALID_COMMAND;
+ }
+
+ ret = mei_spd_gpp_read(spd, cmd->gpp_read_req.offset,
+ cmd->gpp_read_resp.data, len);
+
+ if (ret) {
+ spd_err(spd, "Failed to read from gpp ret = %d\n", ret);
+ return SPD_STATUS_GENERAL_FAILURE;
+ }
+
+ spd_dbg(spd, "read %zd bytes of data\n", len);
+
+ cmd->header.len = spd_cmd_size(gpp_read_rsp) + len;
+
+ return SPD_STATUS_SUCCESS;
+}
+
+static int mei_spd_cmd_response(struct mei_spd *spd, ssize_t out_buf_sz)
+{
+ struct spd_cmd *cmd = (struct spd_cmd *)spd->buf;
+ u32 spd_cmd;
+ int ret;
+
+ spd_cmd = cmd->header.command_type;
+
+ spd_dbg(spd, "rsp [%d] %s : state [%d] %s\n",
+ spd_cmd, spd_cmd_str(spd_cmd),
+ spd->state, mei_spd_state_str(spd->state));
+
+ switch (spd_cmd) {
+ case SPD_INIT_CMD:
+ ret = mei_spd_cmd_init_rsp(spd, cmd, out_buf_sz);
+ if (ret)
+ break;
+ mutex_unlock(&spd->lock);
+ mei_spd_gpp_init(spd);
+ mutex_lock(&spd->lock);
+ break;
+ default:
+ ret = -EINVAL;
+ spd_err(spd, "Wrong response command %d\n", spd_cmd);
+ break;
+ }
+
+ return ret;
+}
+
+/**
+ * mei_spd_cmd_request - dispatch command requests from the SPD device
+ *
+ * @spd: spd device
+ * @out_buf_sz: buffer size
+ *
+ * Return: (TBD)
+ */
+static int mei_spd_cmd_request(struct mei_spd *spd, ssize_t out_buf_sz)
+{
+ struct spd_cmd *cmd = (struct spd_cmd *)spd->buf;
+ ssize_t written;
+ u32 spd_cmd;
+ int ret;
+
+ spd_cmd = cmd->header.command_type;
+
+ spd_dbg(spd, "req [%d] %s : state [%d] %s\n",
+ spd_cmd, spd_cmd_str(spd_cmd),
+ spd->state, mei_spd_state_str(spd->state));
+
+ if (spd->state < MEI_SPD_STATE_RUNNING) {
+ spd_err(spd, "Wrong state %d\n", spd->state);
+ ret = SPD_STATUS_INVALID_COMMAND;
+ goto reply;
+ }
+
+ switch (spd_cmd) {
+ case SPD_RPMB_WRITE_CMD:
+ case SPD_RPMB_READ_CMD:
+ case SPD_RPMB_GET_COUNTER_CMD:
+ spd_err(spd, "Command %d is not supported\n", spd_cmd);
+ ret = SPD_STATUS_NOT_SUPPORTED;
+ break;
+ case SPD_GPP_WRITE_CMD:
+ ret = mei_spd_cmd_gpp_write(spd, cmd, out_buf_sz);
+ break;
+ case SPD_GPP_READ_CMD:
+ ret = mei_spd_cmd_gpp_read(spd, cmd, out_buf_sz);
+ break;
+ case SPD_TRIM_CMD:
+ spd_err(spd, "Command %d is not supported\n", spd_cmd);
+ ret = SPD_STATUS_NOT_SUPPORTED;
+ break;
+ default:
+ spd_err(spd, "Wrong request command %d\n", spd_cmd);
+ ret = SPD_STATUS_INVALID_COMMAND;
+ break;
+ }
+reply:
+ cmd->header.is_response = 1;
+ cmd->header.status = ret;
+ if (ret != SPD_STATUS_SUCCESS)
+ cmd->header.len = sizeof(struct spd_cmd_hdr);
+
+ written = mei_cldev_send(spd->cldev, spd->buf, cmd->header.len);
+ if (written != cmd->header.len) {
+ ret = SPD_STATUS_GENERAL_FAILURE;
+ spd_err(spd, "Failed to send reply written = %zd\n", written);
+ }
+
+ /* FIXME: translate ret to errno */
+ if (ret)
+ return -EINVAL;
+
+ return 0;
+}
+
+ssize_t mei_spd_cmd(struct mei_spd *spd)
+{
+ struct spd_cmd *cmd = (struct spd_cmd *)spd->buf;
+ ssize_t out_buf_sz;
+ int ret;
+
+ out_buf_sz = mei_cldev_recv(spd->cldev, spd->buf, spd->buf_sz);
+ if (out_buf_sz < 0) {
+ spd_err(spd, "failure in receive ret = %zd\n", out_buf_sz);
+ return out_buf_sz;
+ }
+
+ if (out_buf_sz == 0) {
+ spd_err(spd, "received empty msg\n");
+ return 0;
+ }
+
+ /* check that we've received at least sizeof(header) */
+ if (out_buf_sz < sizeof(struct spd_cmd_hdr)) {
+ spd_err(spd, "Request is too short\n");
+ return -EFAULT;
+ }
+
+ if (cmd->header.is_response)
+ ret = mei_spd_cmd_response(spd, out_buf_sz);
+ else
+ ret = mei_spd_cmd_request(spd, out_buf_sz);
+
+ return ret;
+}
+
+static void mei_spd_status_send_work(struct work_struct *work)
+{
+ struct mei_spd *spd =
+ container_of(work, struct mei_spd, status_send_w);
+
+ mutex_lock(&spd->lock);
+ mei_spd_cmd_storage_status_req(spd);
+ mutex_unlock(&spd->lock);
+}
+
+void mei_spd_free(struct mei_spd *spd)
+{
+ if (!spd)
+ return;
+
+ cancel_work_sync(&spd->status_send_w);
+
+ kfree(spd->buf);
+ kfree(spd);
+}
+
+struct mei_spd *mei_spd_alloc(struct mei_cl_device *cldev)
+{
+ struct mei_spd *spd;
+ u8 *buf;
+
+ spd = kzalloc(sizeof(*spd), GFP_KERNEL);
+ if (!spd)
+ return NULL;
+
+ spd->buf_sz = sizeof(struct spd_cmd) + SPD_CLIENT_GPP_DATA_MAX_SIZE;
+ buf = kmalloc(spd->buf_sz, GFP_KERNEL);
+ if (!buf)
+ goto free;
+
+ spd->cldev = cldev;
+ spd->buf = buf;
+ spd->state = MEI_SPD_STATE_INIT;
+ mutex_init(&spd->lock);
+ INIT_WORK(&spd->status_send_w, mei_spd_status_send_work);
+
+ return spd;
+free:
+ kfree(spd);
+ return NULL;
+}
diff --git a/drivers/misc/mei/spd/cmd.h b/drivers/misc/mei/spd/cmd.h
new file mode 100644
index 000000000000..3f77550f44ab
--- /dev/null
+++ b/drivers/misc/mei/spd/cmd.h
@@ -0,0 +1,230 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
+/*
+ * Copyright (C) 2015-2018 Intel Corp. All rights reserved
+ */
+#ifndef _SPD_CMD_H
+#define _SPD_CMD_H
+
+#include <linux/types.h>
+
+/**
+ * enum spd_cmd_type - available commands
+ *
+ * @SPD_NONE_CMD : Lower command sentinel.
+ * @SPD_START_STOP_CMD : start stop command (deprecated). [Host -> TEE]
+ * @SPD_RPMB_WRITE_CMD : RPMB write request. [TEE -> Host]
+ * @SPD_RPMB_READ_CMD : RPMB read request. [TEE -> Host]
+ * @SPD_RPMB_GET_COUNTER_CMD: get counter request [TEE -> Host]
+ * @SPD_GPP_WRITE_CMD : GPP write request. [TEE -> Host]
+ * @SPD_GPP_READ_CMD : GPP read request. [TEE -> Host]
+ * @SPD_TRIM_CMD : TRIM command [TEE -> Host]
+ * @SPD_INIT_CMD : initial handshake between host and fw. [Host -> TEE]
+ * @SPD_STORAGE_STATUS_CMD : the backing storage status. [Host -> TEE]
+ * @SPD_MAX_CMD: Upper command sentinel.
+ */
+enum spd_cmd_type {
+ SPD_NONE_CMD = 0,
+ SPD_START_STOP_CMD,
+ SPD_RPMB_WRITE_CMD,
+ SPD_RPMB_READ_CMD,
+ SPD_RPMB_GET_COUNTER_CMD,
+ SPD_GPP_WRITE_CMD,
+ SPD_GPP_READ_CMD,
+ SPD_TRIM_CMD,
+ SPD_INIT_CMD,
+ SPD_STORAGE_STATUS_CMD,
+ SPD_MAX_CMD,
+};
+
+enum spd_status {
+ SPD_STATUS_SUCCESS = 0,
+ SPD_STATUS_GENERAL_FAILURE = 1,
+ SPD_STATUS_NOT_READY = 2,
+ SPD_STATUS_NOT_SUPPORTED = 3,
+ SPD_STATUS_INVALID_COMMAND = 4,
+};
+
+/**
+ * enum spd_storage_type - storage device type
+ *
+ * @SPD_TYPE_UNDEF: lower enum sentinel
+ * @SPD_TYPE_EMMC: emmc device
+ * @SPD_TYPE_UFS: ufs device
+ * @SPD_TYPE_MAX: upper enum sentinel
+ */
+enum spd_storage_type {
+ SPD_TYPE_UNDEF = 0,
+ SPD_TYPE_EMMC = 1,
+ SPD_TYPE_UFS = 2,
+ SPD_TYPE_MAX
+};
+
+/**
+ * struct spd_cmd_hdr - Host storage Command Header
+ *
+ * @command_type: SPD_TYPES
+ * @is_response: 1 == Response, 0 == Request
+ * @len: command length
+ * @status: command status
+ * @reserved: reserved
+ */
+struct spd_cmd_hdr {
+ u32 command_type : 7;
+ u32 is_response : 1;
+ u32 len : 13;
+ u32 status : 8;
+ u32 reserved : 3;
+} __packed;
+
+/**
+ * RPMB Frame Size as defined by the JDEC spec
+ */
+#define SPD_CLIENT_RPMB_DATA_MAX_SIZE (512)
+
+/**
+ * struct spd_cmd_init_resp
+ * commandType == HOST_STORAGE_INIT_CMD
+ *
+ * @gpp_partition_id: gpp_partition:
+ * UFS: LUN Number (0-7)
+ * EMMC: 1-4.
+ * 0xff: GPP not supported
+ * @type: storage hw type
+ * SPD_TYPE_EMMC
+ * SPD_TYPE_UFS
+ * @serial_no_sz: serial_no size
+ * @serial_no: device serial number
+ */
+struct spd_cmd_init_resp {
+ u32 gpp_partition_id;
+ u32 type;
+ u32 serial_no_sz;
+ u8 serial_no[0];
+};
+
+/**
+ * struct spd_cmd_storage_status_req
+ * commandType == SPD_STORAGE_STATUS_CMD
+ *
+ * @gpp_on: availability of the gpp backing storage
+ * 0 - GP partition is accessible
+ * 1 - GP partition is not accessible
+ * @rpmb_on: availability of the backing storage
+ * 0 - RPMB partition is accessible
+ * 1 - RPBM partition is not accessible
+ */
+struct spd_cmd_storage_status_req {
+ u32 gpp_on;
+ u32 rpmb_on;
+} __packed;
+
+/**
+ * struct spd_cmd_rpmb_write
+ * command_type == SPD_RPMB_WRITE_CMD
+ *
+ * @rpmb_frame: RPMB frame are constant size (512)
+ */
+struct spd_cmd_rpmb_write {
+ u8 rpmb_frame[0];
+} __packed;
+
+/**
+ * struct spd_cmd_rpmb_read
+ * command_type == SPD_RPMB_READ_CMD
+ *
+ * @rpmb_frame: RPMB frame are constant size (512)
+ */
+struct spd_cmd_rpmb_read {
+ u8 rpmb_frame[0];
+} __packed;
+
+/**
+ * struct spd_cmd_rpmb_get_counter
+ * command_type == SPD_RPMB_GET_COUNTER_CMD
+ *
+ * @rpmb_frame: frame containing frame counter
+ */
+struct spd_cmd_rpmb_get_counter {
+ u8 rpmb_frame[0];
+} __packed;
+
+/**
+ * struct spd_cmd_gpp_write_req
+ * command_type == SPD_GPP_WRITE_CMD
+ *
+ * @offset: frame offset in partition
+ * @data: 4K page
+ */
+struct spd_cmd_gpp_write_req {
+ u32 offset;
+ u8 data[0];
+} __packed;
+
+/**
+ * struct spd_cmd_gpp_write_rsp
+ * command_type == SPD_GPP_WRITE_CMD
+ *
+ * @reserved: reserved
+ */
+struct spd_cmd_gpp_write_rsp {
+ u32 reserved[2];
+} __packed;
+
+/**
+ * struct spd_cmd_gpp_read_req
+ * command_type == SPD_GPP_READ_CMD
+ *
+ * @offset: offset of a frame on GPP partition
+ * @size_to_read: data length to read (must be )
+ */
+struct spd_cmd_gpp_read_req {
+ u32 offset;
+ u32 size_to_read;
+} __packed;
+
+/**
+ * struct spd_cmd_gpp_read_rsp
+ * command_type == SPD_GPP_READ_CMD
+ *
+ * @reserved: reserved
+ * @data: data
+ */
+struct spd_cmd_gpp_read_rsp {
+ u32 reserved;
+ u8 data[0];
+} __packed;
+
+#define SPD_GPP_READ_DATA_LEN(cmd) ((cmd).header.len - \
+ (sizeof(struct spd_cmd_hdr) + \
+ sizeof(struct spd_cmd_gpp_read_rsp)))
+
+#define SPD_GPP_WRITE_DATA_LEN(cmd) ((cmd).header.len - \
+ (sizeof(struct spd_cmd_hdr) + \
+ sizeof(struct spd_cmd_gpp_write_req)))
+
+struct spd_cmd {
+ struct spd_cmd_hdr header;
+
+ union {
+ struct spd_cmd_rpmb_write rpmb_write;
+ struct spd_cmd_rpmb_read rpmb_read;
+ struct spd_cmd_rpmb_get_counter rpmb_get_counter;
+
+ struct spd_cmd_gpp_write_req gpp_write_req;
+ struct spd_cmd_gpp_write_rsp gpp_write_rsp;
+
+ struct spd_cmd_gpp_read_req gpp_read_req;
+ struct spd_cmd_gpp_read_rsp gpp_read_resp;
+
+ struct spd_cmd_init_resp init_rsp;
+ struct spd_cmd_storage_status_req status_req;
+ };
+} __packed;
+
+/* GPP Max data 4K */
+#define SPD_CLIENT_GPP_DATA_MAX_SIZE (4096)
+
+const char *spd_cmd_str(enum spd_cmd_type cmd);
+const char *mei_spd_dev_str(enum spd_storage_type type);
+
+#endif /* _SPD_CMD_H */
diff --git a/drivers/misc/mei/spd/debugfs.c b/drivers/misc/mei/spd/debugfs.c
new file mode 100644
index 000000000000..dfbb62a49fcc
--- /dev/null
+++ b/drivers/misc/mei/spd/debugfs.c
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/*
+ * Copyright(c) 2015 - 2018 Intel Corporation. All rights reserved.
+ */
+#include <linux/slab.h>
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/debugfs.h>
+
+#include "cmd.h"
+#include "spd.h"
+
+static ssize_t mei_spd_dbgfs_read_info(struct file *fp, char __user *ubuf,
+ size_t cnt, loff_t *ppos)
+{
+ struct mei_spd *spd = fp->private_data;
+ size_t bufsz = 4095;
+ char *buf;
+ int pos = 0;
+ int ret;
+
+ buf = kzalloc(bufsz, GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
+
+ pos += scnprintf(buf + pos, bufsz - pos, "DEV STATE: [%d] %s\n",
+ spd->state, mei_spd_state_str(spd->state));
+ pos += scnprintf(buf + pos, bufsz - pos, "DEV TYPE : [%d] %s\n",
+ spd->dev_type, mei_spd_dev_str(spd->dev_type));
+ pos += scnprintf(buf + pos, bufsz - pos, " ID SIZE : %d\n",
+ spd->dev_id_sz);
+ pos += scnprintf(buf + pos, bufsz - pos, " ID : '%s'\n", "N/A");
+ pos += scnprintf(buf + pos, bufsz - pos, "GPP\n");
+ pos += scnprintf(buf + pos, bufsz - pos, " id : %d\n",
+ spd->gpp_partition_id);
+ pos += scnprintf(buf + pos, bufsz - pos, " opened : %1d\n",
+ mei_spd_gpp_is_open(spd));
+
+ ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
+ kfree(buf);
+ return ret;
+}
+
+static const struct file_operations mei_spd_dbgfs_fops_info = {
+ .open = simple_open,
+ .read = mei_spd_dbgfs_read_info,
+ .llseek = generic_file_llseek,
+};
+
+void mei_spd_dbgfs_deregister(struct mei_spd *spd)
+{
+ if (!spd->dbgfs_dir)
+ return;
+ debugfs_remove_recursive(spd->dbgfs_dir);
+ spd->dbgfs_dir = NULL;
+}
+
+int mei_spd_dbgfs_register(struct mei_spd *spd, const char *name)
+{
+ struct dentry *dir, *f;
+
+ dir = debugfs_create_dir(name, NULL);
+ if (!dir)
+ return -ENOMEM;
+
+ spd->dbgfs_dir = dir;
+
+ f = debugfs_create_file("info", 0400, dir,
+ spd, &mei_spd_dbgfs_fops_info);
+ if (!f) {
+ spd_err(spd, "info: registration failed\n");
+ goto err;
+ }
+
+ return 0;
+err:
+ mei_spd_dbgfs_deregister(spd);
+ return -ENODEV;
+}
diff --git a/drivers/misc/mei/spd/gpp.c b/drivers/misc/mei/spd/gpp.c
new file mode 100644
index 000000000000..b5d1a27a50ee
--- /dev/null
+++ b/drivers/misc/mei/spd/gpp.c
@@ -0,0 +1,299 @@
+// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+/*
+ * Copyright(c) 2015 - 2018 Intel Corporation. All rights reserved.
+ */
+#include <linux/pagemap.h>
+#include <linux/writeback.h>
+#include <linux/slab.h>
+#include <linux/blkdev.h>
+#include <scsi/scsi_device.h>
+#include <scsi/scsi_host.h>
+
+#include "cmd.h"
+#include "spd.h"
+
+static struct page *page_read(struct address_space *mapping, int index)
+{
+ return read_mapping_page(mapping, index, NULL);
+}
+
+static int mei_spd_bd_read(struct mei_spd *spd, loff_t from, size_t len,
+ size_t *retlen, u_char *buf)
+{
+ struct page *page;
+ int index = from >> PAGE_SHIFT;
+ int offset = from & (PAGE_SIZE - 1);
+ int cpylen;
+
+ while (len) {
+ if ((offset + len) > PAGE_SIZE)
+ cpylen = PAGE_SIZE - offset;
+ else
+ cpylen = len;
+ len = len - cpylen;
+
+ page = page_read(spd->gpp->bd_inode->i_mapping, index);
+ if (IS_ERR(page))
+ return PTR_ERR(page);
+
+ memcpy(buf, page_address(page) + offset, cpylen);
+ put_page(page);
+
+ if (retlen)
+ *retlen += cpylen;
+ buf += cpylen;
+ offset = 0;
+ index++;
+ }
+ return 0;
+}
+
+static int _mei_spd_bd_write(struct block_device *dev, const u_char *buf,
+ loff_t to, size_t len, size_t *retlen)
+{
+ struct page *page;
+ struct address_space *mapping = dev->bd_inode->i_mapping;
+ int index = to >> PAGE_SHIFT; /* page index */
+ int offset = to & ~PAGE_MASK; /* page offset */
+ int cpylen;
+
+ while (len) {
+ if ((offset + len) > PAGE_SIZE)
+ cpylen = PAGE_SIZE - offset;
+ else
+ cpylen = len;
+ len = len - cpylen;
+
+ page = page_read(mapping, index);
+ if (IS_ERR(page))
+ return PTR_ERR(page);
+
+ if (memcmp(page_address(page) + offset, buf, cpylen)) {
+ lock_page(page);
+ memcpy(page_address(page) + offset, buf, cpylen);
+ set_page_dirty(page);
+ unlock_page(page);
+ balance_dirty_pages_ratelimited(mapping);
+ }
+ put_page(page);
+
+ if (retlen)
+ *retlen += cpylen;
+
+ buf += cpylen;
+ offset = 0;
+ index++;
+ }
+ return 0;
+}
+
+static int mei_spd_bd_write(struct mei_spd *spd, loff_t to, size_t len,