-
Notifications
You must be signed in to change notification settings - Fork 60
/
ufs_hmr.c
1174 lines (985 loc) · 27 KB
/
ufs_hmr.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
// SPDX-License-Identifier: GPL-2.0-or-later
/* Copyright (C) 2019 Western Digital Corporation or its affiliates */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <endian.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "ufs.h"
#include "ufs_hmr.h"
#include "ufs_cmds.h"
/*
* HMR Quirks: 1 - enable, 0 - disable
*/
/*
* Big-little-endian byte order: some params
* may occur in order other than expected.
* Change byte order for the following params.
*/
#define HMR_QUIRK_REFRESH_TOTCOUNT_BYTE_ORDER 1
#define HMR_QUIRK_REFRESH_PROGRESS_BYTE_ORDER 1
/*
* There is the maximum number of HMR operations in the life
* of the device. Having reached this number, the device will
* return "General Failure" and set the bRefreshStatus attribute
* to 0x05.
*/
#define HMR_REFRESH_MAX_TOTCOUNT 200
/*
* Output progress every N iterations.
*/
#define HMR_PROGRESS_OUTPUT_ITER 1
#if HMR_PROGRESS_OUTPUT_ITER <= 0
# error keep HMR_PROGRESS_OUTPUT_ITER > 0
#endif
/*
* The HMR mode type description string size we
* support to output. Can be enlarged if required.
*/
#define HMR_MODE_TYPE_MAX_SIZE 40
enum hmr_err_codes {
EHMR_OK = 0, /* success */
EHMR_REFRESH_PROGRESS = 100, /* wrong refresh progress */
EHMR_REFRESH_STATUS, /* wrong refresh status */
EHMR_REFRESH_TOTCOUNT, /* wrong refresh total count */
EHMR_FREEMEM, /* memory wasn't freed */
EHMR_NOMEM, /* not enough memory */
EHMR_INVAL, /* invalid argument */
EHMR_NORETRY, /* cannot be retried */
EHMR_REFRESH_METHOD, /* wrong refrresh method */
EHMR_REFRESH_UNIT, /* wrong refrresh unit */
};
enum hmr_refresh_status {
HMR_ST_IDLE = 0,
HMR_ST_IN_PROGRESS,
HMR_ST_ABORTED,
HMR_ST_COMPLETED,
HMR_ST_BUSY,
HMR_ST_GENERAL,
};
enum hmr_stage_skip {
HMR_SKIP_STATUS_CHECK = 1 << 0,
HMR_SKIP_METHOD_SET = 1 << 1,
HMR_SKIP_UNIT_SET = 1 << 2,
};
#pragma pack(push, 1)
struct descriptor_health_layout {
__u8 length;
__u8 type;
__u8 pre_eol_info;
__u8 dev_life_time_est_a;
__u8 dev_life_time_est_b;
__u8 vendor_prop_info[0x20];
__u32 refresh_total_count;
__u32 refresh_progress;
};
#pragma pack(pop)
struct descriptor {
enum desc_idn idn;
const char *name;
size_t size;
void *layout;
};
extern int do_query_rq(int fd,
struct ufs_bsg_request *bsg_req, /* request struct of the sg_io_v4 */
struct ufs_bsg_reply *bsg_rsp, /* response struct of the sg_io_v4 */
__u8 query_req_func, /* read / write */
__u8 opcode, /* opcode r/w of desc/attr/fl etc.*/
__u8 idn, /* (-t) util option */
__u8 index, /* (-i) util option */
__u8 sel, /* (-s) util option */
__u16 req_buf_len, /* request buffer size */
__u16 res_buf_len, /* response buffer size */
__u8 *data_buf); /* buffer with/for data */
extern struct desc_field_offset device_health_desc_conf_field_name[];
extern struct attr_fields ufs_attrs[];
extern struct flag_fields ufs_flags[];
static struct descriptor_health_layout desc_health_layout;
static struct descriptor desc_health = {
QUERY_DESC_IDN_HEALTH,
"Health",
sizeof desc_health_layout,
&desc_health_layout
};
static inline void hmr_delay_retry(int sec)
{
if (sec > 0)
sleep(sec);
}
static inline void hmr_output_message(const char *msg)
{
/*
* |---------------------------------------------------------|
* | Message format template: |
* |---------------------------------------------------------|
* |<<-6->| |<<-29-> |
* |HMR: | |variable string |
* | |
* |---------------------------------------------------------|
* |HMR: | |waiting idle status |
* |---------------------------------------------------------|
*/
printf("%-6s %-29s\r", "HMR:", msg);
fflush(stdout);
}
static inline void hmr_output_progress(__u32 progress,
__u64 iter, int sec, const char *msg)
{
/*
* |---------------------------------------------------------|
* | Progress format template: |
* |---------------------------------------------------------|
* |<<-6->| | <-9->>| |<<-35-> |
* |HMR: | | hex dig| |variable string |
* | |
* |---------------------------------------------------------|
* | The case of up to 100% progress: |
* |---------------------------------------------------------|
* |<<-6->| | <-9->>| |<<-35-> |
* |HMR: | | f20a| | |
* | | | | | |
* |---------------------------------------------------------|
* | The case of 100% progress: |
* |---------------------------------------------------------|
* |<<-6->| | <-8->>| |<<-20-> |
* |HMR: | | 100|% | |
* |---------------------------------------------------------|
*/
/*
* Completed - print 100%,
* otherwise print hex progress indicator every N iterations.
*/
if (!progress)
printf("%-6s %8d%% %-20s\r", "HMR:", 100, msg);
else if (0 == (iter % HMR_PROGRESS_OUTPUT_ITER))
printf("%-6s %9x %-20s\r", "HMR:", progress, msg);
fflush(stdout);
if (sec > 0)
sleep(sec);
}
static inline void hmr_output_header(const char *unit_str, int method)
{
int count;
const char *method_str;
char mode_str[HMR_MODE_TYPE_MAX_SIZE];
method_str = method == HMR_METHOD_FORCE ? "force" : "selective";
count = snprintf(mode_str, sizeof mode_str, "method:%s, unit:%s",
method_str, unit_str);
if (count >= HMR_MODE_TYPE_MAX_SIZE)
; /* the output was truncated, enlarge HMR_MODE_TYPE_MAX_SIZE */
/*
* |---------------------------------------------------------|
* | Header format template: |
* |---------------------------------------------------------|
* |<<-6->| | <-9->>| |<<-35-> |
* |HMR: | | status| |mode type |
* | |
* |---------------------------------------------------------|
* | Examples: |
* |---------------------------------------------------------|
* |HMR: | | started| |method:selective, unit:minimum |
* | | | | | |
* |---------------------------------------------------------|
* |HMR: | | started| |method:force, unit:full |
* | | | | | |
* |---------------------------------------------------------|
*/
printf("%-6s %9s %-30s\n", "HMR:", "started", mode_str);
fflush(stdout);
}
static inline void hmr_output_footer(int rc)
{
/*
* |---------------------------------------------------------|
* | Footer format template: |
* |---------------------------------------------------------|
* |<<-6->| | <-9->>| |<<-35-> |
* |HMR: | | status| |error msg / code |
* | |
* |---------------------------------------------------------|
* | Examples: |
* |---------------------------------------------------------|
* |HMR: | |completed| | OK |
* | | | | | |
* |---------------------------------------------------------|
* |HMR: | | stopped| |-115 |
* | | | | | |
* |---------------------------------------------------------|
*/
/*
* Leading LF is due to CR-ending on progress print out.
* In case of error LF was already sent with the error msg.
*/
if (0 == rc)
printf("\n%-6s %9s %-30s\n", "HMR:", "completed", "OK");
else
printf("%-6s %9s %-30d\n", "HMR:", "stopped", rc);
fflush(stdout);
}
static inline void hmr_query_error(int rc,
const char *job_type, /* write/read/set/clear/etc. */
const char *subject, /* desc/attr/flag/etc. */
int opcode, /* opcode r/w of desc/attr/fl etc. */
const char *field_name, /* name of the operated field */
int field_idn) /* index of the field */
{
print_error("hmr: query command: %s %s failed: "
"opcode 0x%x, field-name %s, field-idn 0x%x, rc %d.\n",
job_type, subject, opcode, field_name, field_idn);
}
static inline int hmr_attr_sanity(enum attr_idn idn)
{
if (idn < 0 || idn >= QUERY_ATTR_IDN_MAX)
return -EHMR_INVAL;
return EHMR_OK;
}
static inline int hmr_flag_sanity(enum flag_idn idn)
{
if (idn < 0 || idn >= QUERY_FLAG_IDN_MAX)
return -EHMR_INVAL;
return EHMR_OK;
}
static inline int hmr_desc_sanity(enum desc_idn idn)
{
if (idn < 0 || idn >= QUERY_DESC_IDN_MAX)
return -EHMR_INVAL;
return EHMR_OK;
}
static int hmr_dev_open(const char* path, int *fd)
{
int rc = 0;
errno = 0;
*fd = open(path, O_RDWR);
if (*fd < 0) {
rc = errno; /* save errno: errno can be changed by the print */
print_error("hmr: %s: '%s'", strerror(rc), path);
}
return rc == 0 ? rc : -rc;
}
static int hmr_dev_close(const char* path, int fd)
{
int rc;
errno = 0;
rc = close(fd);
if (rc) {
rc = errno;
print_error("hmr: %s: '%s'", strerror(rc), path);
}
return rc == 0 ? rc : -rc;
}
static int hmr_attr_read(__u32 *result,
int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp,
enum attr_idn idn)
{
int rc;
struct attr_fields *field;
if (hmr_attr_sanity(idn) != EHMR_OK || !result)
return -EHMR_INVAL;
field = &ufs_attrs[idn];
/* Query to read attribute */
rc = do_query_rq(fd,
bsg_req,
bsg_rsp,
UPIU_QUERY_FUNC_STANDARD_READ_REQUEST,
UPIU_QUERY_OPCODE_READ_ATTR,
idn,
0,
0,
0,
0,
0);
if (rc) {
hmr_query_error(rc, "read", "attr", UPIU_QUERY_OPCODE_READ_ATTR,
field->name, idn);
goto out;
}
*result = be32toh(bsg_rsp->upiu_rsp.qr.value);
out:
return rc;
}
static int hmr_attr_write(__u32 value,
int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp,
enum attr_idn idn)
{
int rc;
struct attr_fields *field;
if (hmr_attr_sanity(idn) != EHMR_OK)
return -EHMR_INVAL;
field = &ufs_attrs[idn];
bsg_req->upiu_req.qr.value = htobe32(value);
/* Query to write attribute */
rc = do_query_rq(fd,
bsg_req,
bsg_rsp,
UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST,
UPIU_QUERY_OPCODE_WRITE_ATTR,
idn,
0,
0,
0,
0,
0);
if (rc)
hmr_query_error(rc, "write", "attr", UPIU_QUERY_OPCODE_WRITE_ATTR,
field->name, idn);
return rc;
}
static int hmr_flag_modify(int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp,
enum query_opcode opcode,
enum flag_idn idn)
{
int rc;
struct flag_fields *field;
if (opcode != UPIU_QUERY_OPCODE_SET_FLAG &&
opcode != UPIU_QUERY_OPCODE_CLEAR_FLAG &&
opcode != UPIU_QUERY_OPCODE_TOGGLE_FLAG)
return -EHMR_INVAL;
if (hmr_flag_sanity(idn) != EHMR_OK)
return -EHMR_INVAL;
field = &ufs_flags[idn];
/* Query to set/clear/toggle flag */
rc = do_query_rq(fd,
bsg_req,
bsg_rsp,
UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST,
opcode,
idn,
0,
0,
0,
0,
0);
if (rc)
hmr_query_error(rc, "modify", "flag", opcode, field->name, idn);
return rc;
}
static int hmr_desc_read(struct descriptor *result,
int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp,
enum desc_idn idn)
{
int rc;
if (hmr_desc_sanity(idn) != EHMR_OK ||
!result ||
!result->layout ||
result->size <= 0)
return -EHMR_INVAL;
/* Query to read descriptor */
rc = do_query_rq(fd,
bsg_req,
bsg_rsp,
UPIU_QUERY_FUNC_STANDARD_READ_REQUEST,
UPIU_QUERY_OPCODE_READ_DESC,
idn,
0,
0,
0,
result->size,
result->layout);
if (rc)
hmr_query_error(rc, "read", "desc", UPIU_QUERY_OPCODE_READ_DESC,
result->name, idn);
return rc;
}
static int hmr_progress_read(__u32 *result,
int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp)
{
int rc;
struct descriptor *desc;
struct descriptor_health_layout *layout;
desc = &desc_health;
/* Read descriptor Health */
rc = hmr_desc_read(desc,
fd,
bsg_req,
bsg_rsp,
desc->idn);
if (rc)
goto out;
layout = desc->layout;
if (!HMR_QUIRK_REFRESH_PROGRESS_BYTE_ORDER)
*result = be32toh(layout->refresh_progress);
else
*result = layout->refresh_progress;
return rc;
out:
print_error("hmr: read progress: failed.");
return rc;
}
static int inline hmr_method_set(int fd, int method)
{
int rc;
struct ufs_bsg_request bsg_req = {0};
struct ufs_bsg_reply bsg_rsp = {0};
/* Set attribute bRefreshMethod - force or selective */
rc = hmr_attr_write(method,
fd, &bsg_req, &bsg_rsp, QUERY_ATTR_IDN_REFRESH_METHOD);
return rc;
}
static inline int hmr_unit_set(int fd, int unit)
{
int rc;
struct ufs_bsg_request bsg_req = {0};
struct ufs_bsg_reply bsg_rsp = {0};
/* Set attribute bRefreshUnit - minimum or full */
rc = hmr_attr_write(unit,
fd, &bsg_req, &bsg_rsp, QUERY_ATTR_IDN_REFRESH_UNIT);
return rc;
}
static inline int hmr_precondition_verify_status(int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp)
{
int rc;
int cur = 0;
int count = 10;
__u32 result;
retry:
/* Read refresh status */
rc = hmr_attr_read(&result,
fd,
bsg_req,
bsg_rsp,
QUERY_ATTR_IDN_REFRESH_STATUS);
if (rc)
goto out;
if (result != HMR_ST_IDLE) {
/* One time only */
if (0 == cur)
hmr_output_message("waiting idle status");
/* Retry */
if (++cur <= count) {
hmr_delay_retry(1);
goto retry;
}
/* Error */
print_error("hmr: precondition: "
"refresh status != 0x%x (0x%x)", HMR_ST_IDLE, result);
rc = -EHMR_REFRESH_STATUS;
}
out:
return rc;
}
static inline int hmr_precondition_validate_totcount(__u32 *result,
struct descriptor_health_layout *layout)
{
int rc = EHMR_OK;
__u32 refresh_totcount;
if (!result || !layout)
return -EHMR_INVAL;
if (!HMR_QUIRK_REFRESH_TOTCOUNT_BYTE_ORDER)
refresh_totcount = be32toh(layout->refresh_total_count);
else
refresh_totcount = layout->refresh_total_count;
if (refresh_totcount >= HMR_REFRESH_MAX_TOTCOUNT) {
print_error("hmr: precondition: "
"refresh total count >= max (0x%x >= 0x%x).",
refresh_totcount, HMR_REFRESH_MAX_TOTCOUNT);
rc = -EHMR_REFRESH_TOTCOUNT;
goto out;
}
*result = refresh_totcount;
out:
return rc;
}
static inline void hmr_output_wrong_run(int method, int unit)
{
print_error("hmr: precondition: device's hmr-progress is not empty. "
"Run is possible only with the method: %d, unit: %d",
method, unit);
}
static int hmr_precondition_verify_run(int fd,
int req_method, int req_unit, enum hmr_stage_skip *stage_passer)
{
int rc;
__u32 dev_method;
__u32 dev_unit;
__u32 dev_progress;
struct ufs_bsg_request bsg_req = {0};
struct ufs_bsg_reply bsg_rsp = {0};
if (!stage_passer)
return -EHMR_INVAL;
/*
* This code is intended to solve the case when the
* utility was run while HMR progress is not empty.
* It can happen, for example, when running the utility
* in one mode, then exit and run it again in some
* other mode, while the underline device's HMR status
* is still in progress.
*
* Let's make the policy simple:
* 1. Refresh progress is 0 -> OK.
* 2. Requested method equal device method
* and
* Requested unit equal device unit
* 1) Unit is minimum -> OK.
* 2) Unit is full -> OK, but have to skip the check for
* refresh status to be "idle", since
* in this mode it is "in-progress"
* until the HMR is completed.
* 3. Any other case is considered as violation.
*/
/* Read progress */
rc = hmr_progress_read(&dev_progress, fd, &bsg_req, &bsg_rsp);
if (rc)
goto out;
/* Clean run, may proceed with HMR */
if (0 == dev_progress)
goto out;
/* Read method */
rc = hmr_attr_read(&dev_method,
fd,
&bsg_req,
&bsg_rsp,
QUERY_ATTR_IDN_REFRESH_METHOD);
if (rc)
goto out;
/* Read unit */
rc = hmr_attr_read(&dev_unit,
fd,
&bsg_req,
&bsg_rsp,
QUERY_ATTR_IDN_REFRESH_UNIT);
if (rc)
goto out;
/* Method is different, set error */
if (req_method != dev_method) {
hmr_output_wrong_run(dev_method, dev_unit);
rc = -EHMR_REFRESH_UNIT;
goto out;
}
/* Unit is different, set error */
if (req_unit != dev_unit) {
hmr_output_wrong_run(dev_method, dev_unit);
rc = -EHMR_REFRESH_METHOD;
goto out;
}
/*
* The progress is not empty, and method/unit are the same.
*
* 1. Unit type is full? - skip the refresh status check,
* since it will never be completed/idle in this mode until ends.
* 2. Skip set method and unit, no need - the values are already set.
*/
if (dev_unit == HMR_UNIT_FULL)
*stage_passer |= HMR_SKIP_STATUS_CHECK;
*stage_passer |= (HMR_SKIP_METHOD_SET | HMR_SKIP_UNIT_SET);
out:
return rc;
}
static int hmr_precondition_verify(int fd,
struct tool_options *opt, __u32 *totcount,
enum hmr_stage_skip *stage_passer)
{
int rc;
struct descriptor *desc;
struct ufs_bsg_request bsg_req = {0};
struct ufs_bsg_reply bsg_rsp = {0};
if (!opt || !totcount || !stage_passer)
return -EHMR_INVAL;
/*
* 1. Verify the run perspective.
*
* In case of non-empty progress, make a decision
* if the run still can be proceeded.
*/
rc = hmr_precondition_verify_run(fd,
opt->hmr_method, opt->hmr_unit, stage_passer);
if (rc)
goto err;
/*
* 2. Verify refresh total count.
*
* Note: the Health descriptor was already read in
* hmr_precondition_verify_run(), so updated
* refresh total count is already available.
*/
desc = &desc_health;
rc = hmr_precondition_validate_totcount(totcount, desc->layout);
if (rc)
goto err;
/* Skip the refresh status check */
if (*stage_passer & HMR_SKIP_STATUS_CHECK)
goto success;
/*
* 3. Verify refresh status.
*
* Read refresh status.
* In case the status is not Idle - retry.
*
* We are expecting here to get the Idle status (0x00),
* but in some cases the status may be different:
* aborted (0x02), completed (0x03), etc.
*
* Usually, the statuses of this kind are saved until
* the first reading of the attribute is occured, then
* the value o the attribute should be change to Idle (0x00),
* but let's be generous and give it a sufficient number of
* retries.
*/
rc = hmr_precondition_verify_status(fd, &bsg_req, &bsg_rsp);
if (rc)
goto err;
success:
return rc;
err:
print_error("hmr: precondition failed.");
return rc;
}
static inline int hmr_postcondition_verify_progress(int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp)
{
int rc;
__u32 result;
/* Read Health descriptor, and get refresh progress field */
rc = hmr_progress_read(&result, fd, bsg_req, bsg_rsp);
if (rc)
goto out;
/* Progress should be 0 on HMR completion */
if (result != 0) {
print_error("hmr: postcondition: "
"refresh progress != 0x0 (0x%x).", result);
rc = -EHMR_REFRESH_PROGRESS;
goto out;
}
out:
return rc;
}
static inline int hmr_postcondition_verify_status(int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp)
{
int rc;
__u32 result;
/* Read refresh status */
rc = hmr_attr_read(&result,
fd,
bsg_req,
bsg_rsp,
QUERY_ATTR_IDN_REFRESH_STATUS);
if (rc)
goto out;
/* The accepted statuses are Completed and Idle */
if (result != HMR_ST_COMPLETED && result != HMR_ST_IDLE) {
print_error("hmr: postcondition: "
"refresh status != (0x%x or 0x%x) (0x%x)",
HMR_ST_COMPLETED, HMR_ST_IDLE, result);
rc = -EHMR_REFRESH_STATUS;
goto out;
}
out:
return rc;
}
static inline int hmr_postcondition_verify_totcount(__u32 prev_totcount,
struct descriptor_health_layout *layout)
{
int rc = EHMR_OK;
__u32 result;
if (!layout)
return -EHMR_INVAL;
if (!HMR_QUIRK_REFRESH_TOTCOUNT_BYTE_ORDER)
result = be32toh(layout->refresh_total_count);
else
result = layout->refresh_total_count;
if (result <= prev_totcount) {
print_error("hmr: postcondition: "
"refresh total count 0x%x <= 0x%x.",
prev_totcount, result);
rc = -EHMR_REFRESH_TOTCOUNT;
}
return rc;
}
static int hmr_postcondition_verify(int fd, __u32 totcount)
{
int rc;
struct ufs_bsg_request bsg_req = {0};
struct ufs_bsg_reply bsg_rsp = {0};
struct descriptor *desc;
desc = &desc_health;
/*
* 1. Verify refresh progress.
*
* Progress equal to 0 indicates refresh was completed.
*/
rc = hmr_postcondition_verify_progress(fd, &bsg_req, &bsg_rsp);
if (rc)
goto err;
/*
* 2. Check refresh status.
*
* The expected value is Completed (0x03) in first read,
* and Idle (0x00) in any following read (assuming no new
* HMR process begins).
* Perhaps, depending on the type of test, the first reading
* has already taken place. Therefore, we may consider both
* values to be valid.
*/
rc = hmr_postcondition_verify_status(fd, &bsg_req, &bsg_rsp);
if (rc)
goto err;
/*
* 3. Verify refresh total count.
*
* Refresh total count expected to be increased
* upon completion.
*
* The health descriptor was already read in step 1
* doing hmr_progress_read(). Just use its layout.
*/
rc = hmr_postcondition_verify_totcount(totcount, desc->layout);
if (rc)
goto err;
/* Success */
return rc;
err:
print_error("hmr: postcondition failed.");
return rc;
}
static inline int hmr_refresh_initiate_retry(int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp,
int cur,
int count)
{
int rc;
__u32 result;
if (cur > count)
return -EHMR_NORETRY;
/*
* Read refresh status.
* Then retry only in case the status is Busy.
*/
rc = hmr_attr_read(&result,
fd,
bsg_req,
bsg_rsp,
QUERY_ATTR_IDN_REFRESH_STATUS);
if (rc)
goto out;
/* Other than Busy - canot be retried */
if (result != HMR_ST_BUSY) {
rc = -EHMR_NORETRY;
goto out;
}
out:
/* 0 - can be retried, otherwise cannot */
return rc;
}
static int hmr_refresh_initiate(int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp)
{
int rc;
int err;
int cur = 0;
int count = 10;
/*
* Initiate refresh.
* For that, set flag fRefreshEnable to 1.
* In case device command queues are not empty,
* this query request may fail. In such a case
* the bRefreshStatus attribite shell be set to
* 0x04 - HMR_ST_BUSY. This case is handled by
* hmr_refresh_initiate_retry().
*/
retry:
rc = hmr_flag_modify(fd, bsg_req, bsg_rsp,
UPIU_QUERY_OPCODE_SET_FLAG,
QUERY_FLAG_IDN_REFRESH_ENABLE);
/* Success */
if (!rc)
goto out;
/* Check query can be retried */
err = hmr_refresh_initiate_retry(fd, bsg_req, bsg_rsp, ++cur, count);
if (!err) {
hmr_delay_retry(1);
goto retry;
}
print_error("hmr: initiate unit refresh: setting flag "
"fRefreshEnable failed after %d retries (%d).", count, rc);
out:
return rc;
}
static int hmr_unit_verify_completed(int fd,
struct ufs_bsg_request *bsg_req,
struct ufs_bsg_reply *bsg_rsp)
{
__u32 result;
int rc;
int cur = 0;
int count = 10;
/*
* Read refresh status.
*
* The expected status is Completed, which changes to Idle
* after it was read once. Thus, if a given attribute reads
* some other process, then we are in a race state and there
* is a possibility that the attribute value will be reset
* to Idle by another process. Thus, we will consider both
* statuses - Completed and Idle as valid at this stage.
*/
retry:
rc = hmr_attr_read(&result,
fd,
bsg_req,
bsg_rsp,
QUERY_ATTR_IDN_REFRESH_STATUS);
/* Error or (Completed / Idle) */
if (rc ||
result == HMR_ST_COMPLETED ||
result == HMR_ST_IDLE)
goto out;