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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2018-2019, Intel Corporation
 * Copyright (C) 2025, Altera Corporation
 */

#include <linux/arm-smccc.h>
#include <linux/bitfield.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/firmware/intel/stratix10-svc-client.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/string.h>
#include <linux/sysfs.h>

#define RSU_STATE_MASK			GENMASK_ULL(31, 0)
#define RSU_VERSION_MASK		GENMASK_ULL(63, 32)
#define RSU_ERROR_LOCATION_MASK		GENMASK_ULL(31, 0)
#define RSU_ERROR_DETAIL_MASK		GENMASK_ULL(63, 32)
/*
 * INTEL_SIP_SMC_RSU_GET_DEVICE_INFO packs each flash word as:
 *   [63:32] erase_size, [31:0] size (see stratix10-smc.h).
 */
#define RSU_DEVICE_INFO_SIZE_MASK	GENMASK_ULL(31, 0)
#define RSU_DEVICE_INFO_ERASE_SIZE_MASK	GENMASK_ULL(63, 32)

#define RSU_DCMF0_MASK			GENMASK_ULL(31, 0)
#define RSU_DCMF1_MASK			GENMASK_ULL(63, 32)
#define RSU_DCMF2_MASK			GENMASK_ULL(31, 0)
#define RSU_DCMF3_MASK			GENMASK_ULL(63, 32)
#define RSU_DCMF0_STATUS_MASK		GENMASK_ULL(15, 0)
#define RSU_DCMF1_STATUS_MASK		GENMASK_ULL(31, 16)
#define RSU_DCMF2_STATUS_MASK		GENMASK_ULL(47, 32)
#define RSU_DCMF3_STATUS_MASK		GENMASK_ULL(63, 48)

#define RSU_TIMEOUT	(msecs_to_jiffies(SVC_RSU_REQUEST_TIMEOUT_MS))

#define INVALID_RETRY_COUNTER		0xFF
#define INVALID_DCMF_VERSION		0xFF
#define INVALID_DCMF_STATUS		0xFFFFFFFF
#define INVALID_SPT_ADDRESS		0x0
#define INVALID_DEVICE_INFO		(~0U)

#define RSU_RETRY_SLEEP_MS		(1U)
#define RSU_ASYNC_MSG_RETRY		(3U)
#define RSU_GET_SPT_CMD			0x5A
#define RSU_GET_SPT_RESP_LEN		(4 * sizeof(unsigned int))

struct flash_device_info {
	unsigned int size;
	unsigned int erase_size;
};

/**
 * rsu_device_info_set_from_packed() - Decode one RSU device-info SMC word
 * @di: slot to fill
 * @packed: register value: [63:32] erase_size, [31:0] size
 *	(INTEL_SIP_SMC_RSU_GET_DEVICE_INFO)
 */
static void rsu_device_info_set_from_packed(struct flash_device_info *di,
					    unsigned long packed)
{
	di->size = (unsigned int)FIELD_GET(RSU_DEVICE_INFO_SIZE_MASK, packed);
	di->erase_size = (unsigned int)FIELD_GET(RSU_DEVICE_INFO_ERASE_SIZE_MASK,
						 packed);
}

typedef void (*rsu_callback)(struct stratix10_svc_client *client,
			     struct stratix10_svc_cb_data *data);
/**
 * struct stratix10_rsu_priv - rsu data structure
 * @chan: pointer to the allocated service channel
 * @client: active service client
 * @completion: state for callback completion
 * @lock: a mutex to protect callback completion state
 * @async: supports async operations
 * @status.current_image: address of image currently running in flash
 * @status.fail_image: address of failed image in flash
 * @status.version: the interface version number of RSU firmware
 * @status.state: the state of RSU system
 * @status.error_details: error code
 * @status.error_location: the error offset inside the image that failed
 * @dcmf_version.dcmf0: Quartus dcmf0 version
 * @dcmf_version.dcmf1: Quartus dcmf1 version
 * @dcmf_version.dcmf2: Quartus dcmf2 version
 * @dcmf_version.dcmf3: Quartus dcmf3 version
 * @dcmf_status.dcmf0: dcmf0 status
 * @dcmf_status.dcmf1: dcmf1 status
 * @dcmf_status.dcmf2: dcmf2 status
 * @dcmf_status.dcmf3: dcmf3 status
 * @device_info: per-device flash information array; each entry contains
 * size and erase size for one flash device
 * @retry_counter: the current image's retry counter
 * @max_retry: the preset max retry value
 * @spt0_address: address of spt0
 * @spt1_address: address of spt1
 * @get_spt_response_buf: response from sdm for get_spt command
 */
struct stratix10_rsu_priv {
	struct stratix10_svc_chan *chan;
	struct stratix10_svc_client client;
	struct completion completion;
	struct mutex lock;
	bool async;
	struct {
		unsigned long current_image;
		unsigned long fail_image;
		unsigned int version;
		unsigned int state;
		unsigned int error_details;
		unsigned int error_location;
	} status;

	struct {
		unsigned int dcmf0;
		unsigned int dcmf1;
		unsigned int dcmf2;
		unsigned int dcmf3;
	} dcmf_version;

	struct {
		unsigned int dcmf0;
		unsigned int dcmf1;
		unsigned int dcmf2;
		unsigned int dcmf3;
	} dcmf_status;

	struct flash_device_info device_info[4];

	unsigned int retry_counter;
	unsigned int max_retry;

	unsigned long spt0_address;
	unsigned long spt1_address;

	unsigned int *get_spt_response_buf;
};

/**
 * rsu_device_info_invalidate() - Mark all cached QSPI device slots invalid
 * @priv: RSU private data
 */
static void rsu_device_info_invalidate(struct stratix10_rsu_priv *priv)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(priv->device_info); i++) {
		priv->device_info[i].size = INVALID_DEVICE_INFO;
		priv->device_info[i].erase_size = INVALID_DEVICE_INFO;
	}
}

typedef void (*rsu_async_callback)(struct device *dev,
	struct stratix10_rsu_priv *priv, struct stratix10_svc_cb_data *data);

/**
 * rsu_status_callback() - Status callback from Intel Service Layer
 * @client: pointer to service client
 * @data: pointer to callback data structure
 *
 * Callback from Intel service layer for RSU status request. Status is
 * only updated after a system reboot, so a get updated status call is
 * made during driver probe.
 */
static void rsu_status_callback(struct stratix10_svc_client *client,
				struct stratix10_svc_cb_data *data)
{
	struct stratix10_rsu_priv *priv = client->priv;
	struct arm_smccc_res *res = (struct arm_smccc_res *)data->kaddr1;

	if (data->status == BIT(SVC_STATUS_OK)) {
		priv->status.version = FIELD_GET(RSU_VERSION_MASK,
						 res->a2);
		priv->status.state = FIELD_GET(RSU_STATE_MASK, res->a2);
		priv->status.fail_image = res->a1;
		priv->status.current_image = res->a0;
		priv->status.error_location =
			FIELD_GET(RSU_ERROR_LOCATION_MASK, res->a3);
		priv->status.error_details =
			FIELD_GET(RSU_ERROR_DETAIL_MASK, res->a3);
	} else {
		dev_err(client->dev, "COMMAND_RSU_STATUS returned 0x%lX\n",
			res->a0);
		priv->status.version = 0;
		priv->status.state = 0;
		priv->status.fail_image = 0;
		priv->status.current_image = 0;
		priv->status.error_location = 0;
		priv->status.error_details = 0;
	}

	complete(&priv->completion);
}

/**
 * rsu_async_status_callback() - Status callback from rsu_async_send()
 * @dev: pointer to device object
 * @priv: pointer to priv object
 * @data: pointer to callback data structure
 *
 * Callback from rsu_async_send() to get the system rsu error status.
 */
static void rsu_async_status_callback(struct device *dev,
				      struct stratix10_rsu_priv *priv,
				      struct stratix10_svc_cb_data *data)
{
	struct arm_smccc_1_2_regs *res = (struct arm_smccc_1_2_regs *)data->kaddr1;

	priv->status.current_image = res->a2;
	priv->status.fail_image = res->a3;
	priv->status.state = res->a4;
	priv->status.version = res->a5;
	priv->status.error_location = res->a7;
	priv->status.error_details = res->a8;
	priv->retry_counter = res->a9;
}

/**
 * rsu_command_callback() - Update callback from Intel Service Layer
 * @client: pointer to client
 * @data: pointer to callback data structure
 *
 * Callback from Intel service layer for RSU commands.
 */
static void rsu_command_callback(struct stratix10_svc_client *client,
				 struct stratix10_svc_cb_data *data)
{
	struct stratix10_rsu_priv *priv = client->priv;

	if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
		dev_warn(client->dev, "Secure FW doesn't support notify\n");
	else if (data->status == BIT(SVC_STATUS_ERROR))
		dev_err(client->dev, "Failure, returned status is %lu\n",
			BIT(data->status));

	complete(&priv->completion);
}

/**
 * rsu_retry_callback() - Callback from Intel service layer for getting
 * the current image's retry counter from the firmware
 * @client: pointer to client
 * @data: pointer to callback data structure
 *
 * Callback from Intel service layer for retry counter, which is used by
 * user to know how many times the images is still allowed to reload
 * itself before giving up and starting RSU fail-over flow.
 */
static void rsu_retry_callback(struct stratix10_svc_client *client,
			       struct stratix10_svc_cb_data *data)
{
	struct stratix10_rsu_priv *priv = client->priv;
	unsigned int *counter = (unsigned int *)data->kaddr1;

	if (data->status == BIT(SVC_STATUS_OK))
		priv->retry_counter = *counter;
	else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
		dev_warn(client->dev, "Secure FW doesn't support retry\n");
	else
		dev_err(client->dev, "Failed to get retry counter %lu\n",
			BIT(data->status));

	complete(&priv->completion);
}

/**
 * rsu_max_retry_callback() - Callback from Intel service layer for getting
 * the max retry value from the firmware
 * @client: pointer to client
 * @data: pointer to callback data structure
 *
 * Callback from Intel service layer for max retry.
 */
static void rsu_max_retry_callback(struct stratix10_svc_client *client,
				   struct stratix10_svc_cb_data *data)
{
	struct stratix10_rsu_priv *priv = client->priv;
	unsigned int *max_retry = (unsigned int *)data->kaddr1;

	if (data->status == BIT(SVC_STATUS_OK))
		priv->max_retry = *max_retry;
	else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
		dev_warn(client->dev, "Secure FW doesn't support max retry\n");
	else
		dev_err(client->dev, "Failed to get max retry %lu\n",
			BIT(data->status));

	complete(&priv->completion);
}

/**
 * rsu_dcmf_version_callback() - Callback from Intel service layer for getting
 * the DCMF version
 * @client: pointer to client
 * @data: pointer to callback data structure
 *
 * Callback from Intel service layer for DCMF version number
 */
static void rsu_dcmf_version_callback(struct stratix10_svc_client *client,
				      struct stratix10_svc_cb_data *data)
{
	struct stratix10_rsu_priv *priv = client->priv;
	unsigned long long *value1 = (unsigned long long *)data->kaddr1;
	unsigned long long *value2 = (unsigned long long *)data->kaddr2;

	if (data->status == BIT(SVC_STATUS_OK)) {
		priv->dcmf_version.dcmf0 = FIELD_GET(RSU_DCMF0_MASK, *value1);
		priv->dcmf_version.dcmf1 = FIELD_GET(RSU_DCMF1_MASK, *value1);
		priv->dcmf_version.dcmf2 = FIELD_GET(RSU_DCMF2_MASK, *value2);
		priv->dcmf_version.dcmf3 = FIELD_GET(RSU_DCMF3_MASK, *value2);
	} else
		dev_err(client->dev, "failed to get DCMF version\n");

	complete(&priv->completion);
}

/**
 * rsu_dcmf_status_callback() - Callback from Intel service layer for getting
 * the DCMF status
 * @client: pointer to client
 * @data: pointer to callback data structure
 *
 * Callback from Intel service layer for DCMF status
 */
static void rsu_dcmf_status_callback(struct stratix10_svc_client *client,
				     struct stratix10_svc_cb_data *data)
{
	struct stratix10_rsu_priv *priv = client->priv;
	unsigned long long *value = (unsigned long long *)data->kaddr1;

	if (data->status == BIT(SVC_STATUS_OK)) {
		priv->dcmf_status.dcmf0 = FIELD_GET(RSU_DCMF0_STATUS_MASK,
						    *value);
		priv->dcmf_status.dcmf1 = FIELD_GET(RSU_DCMF1_STATUS_MASK,
						    *value);
		priv->dcmf_status.dcmf2 = FIELD_GET(RSU_DCMF2_STATUS_MASK,
						    *value);
		priv->dcmf_status.dcmf3 = FIELD_GET(RSU_DCMF3_STATUS_MASK,
						    *value);
	} else
		dev_err(client->dev, "failed to get DCMF status\n");

	complete(&priv->completion);
}

/**
 * rsu_get_device_info_callback() - Callback from Intel service layer for
 * getting the QSPI device info
 * @client: pointer to client
 * @data: pointer to callback data structure
 *
 * Callback from Intel service layer for QSPI device info.
 * @data->kaddr1 points to struct arm_smccc_1_2_regs on SVC_STATUS_OK or
 * SVC_STATUS_ERROR; it is NULL on SVC_STATUS_NO_SUPPORT (unsupported command).
 */
static void rsu_get_device_info_callback(struct stratix10_svc_client *client,
					 struct stratix10_svc_cb_data *data)
{
	struct stratix10_rsu_priv *priv = client->priv;
	struct arm_smccc_1_2_regs *res = data->kaddr1;

	if (data->status == BIT(SVC_STATUS_OK)) {
		if (!res) {
			dev_err(client->dev,
				"COMMAND_RSU_GET_DEVICE_INFO: missing result payload\n");
			rsu_device_info_invalidate(priv);
			complete(&priv->completion);
			return;
		}

		rsu_device_info_set_from_packed(&priv->device_info[0], res->a1);
		rsu_device_info_set_from_packed(&priv->device_info[1], res->a2);
		rsu_device_info_set_from_packed(&priv->device_info[2], res->a3);
		rsu_device_info_set_from_packed(&priv->device_info[3], res->a4);

	} else if (data->status == BIT(SVC_STATUS_NO_SUPPORT)) {
		dev_warn(client->dev,
			 "COMMAND_RSU_GET_DEVICE_INFO not supported by firmware\n");
		rsu_device_info_invalidate(priv);
	} else {
		if (res)
			dev_err(client->dev,
				"COMMAND_RSU_GET_DEVICE_INFO returned 0x%lX\n",
				res->a0);
		else
			dev_err(client->dev,
				"COMMAND_RSU_GET_DEVICE_INFO failed with status 0x%X\n",
				data->status);
		rsu_device_info_invalidate(priv);
	}

	complete(&priv->completion);
}

/**
 * rsu_async_get_spt_table_callback() - Callback to be used by the
 * rsu_async_send() to retrieve the SPT table information.
 * @dev: pointer to device object
 * @priv: pointer to priv object
 * @data: pointer to callback data structure
 */
static void rsu_async_get_spt_table_callback(struct device *dev,
					     struct stratix10_rsu_priv *priv,
					     struct stratix10_svc_cb_data *data)
{
	priv->spt0_address = *((unsigned long *)data->kaddr1);
	priv->spt1_address = *((unsigned long *)data->kaddr2);
}

static void rsu_get_spt_callback(struct stratix10_svc_client *client,
				 struct stratix10_svc_cb_data *data)
{
	struct stratix10_rsu_priv *priv = client->priv;
	unsigned long *mbox_err = (unsigned long *)data->kaddr1;
	unsigned long *resp_len = (unsigned long *)data->kaddr2;

	if (data->status != BIT(SVC_STATUS_OK) || (*mbox_err) ||
	    (*resp_len != RSU_GET_SPT_RESP_LEN))
		goto error;

	priv->spt0_address = priv->get_spt_response_buf[0];
	priv->spt0_address <<= 32;
	priv->spt0_address |= priv->get_spt_response_buf[1];
	priv->spt1_address = priv->get_spt_response_buf[2];
	priv->spt1_address <<= 32;
	priv->spt1_address |= priv->get_spt_response_buf[3];

	goto complete;

error:
	dev_err(priv->client.dev,
		"failed to get SPTs (status=%#x, mbox_err=%lu, resp_len=%lu)\n",
		data->status, mbox_err ? *mbox_err : 0,
		resp_len ? *resp_len : 0);

complete:
	stratix10_svc_free_memory(priv->chan, priv->get_spt_response_buf);
	priv->get_spt_response_buf = NULL;
	complete(&priv->completion);
}

/**
 * __rsu_send_msg_locked() - send a message to Intel service layer
 * @priv: pointer to rsu private data
 * @command: RSU status or update command
 * @arg: the request argument, the bitstream address or notify status
 * @callback: function pointer for the callback (status or update)
 *
 * Perform the actual SMC transaction. The caller must hold @priv->lock.
 *
 * Returns 0 on success or a negative errno on failure.
 */
static int __rsu_send_msg_locked(struct stratix10_rsu_priv *priv,
				 enum stratix10_svc_command_code command,
				 unsigned long arg,
				 rsu_callback callback)
{
	struct stratix10_svc_client_msg msg = {0};
	int ret;

	lockdep_assert_held(&priv->lock);

	reinit_completion(&priv->completion);
	priv->client.receive_cb = callback;

	msg.command = command;
	if (arg)
		msg.arg[0] = arg;

	if (command == COMMAND_MBOX_SEND_CMD) {
		msg.arg[1] = 0;
		msg.payload = NULL;
		msg.payload_length = 0;
		msg.payload_output = priv->get_spt_response_buf;
		msg.payload_length_output = RSU_GET_SPT_RESP_LEN;
	}

	ret = stratix10_svc_send(priv->chan, &msg);
	if (ret < 0)
		goto status_done;

	ret = wait_for_completion_interruptible_timeout(&priv->completion,
							RSU_TIMEOUT);
	if (!ret) {
		dev_err(priv->client.dev,
			"timeout waiting for SMC call\n");
		ret = -ETIMEDOUT;
		goto status_done;
	} else if (ret < 0) {
		dev_err(priv->client.dev,
			"error %d waiting for SMC call\n", ret);
		goto status_done;
	} else {
		ret = 0;
	}

status_done:
	stratix10_svc_done(priv->chan);
	return ret;
}

/**
 * rsu_send_msg() - send a message to Intel service layer
 * @priv: pointer to rsu private data
 * @command: RSU status or update command
 * @arg: the request argument, the bitstream address or notify status
 * @callback: function pointer for the callback (status or update)
 *
 * Start an Intel service layer transaction to perform the SMC call that
 * is necessary to get RSU boot log or set the address of bitstream to
 * boot after reboot. This call will block until the RSU lock can be
 * acquired.
 *
 * Returns 0 on success or a negative errno on failure.
 */
static int rsu_send_msg(struct stratix10_rsu_priv *priv,
			enum stratix10_svc_command_code command,
			unsigned long arg,
			rsu_callback callback)
{
	int ret;

	mutex_lock(&priv->lock);
	ret = __rsu_send_msg_locked(priv, command, arg, callback);
	mutex_unlock(&priv->lock);
	return ret;
}

/**
 * rsu_try_send_msg() - non-blocking variant of rsu_send_msg()
 * @priv: pointer to rsu private data
 * @command: RSU status or update command
 * @arg: the request argument, the bitstream address or notify status
 * @callback: function pointer for the callback (status or update)
 *
 * Same as rsu_send_msg() but returns -EBUSY immediately when another
 * RSU operation is already in flight, instead of waiting for the lock.
 *
 * Returns 0 on success, -EBUSY if the RSU is busy, or another negative
 * errno on failure.
 */
static int rsu_try_send_msg(struct stratix10_rsu_priv *priv,
			    enum stratix10_svc_command_code command,
			    unsigned long arg,
			    rsu_callback callback)
{
	int ret;

	if (!mutex_trylock(&priv->lock))
		return -EBUSY;
	ret = __rsu_send_msg_locked(priv, command, arg, callback);
	mutex_unlock(&priv->lock);
	return ret;
}

/**
 * soc64_async_callback() - Callback from Intel service layer for async requests
 * @ptr: pointer to the completion object
 */
static void soc64_async_callback(void *ptr)
{
	if (ptr)
		complete(ptr);
}

/**
 * rsu_send_async_msg() - send an async message to Intel service layer
 * @dev: pointer to device object
 * @priv: pointer to rsu private data
 * @command: RSU status or update command
 * @arg: the request argument, notify status
 * @callback: function pointer for the callback (status or update)
 */
static int rsu_send_async_msg(struct device *dev, struct stratix10_rsu_priv *priv,
			      enum stratix10_svc_command_code command,
			      unsigned long arg,
			      rsu_async_callback callback)
{
	struct stratix10_svc_client_msg msg = {0};
	struct stratix10_svc_cb_data data = {0};
	struct completion completion;
	int status, index, ret;
	void *handle = NULL;

	msg.command = command;
	msg.arg[0] = arg;

	init_completion(&completion);

	for (index = 0; index < RSU_ASYNC_MSG_RETRY; index++) {
		status = stratix10_svc_async_send(priv->chan, &msg,
						  &handle, soc64_async_callback,
						  &completion);
		if (status == 0)
			break;
		dev_warn(dev, "Failed to send async message\n");
		msleep(RSU_RETRY_SLEEP_MS);
	}

	if (status && !handle) {
		dev_err(dev, "Failed to send async message\n");
		if (msg.payload_output)
			stratix10_svc_free_memory(priv->chan, msg.payload_output);
		return -ETIMEDOUT;
	}

	ret = wait_for_completion_io_timeout(&completion, RSU_TIMEOUT);
	if (ret > 0)
		dev_dbg(dev, "Received async interrupt\n");
	else if (ret == 0)
		dev_dbg(dev, "Timeout occurred. Trying to poll the response\n");

	for (index = 0; index < RSU_ASYNC_MSG_RETRY; index++) {
		status = stratix10_svc_async_poll(priv->chan, handle, &data);
		if (status == -EAGAIN) {
			dev_dbg(dev, "Async message is still in progress\n");
		} else if (status < 0) {
			dev_alert(dev, "Failed to poll async message\n");
			ret = -ETIMEDOUT;
		} else if (status == 0) {
			ret = 0;
			break;
		}
		msleep(RSU_RETRY_SLEEP_MS);
	}

	if (ret) {
		dev_err(dev, "Failed to get async response\n");
		goto status_done;
	}

	if (data.status == 0) {
		ret = 0;
		if (callback)
			callback(dev, priv, &data);
	} else {
		dev_err(dev, "%s returned 0x%x from SDM\n", __func__,
			data.status);
		ret = -EFAULT;
	}

status_done:
	if (msg.payload_output)
		stratix10_svc_free_memory(priv->chan, msg.payload_output);
	stratix10_svc_async_done(priv->chan, handle);
	return ret;
}

/*
 * This driver exposes some optional features of the Intel Stratix 10 SoC FPGA.
 * The sysfs interfaces exposed here are FPGA Remote System Update (RSU)
 * related. They allow user space software to query the configuration system
 * status and to request optional reboot behavior specific to Intel FPGAs.
 */

static ssize_t current_image_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08lx\n", priv->status.current_image);
}

static ssize_t fail_image_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08lx\n", priv->status.fail_image);
}

static ssize_t version_show(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08x\n", priv->status.version);
}

static ssize_t state_show(struct device *dev, struct device_attribute *attr,
			  char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08x\n", priv->status.state);
}

static ssize_t error_location_show(struct device *dev,
				   struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08x\n", priv->status.error_location);
}

static ssize_t error_details_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08x\n", priv->status.error_details);
}

static ssize_t retry_counter_show(struct device *dev,
				  struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08x\n", priv->retry_counter);
}

static ssize_t max_retry_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sysfs_emit(buf, "0x%08x\n", priv->max_retry);
}

static ssize_t dcmf0_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf0);
}

static ssize_t dcmf1_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf1);
}

static ssize_t dcmf2_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf2);
}

static ssize_t dcmf3_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf3);
}

static ssize_t dcmf0_status_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	if (priv->dcmf_status.dcmf0 == INVALID_DCMF_STATUS)
		return -EIO;

	return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf0);
}

static ssize_t dcmf1_status_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	if (priv->dcmf_status.dcmf1 == INVALID_DCMF_STATUS)
		return -EIO;

	return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf1);
}

static ssize_t dcmf2_status_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	if (priv->dcmf_status.dcmf2 == INVALID_DCMF_STATUS)
		return -EIO;

	return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf2);
}

static ssize_t dcmf3_status_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	if (priv->dcmf_status.dcmf3 == INVALID_DCMF_STATUS)
		return -EIO;

	return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf3);
}
static ssize_t reboot_image_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
	unsigned long address;
	int ret;

	if (!priv)
		return -ENODEV;

	ret = kstrtoul(buf, 0, &address);
	if (ret)
		return ret;

	/*
	 * Use the non-blocking variant so a write to this sysfs attribute
	 * does not stall the caller while another RSU operation is in
	 * flight. Userspace can retry on -EBUSY.
	 */
	ret = rsu_try_send_msg(priv, COMMAND_RSU_UPDATE,
			       address, rsu_command_callback);
	if (ret == -EBUSY) {
		dev_dbg(dev, "RSU busy, reboot_image write rejected\n");
		return ret;
	}
	if (ret) {
		dev_err(dev, "Error, RSU update returned %i\n", ret);
		return ret;
	}

	return count;
}

static ssize_t notify_store(struct device *dev,
			    struct device_attribute *attr,
			    const char *buf, size_t count)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
	unsigned long status;
	int ret;

	if (!priv)
		return -ENODEV;

	ret = kstrtoul(buf, 0, &status);
	if (ret)
		return ret;

	if (priv->async)
		ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_NOTIFY, status, NULL);
	else
		ret = rsu_send_msg(priv, COMMAND_RSU_NOTIFY, status, rsu_command_callback);
	if (ret) {
		dev_err(dev, "Error, RSU notify returned %i\n", ret);
		return ret;
	}

	/* to get the updated state */
	if (priv->async) {
		ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_STATUS, 0,
					 rsu_async_status_callback);
	} else {
		ret = rsu_send_msg(priv, COMMAND_RSU_STATUS, 0,
				   rsu_status_callback);
		/*
		 * In async mode COMMAND_RSU_RETRY is part of COMMAND_RSU_STATUS;
		 * issue it separately only in synchronous mode.
		 */
		if (!ret)
			ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0,
					   rsu_retry_callback);
	}
	if (ret) {
		dev_err(dev, "Error, getting RSU status %i\n", ret);
		return ret;
	}

	return count;
}

static ssize_t rsu_device_info_show(struct device *dev, char *buf,
				    unsigned int index, bool erase_size)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
	unsigned int value;

	if (!priv)
		return -ENODEV;

	if (index >= ARRAY_SIZE(priv->device_info))
		return -EINVAL;

	value = erase_size ? priv->device_info[index].erase_size :
			     priv->device_info[index].size;

	if (value == INVALID_DEVICE_INFO)
		return -EIO;

	return sysfs_emit(buf, "0x%08x\n", value);
}

static ssize_t size0_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	return rsu_device_info_show(dev, buf, 0, false);
}

static ssize_t size1_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	return rsu_device_info_show(dev, buf, 1, false);
}

static ssize_t size2_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	return rsu_device_info_show(dev, buf, 2, false);
}

static ssize_t size3_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	return rsu_device_info_show(dev, buf, 3, false);
}

static ssize_t erase_size0_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	return rsu_device_info_show(dev, buf, 0, true);
}

static ssize_t erase_size1_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	return rsu_device_info_show(dev, buf, 1, true);
}

static ssize_t erase_size2_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	return rsu_device_info_show(dev, buf, 2, true);
}

static ssize_t erase_size3_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	return rsu_device_info_show(dev, buf, 3, true);
}

static ssize_t spt0_address_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	if (priv->spt0_address == INVALID_SPT_ADDRESS)
		return -EIO;

	return sysfs_emit(buf, "0x%08lx\n", priv->spt0_address);
}

static ssize_t spt1_address_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);

	if (!priv)
		return -ENODEV;

	if (priv->spt1_address == INVALID_SPT_ADDRESS)
		return -EIO;

	return sysfs_emit(buf, "0x%08lx\n", priv->spt1_address);
}

static DEVICE_ATTR_RO(current_image);
static DEVICE_ATTR_RO(fail_image);
static DEVICE_ATTR_RO(state);
static DEVICE_ATTR_RO(version);
static DEVICE_ATTR_RO(error_location);
static DEVICE_ATTR_RO(error_details);
static DEVICE_ATTR_RO(retry_counter);
static DEVICE_ATTR_RO(max_retry);
static DEVICE_ATTR_RO(dcmf0);
static DEVICE_ATTR_RO(dcmf1);
static DEVICE_ATTR_RO(dcmf2);
static DEVICE_ATTR_RO(dcmf3);
static DEVICE_ATTR_RO(dcmf0_status);
static DEVICE_ATTR_RO(dcmf1_status);
static DEVICE_ATTR_RO(dcmf2_status);
static DEVICE_ATTR_RO(dcmf3_status);
static DEVICE_ATTR_RO(size0);
static DEVICE_ATTR_RO(size1);
static DEVICE_ATTR_RO(size2);
static DEVICE_ATTR_RO(size3);
static DEVICE_ATTR_RO(erase_size0);
static DEVICE_ATTR_RO(erase_size1);
static DEVICE_ATTR_RO(erase_size2);
static DEVICE_ATTR_RO(erase_size3);
static DEVICE_ATTR_WO(reboot_image);
static DEVICE_ATTR_WO(notify);
static DEVICE_ATTR_RO(spt0_address);
static DEVICE_ATTR_RO(spt1_address);

static struct attribute *rsu_attrs[] = {
	&dev_attr_current_image.attr,
	&dev_attr_fail_image.attr,
	&dev_attr_state.attr,
	&dev_attr_version.attr,
	&dev_attr_error_location.attr,
	&dev_attr_error_details.attr,
	&dev_attr_retry_counter.attr,
	&dev_attr_max_retry.attr,
	&dev_attr_dcmf0.attr,
	&dev_attr_dcmf1.attr,
	&dev_attr_dcmf2.attr,
	&dev_attr_dcmf3.attr,
	&dev_attr_dcmf0_status.attr,
	&dev_attr_dcmf1_status.attr,
	&dev_attr_dcmf2_status.attr,
	&dev_attr_dcmf3_status.attr,
	&dev_attr_size0.attr,
	&dev_attr_size1.attr,
	&dev_attr_size2.attr,
	&dev_attr_size3.attr,
	&dev_attr_erase_size0.attr,
	&dev_attr_erase_size1.attr,
	&dev_attr_erase_size2.attr,
	&dev_attr_erase_size3.attr,
	&dev_attr_reboot_image.attr,
	&dev_attr_notify.attr,
	&dev_attr_spt0_address.attr,
	&dev_attr_spt1_address.attr,
	NULL
};

ATTRIBUTE_GROUPS(rsu);

static int stratix10_rsu_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct stratix10_rsu_priv *priv;
	int ret;

	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->client.dev = dev;
	priv->client.priv = priv;
	priv->retry_counter = INVALID_RETRY_COUNTER;
	priv->max_retry = INVALID_RETRY_COUNTER;
	priv->dcmf_version.dcmf0 = INVALID_DCMF_VERSION;
	priv->dcmf_version.dcmf1 = INVALID_DCMF_VERSION;
	priv->dcmf_version.dcmf2 = INVALID_DCMF_VERSION;
	priv->dcmf_version.dcmf3 = INVALID_DCMF_VERSION;
	priv->dcmf_status.dcmf0 = INVALID_DCMF_STATUS;
	priv->dcmf_status.dcmf1 = INVALID_DCMF_STATUS;
	priv->dcmf_status.dcmf2 = INVALID_DCMF_STATUS;
	priv->dcmf_status.dcmf3 = INVALID_DCMF_STATUS;
	priv->max_retry = INVALID_RETRY_COUNTER;
	priv->spt0_address = INVALID_SPT_ADDRESS;
	priv->spt1_address = INVALID_SPT_ADDRESS;
	priv->get_spt_response_buf = NULL;
	priv->async = false;
	rsu_device_info_invalidate(priv);

	mutex_init(&priv->lock);
	init_completion(&priv->completion);

	priv->chan = stratix10_svc_request_channel_byname(&priv->client,
							  SVC_CLIENT_RSU);
	if (IS_ERR(priv->chan)) {
		dev_err(dev, "couldn't get service channel %s\n",
			SVC_CLIENT_RSU);
		return PTR_ERR(priv->chan);
	}

	ret = stratix10_svc_add_async_client(priv->chan, false);
	if (ret < 0) {
		dev_dbg(dev, "Async operations not supported, fallback to non-async mode\n");
		priv->async = false;
	} else {
		priv->async = true;
	}

	platform_set_drvdata(pdev, priv);

	/* get the initial state from firmware */
	if (priv->async)
		ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_STATUS, 0,
					 rsu_async_status_callback);
	else
		ret = rsu_send_msg(priv, COMMAND_RSU_STATUS, 0,
				   rsu_status_callback);
	if (ret) {
		dev_err(dev, "Error, getting RSU status %i\n", ret);
		if (priv->async)
			goto remove_async_client;
		goto free_channel;
	}

	/*
	 * In async mode COMMAND_RSU_RETRY is part of COMMAND_RSU_STATUS;
	 * issue it separately only in synchronous mode.
	 */
	if (!priv->async) {
		ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
		if (ret) {
			dev_err(dev, "Error, getting RSU retry %i\n", ret);
			goto free_channel;
		}
	}

	/* get DCMF version from firmware */
	ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_VERSION, 0,
			   rsu_dcmf_version_callback);
	if (ret) {
		dev_err(dev, "Error, getting DCMF version %i\n", ret);
		if (priv->async)
			goto remove_async_client;
		goto free_channel;
	}

	ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_STATUS, 0,
			   rsu_dcmf_status_callback);
	if (ret) {
		dev_err(dev, "Error, getting DCMF status %i\n", ret);
		if (priv->async)
			goto remove_async_client;
		goto free_channel;
	}

	ret = rsu_send_msg(priv, COMMAND_RSU_MAX_RETRY, 0,
			   rsu_max_retry_callback);
	if (ret) {
		dev_err(dev, "Error, getting RSU max retry %i\n", ret);
		if (priv->async)
			goto remove_async_client;
		goto free_channel;
	}

	/* get QSPI device info from firmware */
	ret = rsu_send_msg(priv, COMMAND_RSU_GET_DEVICE_INFO, 0,
			   rsu_get_device_info_callback);
	if (ret) {
		dev_err(dev, "Error, getting QSPI Device Info %i\n", ret);
		if (priv->async)
			goto remove_async_client;
		goto free_channel;
	}

	if (priv->async) {
		ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_GET_SPT_TABLE,
					 0, rsu_async_get_spt_table_callback);
	} else {
		priv->get_spt_response_buf =
			stratix10_svc_allocate_memory(priv->chan, RSU_GET_SPT_RESP_LEN);
		if (IS_ERR(priv->get_spt_response_buf)) {
			ret = PTR_ERR(priv->get_spt_response_buf);
			priv->get_spt_response_buf = NULL;
			dev_err(dev, "failed to allocate get spt buffer\n");
		} else {
			ret = rsu_send_msg(priv, COMMAND_MBOX_SEND_CMD,
					   RSU_GET_SPT_CMD, rsu_get_spt_callback);
		}
	}
	if (ret) {
		dev_err(dev, "Error, getting SPT table %i\n", ret);
		if (priv->async) {
			goto remove_async_client;
		} else if (!IS_ERR_OR_NULL(priv->get_spt_response_buf)) {
			stratix10_svc_free_memory(priv->chan,
						  priv->get_spt_response_buf);
			priv->get_spt_response_buf = NULL;
		}
		goto free_channel;
	}

	return 0;

remove_async_client:
	stratix10_svc_remove_async_client(priv->chan);
free_channel:
	stratix10_svc_free_channel(priv->chan);
	return ret;
}

static void stratix10_rsu_remove(struct platform_device *pdev)
{
	struct stratix10_rsu_priv *priv = platform_get_drvdata(pdev);

	if (priv->async)
		stratix10_svc_remove_async_client(priv->chan);

	stratix10_svc_free_channel(priv->chan);
}

static struct platform_driver stratix10_rsu_driver = {
	.probe = stratix10_rsu_probe,
	.remove = stratix10_rsu_remove,
	.driver = {
		.name = "stratix10-rsu",
		.dev_groups = rsu_groups,
	},
};

module_platform_driver(stratix10_rsu_driver);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Intel Remote System Update Driver");
MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");