forked from cornelisnetworks/opa-hfi1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirmware.c
2065 lines (1809 loc) · 57 KB
/
firmware.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright(c) 2015, 2016 Intel Corporation.
*
* This file is provided under a dual BSD/GPLv2 license. When using or
* redistributing this file, you may do so under either license.
*
* GPL LICENSE SUMMARY
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* BSD LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* - Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <linux/firmware.h>
#include <linux/mutex.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/crc32.h>
#include "hfi.h"
#include "trace.h"
/*
* Make it easy to toggle firmware file name and if it gets loaded by
* editing the following. This may be something we do while in development
* but not necessarily something a user would ever need to use.
*/
#define DEFAULT_FW_8051_NAME_FPGA "hfi_dc8051.bin"
#define DEFAULT_FW_8051_NAME_ASIC "hfi1_dc8051.fw"
#define DEFAULT_FW_FABRIC_NAME "hfi1_fabric.fw"
#define DEFAULT_FW_SBUS_NAME "hfi1_sbus.fw"
#define DEFAULT_FW_PCIE_NAME "hfi1_pcie.fw"
#define DEFAULT_PLATFORM_CONFIG_NAME "hfi1_platform.dat"
#define ALT_FW_8051_NAME_ASIC "hfi1_dc8051_d.fw"
#define ALT_FW_FABRIC_NAME "hfi1_fabric_d.fw"
#define ALT_FW_SBUS_NAME "hfi1_sbus_d.fw"
#define ALT_FW_PCIE_NAME "hfi1_pcie_d.fw"
static uint fw_8051_load = 1;
static uint fw_fabric_serdes_load = 1;
static uint fw_pcie_serdes_load = 1;
static uint fw_sbus_load = 1;
/*
* Access required in platform.c
* Maintains state of whether the platform config was fetched via the
* fallback option
*/
uint platform_config_load;
/* Firmware file names get set in hfi1_firmware_init() based on the above */
static char *fw_8051_name;
static char *fw_fabric_serdes_name;
static char *fw_sbus_name;
static char *fw_pcie_serdes_name;
static char *platform_config_name;
#define SBUS_MAX_POLL_COUNT 100
#define SBUS_COUNTER(reg, name) \
(((reg) >> ASIC_STS_SBUS_COUNTERS_##name##_CNT_SHIFT) & \
ASIC_STS_SBUS_COUNTERS_##name##_CNT_MASK)
/*
* Firmware security header.
*/
struct css_header {
u32 module_type;
u32 header_len;
u32 header_version;
u32 module_id;
u32 module_vendor;
u32 date; /* BCD yyyymmdd */
u32 size; /* in DWORDs */
u32 key_size; /* in DWORDs */
u32 modulus_size; /* in DWORDs */
u32 exponent_size; /* in DWORDs */
u32 reserved[22];
};
/* expected field values */
#define CSS_MODULE_TYPE 0x00000006
#define CSS_HEADER_LEN 0x000000a1
#define CSS_HEADER_VERSION 0x00010000
#define CSS_MODULE_VENDOR 0x00008086
#define KEY_SIZE 256
#define MU_SIZE 8
#define EXPONENT_SIZE 4
/* the file itself */
struct firmware_file {
struct css_header css_header;
u8 modulus[KEY_SIZE];
u8 exponent[EXPONENT_SIZE];
u8 signature[KEY_SIZE];
u8 firmware[];
};
struct augmented_firmware_file {
struct css_header css_header;
u8 modulus[KEY_SIZE];
u8 exponent[EXPONENT_SIZE];
u8 signature[KEY_SIZE];
u8 r2[KEY_SIZE];
u8 mu[MU_SIZE];
u8 firmware[];
};
/* augmented file size difference */
#define AUGMENT_SIZE (sizeof(struct augmented_firmware_file) - \
sizeof(struct firmware_file))
struct firmware_details {
/* Linux core piece */
const struct firmware *fw;
struct css_header *css_header;
u8 *firmware_ptr; /* pointer to binary data */
u32 firmware_len; /* length in bytes */
u8 *modulus; /* pointer to the modulus */
u8 *exponent; /* pointer to the exponent */
u8 *signature; /* pointer to the signature */
u8 *r2; /* pointer to r2 */
u8 *mu; /* pointer to mu */
struct augmented_firmware_file dummy_header;
};
/*
* The mutex protects fw_state, fw_err, and all of the firmware_details
* variables.
*/
static DEFINE_MUTEX(fw_mutex);
enum fw_state {
FW_EMPTY,
FW_TRY,
FW_FINAL,
FW_ERR
};
static enum fw_state fw_state = FW_EMPTY;
static int fw_err;
static struct firmware_details fw_8051;
static struct firmware_details fw_fabric;
static struct firmware_details fw_pcie;
static struct firmware_details fw_sbus;
static const struct firmware *platform_config;
/* flags for turn_off_spicos() */
#define SPICO_SBUS 0x1
#define SPICO_FABRIC 0x2
#define ENABLE_SPICO_SMASK 0x1
/* security block commands */
#define RSA_CMD_INIT 0x1
#define RSA_CMD_START 0x2
/* security block status */
#define RSA_STATUS_IDLE 0x0
#define RSA_STATUS_ACTIVE 0x1
#define RSA_STATUS_DONE 0x2
#define RSA_STATUS_FAILED 0x3
/* RSA engine timeout, in ms */
#define RSA_ENGINE_TIMEOUT 100 /* ms */
/* hardware mutex timeout, in ms */
#define HM_TIMEOUT 10 /* ms */
/* 8051 memory access timeout, in us */
#define DC8051_ACCESS_TIMEOUT 100 /* us */
/* the number of fabric SerDes on the SBus */
#define NUM_FABRIC_SERDES 4
/* SBus fabric SerDes addresses, one set per HFI */
static const u8 fabric_serdes_addrs[2][NUM_FABRIC_SERDES] = {
{ 0x01, 0x02, 0x03, 0x04 },
{ 0x28, 0x29, 0x2a, 0x2b }
};
/* SBus PCIe SerDes addresses, one set per HFI */
static const u8 pcie_serdes_addrs[2][NUM_PCIE_SERDES] = {
{ 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16,
0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26 },
{ 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d,
0x3f, 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d }
};
/* SBus PCIe PCS addresses, one set per HFI */
const u8 pcie_pcs_addrs[2][NUM_PCIE_SERDES] = {
{ 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17,
0x19, 0x1b, 0x1d, 0x1f, 0x21, 0x23, 0x25, 0x27 },
{ 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e }
};
/* SBus fabric SerDes broadcast addresses, one per HFI */
static const u8 fabric_serdes_broadcast[2] = { 0xe4, 0xe5 };
static const u8 all_fabric_serdes_broadcast = 0xe1;
/* SBus PCIe SerDes broadcast addresses, one per HFI */
const u8 pcie_serdes_broadcast[2] = { 0xe2, 0xe3 };
static const u8 all_pcie_serdes_broadcast = 0xe0;
/* forwards */
static void dispose_one_firmware(struct firmware_details *fdet);
static int load_fabric_serdes_firmware(struct hfi1_devdata *dd,
struct firmware_details *fdet);
/*
* Read a single 64-bit value from 8051 data memory.
*
* Expects:
* o caller to have already set up data read, no auto increment
* o caller to turn off read enable when finished
*
* The address argument is a byte offset. Bits 0:2 in the address are
* ignored - i.e. the hardware will always do aligned 8-byte reads as if
* the lower bits are zero.
*
* Return 0 on success, -ENXIO on a read error (timeout).
*/
static int __read_8051_data(struct hfi1_devdata *dd, u32 addr, u64 *result)
{
u64 reg;
int count;
/* start the read at the given address */
reg = ((addr & DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK)
<< DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT)
| DC_DC8051_CFG_RAM_ACCESS_CTRL_READ_ENA_SMASK;
write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, reg);
/* wait until ACCESS_COMPLETED is set */
count = 0;
while ((read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_STATUS)
& DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK)
== 0) {
count++;
if (count > DC8051_ACCESS_TIMEOUT) {
dd_dev_err(dd, "timeout reading 8051 data\n");
return -ENXIO;
}
ndelay(10);
}
/* gather the data */
*result = read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_RD_DATA);
return 0;
}
/*
* Read 8051 data starting at addr, for len bytes. Will read in 8-byte chunks.
* Return 0 on success, -errno on error.
*/
int read_8051_data(struct hfi1_devdata *dd, u32 addr, u32 len, u64 *result)
{
unsigned long flags;
u32 done;
int ret = 0;
spin_lock_irqsave(&dd->dc8051_memlock, flags);
/* data read set-up, no auto-increment */
write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, 0);
for (done = 0; done < len; addr += 8, done += 8, result++) {
ret = __read_8051_data(dd, addr, result);
if (ret)
break;
}
/* turn off read enable */
write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, 0);
spin_unlock_irqrestore(&dd->dc8051_memlock, flags);
return ret;
}
/*
* Write data or code to the 8051 code or data RAM.
*/
static int write_8051(struct hfi1_devdata *dd, int code, u32 start,
const u8 *data, u32 len)
{
u64 reg;
u32 offset;
int aligned, count;
/* check alignment */
aligned = ((unsigned long)data & 0x7) == 0;
/* write set-up */
reg = (code ? DC_DC8051_CFG_RAM_ACCESS_SETUP_RAM_SEL_SMASK : 0ull)
| DC_DC8051_CFG_RAM_ACCESS_SETUP_AUTO_INCR_ADDR_SMASK;
write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, reg);
reg = ((start & DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK)
<< DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT)
| DC_DC8051_CFG_RAM_ACCESS_CTRL_WRITE_ENA_SMASK;
write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, reg);
/* write */
for (offset = 0; offset < len; offset += 8) {
int bytes = len - offset;
if (bytes < 8) {
reg = 0;
memcpy(®, &data[offset], bytes);
} else if (aligned) {
reg = *(u64 *)&data[offset];
} else {
memcpy(®, &data[offset], 8);
}
write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_WR_DATA, reg);
/* wait until ACCESS_COMPLETED is set */
count = 0;
while ((read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_STATUS)
& DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK)
== 0) {
count++;
if (count > DC8051_ACCESS_TIMEOUT) {
dd_dev_err(dd, "timeout writing 8051 data\n");
return -ENXIO;
}
udelay(1);
}
}
/* turn off write access, auto increment (also sets to data access) */
write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, 0);
write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, 0);
return 0;
}
/* return 0 if values match, non-zero and complain otherwise */
static int invalid_header(struct hfi1_devdata *dd, const char *what,
u32 actual, u32 expected)
{
if (actual == expected)
return 0;
dd_dev_err(dd,
"invalid firmware header field %s: expected 0x%x, actual 0x%x\n",
what, expected, actual);
return 1;
}
/*
* Verify that the static fields in the CSS header match.
*/
static int verify_css_header(struct hfi1_devdata *dd, struct css_header *css)
{
/* verify CSS header fields (most sizes are in DW, so add /4) */
if (invalid_header(dd, "module_type", css->module_type,
CSS_MODULE_TYPE) ||
invalid_header(dd, "header_len", css->header_len,
(sizeof(struct firmware_file) / 4)) ||
invalid_header(dd, "header_version",
css->header_version, CSS_HEADER_VERSION) ||
invalid_header(dd, "module_vendor",
css->module_vendor, CSS_MODULE_VENDOR) ||
invalid_header(dd, "key_size", css->key_size, KEY_SIZE / 4) ||
invalid_header(dd, "modulus_size",
css->modulus_size, KEY_SIZE / 4) ||
invalid_header(dd, "exponent_size", css->exponent_size,
EXPONENT_SIZE / 4)) {
return -EINVAL;
}
return 0;
}
/*
* Make sure there are at least some bytes after the prefix.
*/
static int payload_check(struct hfi1_devdata *dd, const char *name,
long file_size, long prefix_size)
{
/* make sure we have some payload */
if (prefix_size >= file_size) {
dd_dev_err(dd,
"firmware \"%s\", size %ld, must be larger than %ld bytes\n",
name, file_size, prefix_size);
return -EINVAL;
}
return 0;
}
/*
* Request the firmware from the system. Extract the pieces and fill in
* fdet. If successful, the caller will need to call dispose_one_firmware().
* Returns 0 on success, -ERRNO on error.
*/
static int obtain_one_firmware(struct hfi1_devdata *dd, const char *name,
struct firmware_details *fdet)
{
struct css_header *css;
int ret;
memset(fdet, 0, sizeof(*fdet));
ret = request_firmware(&fdet->fw, name, &dd->pcidev->dev);
if (ret) {
dd_dev_warn(dd, "cannot find firmware \"%s\", err %d\n",
name, ret);
return ret;
}
/* verify the firmware */
if (fdet->fw->size < sizeof(struct css_header)) {
dd_dev_err(dd, "firmware \"%s\" is too small\n", name);
ret = -EINVAL;
goto done;
}
css = (struct css_header *)fdet->fw->data;
hfi1_cdbg(FIRMWARE, "Firmware %s details:", name);
hfi1_cdbg(FIRMWARE, "file size: 0x%lx bytes", fdet->fw->size);
hfi1_cdbg(FIRMWARE, "CSS structure:");
hfi1_cdbg(FIRMWARE, " module_type 0x%x", css->module_type);
hfi1_cdbg(FIRMWARE, " header_len 0x%03x (0x%03x bytes)",
css->header_len, 4 * css->header_len);
hfi1_cdbg(FIRMWARE, " header_version 0x%x", css->header_version);
hfi1_cdbg(FIRMWARE, " module_id 0x%x", css->module_id);
hfi1_cdbg(FIRMWARE, " module_vendor 0x%x", css->module_vendor);
hfi1_cdbg(FIRMWARE, " date 0x%x", css->date);
hfi1_cdbg(FIRMWARE, " size 0x%03x (0x%03x bytes)",
css->size, 4 * css->size);
hfi1_cdbg(FIRMWARE, " key_size 0x%03x (0x%03x bytes)",
css->key_size, 4 * css->key_size);
hfi1_cdbg(FIRMWARE, " modulus_size 0x%03x (0x%03x bytes)",
css->modulus_size, 4 * css->modulus_size);
hfi1_cdbg(FIRMWARE, " exponent_size 0x%03x (0x%03x bytes)",
css->exponent_size, 4 * css->exponent_size);
hfi1_cdbg(FIRMWARE, "firmware size: 0x%lx bytes",
fdet->fw->size - sizeof(struct firmware_file));
/*
* If the file does not have a valid CSS header, fail.
* Otherwise, check the CSS size field for an expected size.
* The augmented file has r2 and mu inserted after the header
* was generated, so there will be a known difference between
* the CSS header size and the actual file size. Use this
* difference to identify an augmented file.
*
* Note: css->size is in DWORDs, multiply by 4 to get bytes.
*/
ret = verify_css_header(dd, css);
if (ret) {
dd_dev_info(dd, "Invalid CSS header for \"%s\"\n", name);
} else {
if ((css->size * 4) == fdet->fw->size) {
/* non-augmented firmware file */
struct firmware_file *ff = (struct firmware_file *)
fdet->fw->data;
/* make sure there are bytes in the payload */
ret = payload_check(dd, name, fdet->fw->size,
sizeof(struct firmware_file));
if (ret == 0) {
fdet->css_header = css;
fdet->modulus = ff->modulus;
fdet->exponent = ff->exponent;
fdet->signature = ff->signature;
/* use dummy space */
fdet->r2 = fdet->dummy_header.r2;
/* use dummy space */
fdet->mu = fdet->dummy_header.mu;
fdet->firmware_ptr = ff->firmware;
fdet->firmware_len = fdet->fw->size -
sizeof(struct firmware_file);
/*
* Header does not include r2 and mu -
* generate here.
* For now, fail.
*/
dd_dev_err(dd, "driver is unable to validate firmware without r2 and mu (not in firmware file)\n");
ret = -EINVAL;
}
} else {
if ((css->size * 4) + AUGMENT_SIZE == fdet->fw->size) {
/* augmented firmware file */
struct augmented_firmware_file *aff =
(struct augmented_firmware_file *)
fdet->fw->data;
/* make sure there are bytes in the payload */
ret = payload_check(dd, name, fdet->fw->size,
sizeof
(struct
augmented_firmware_file));
if (ret == 0) {
fdet->css_header = css;
fdet->modulus = aff->modulus;
fdet->exponent = aff->exponent;
fdet->signature = aff->signature;
fdet->r2 = aff->r2;
fdet->mu = aff->mu;
fdet->firmware_ptr = aff->firmware;
fdet->firmware_len = fdet->fw->size -
sizeof(struct augmented_firmware_file);
}
} else {
/* css->size check failed */
dd_dev_err(dd,
"invalid firmware header field size: expected 0x%lx or 0x%lx, actual 0x%x\n",
fdet->fw->size / 4,
(fdet->fw->size - AUGMENT_SIZE) / 4,
css->size);
ret = -EINVAL;
}
}
}
done:
/* if returning an error, clean up after ourselves */
if (ret)
dispose_one_firmware(fdet);
return ret;
}
static void dispose_one_firmware(struct firmware_details *fdet)
{
release_firmware(fdet->fw);
/* erase all previous information */
memset(fdet, 0, sizeof(*fdet));
}
/*
* Obtain the 4 firmwares from the OS. All must be obtained at once or not
* at all. If called with the firmware state in FW_TRY, use alternate names.
* On exit, this routine will have set the firmware state to one of FW_TRY,
* FW_FINAL, or FW_ERR.
*
* Must be holding fw_mutex.
*/
static void __obtain_firmware(struct hfi1_devdata *dd)
{
int err = 0;
if (fw_state == FW_FINAL) /* nothing more to obtain */
return;
if (fw_state == FW_ERR) /* already in error */
return;
/* fw_state is FW_EMPTY or FW_TRY */
retry:
if (fw_state == FW_TRY) {
/*
* We tried the original and it failed. Move to the
* alternate.
*/
dd_dev_warn(dd, "using alternate firmware names\n");
/*
* Let others run. Some systems, when missing firmware, does
* something that holds for 30 seconds. If we do that twice
* in a row it triggers task blocked warning.
*/
cond_resched();
if (fw_8051_load)
dispose_one_firmware(&fw_8051);
if (fw_fabric_serdes_load)
dispose_one_firmware(&fw_fabric);
if (fw_sbus_load)
dispose_one_firmware(&fw_sbus);
if (fw_pcie_serdes_load)
dispose_one_firmware(&fw_pcie);
fw_8051_name = ALT_FW_8051_NAME_ASIC;
fw_fabric_serdes_name = ALT_FW_FABRIC_NAME;
fw_sbus_name = ALT_FW_SBUS_NAME;
fw_pcie_serdes_name = ALT_FW_PCIE_NAME;
}
if (fw_sbus_load) {
err = obtain_one_firmware(dd, fw_sbus_name, &fw_sbus);
if (err)
goto done;
}
if (fw_pcie_serdes_load) {
err = obtain_one_firmware(dd, fw_pcie_serdes_name, &fw_pcie);
if (err)
goto done;
}
if (fw_fabric_serdes_load) {
err = obtain_one_firmware(dd, fw_fabric_serdes_name,
&fw_fabric);
if (err)
goto done;
}
if (fw_8051_load) {
err = obtain_one_firmware(dd, fw_8051_name, &fw_8051);
if (err)
goto done;
}
done:
if (err) {
/* oops, had problems obtaining a firmware */
if (fw_state == FW_EMPTY && dd->icode == ICODE_RTL_SILICON) {
/* retry with alternate (RTL only) */
fw_state = FW_TRY;
goto retry;
}
dd_dev_err(dd, "unable to obtain working firmware\n");
fw_state = FW_ERR;
fw_err = -ENOENT;
} else {
/* success */
if (fw_state == FW_EMPTY &&
dd->icode != ICODE_FUNCTIONAL_SIMULATOR)
fw_state = FW_TRY; /* may retry later */
else
fw_state = FW_FINAL; /* cannot try again */
}
}
/*
* Called by all HFIs when loading their firmware - i.e. device probe time.
* The first one will do the actual firmware load. Use a mutex to resolve
* any possible race condition.
*
* The call to this routine cannot be moved to driver load because the kernel
* call request_firmware() requires a device which is only available after
* the first device probe.
*/
static int obtain_firmware(struct hfi1_devdata *dd)
{
unsigned long timeout;
int err = 0;
mutex_lock(&fw_mutex);
/* 40s delay due to long delay on missing firmware on some systems */
timeout = jiffies + msecs_to_jiffies(40000);
while (fw_state == FW_TRY) {
/*
* Another device is trying the firmware. Wait until it
* decides what works (or not).
*/
if (time_after(jiffies, timeout)) {
/* waited too long */
dd_dev_err(dd, "Timeout waiting for firmware try");
fw_state = FW_ERR;
fw_err = -ETIMEDOUT;
break;
}
mutex_unlock(&fw_mutex);
msleep(20); /* arbitrary delay */
mutex_lock(&fw_mutex);
}
/* not in FW_TRY state */
if (fw_state == FW_FINAL) {
if (platform_config) {
dd->platform_config.data = platform_config->data;
dd->platform_config.size = platform_config->size;
}
goto done; /* already acquired */
} else if (fw_state == FW_ERR) {
goto done; /* already tried and failed */
}
/* fw_state is FW_EMPTY */
/* set fw_state to FW_TRY, FW_FINAL, or FW_ERR, and fw_err */
__obtain_firmware(dd);
if (platform_config_load) {
platform_config = NULL;
err = request_firmware(&platform_config, platform_config_name,
&dd->pcidev->dev);
if (err) {
platform_config = NULL;
goto done;
}
dd->platform_config.data = platform_config->data;
dd->platform_config.size = platform_config->size;
}
done:
mutex_unlock(&fw_mutex);
return fw_err;
}
/*
* Called when the driver unloads. The timing is asymmetric with its
* counterpart, obtain_firmware(). If called at device remove time,
* then it is conceivable that another device could probe while the
* firmware is being disposed. The mutexes can be moved to do that
* safely, but then the firmware would be requested from the OS multiple
* times.
*
* No mutex is needed as the driver is unloading and there cannot be any
* other callers.
*/
void dispose_firmware(void)
{
dispose_one_firmware(&fw_8051);
dispose_one_firmware(&fw_fabric);
dispose_one_firmware(&fw_pcie);
dispose_one_firmware(&fw_sbus);
release_firmware(platform_config);
platform_config = NULL;
/* retain the error state, otherwise revert to empty */
if (fw_state != FW_ERR)
fw_state = FW_EMPTY;
}
/*
* Called with the result of a firmware download.
*
* Return 1 to retry loading the firmware, 0 to stop.
*/
static int retry_firmware(struct hfi1_devdata *dd, int load_result)
{
int retry;
mutex_lock(&fw_mutex);
if (load_result == 0) {
/*
* The load succeeded, so expect all others to do the same.
* Do not retry again.
*/
if (fw_state == FW_TRY)
fw_state = FW_FINAL;
retry = 0; /* do NOT retry */
} else if (fw_state == FW_TRY) {
/* load failed, obtain alternate firmware */
__obtain_firmware(dd);
retry = (fw_state == FW_FINAL);
} else {
/* else in FW_FINAL or FW_ERR, no retry in either case */
retry = 0;
}
mutex_unlock(&fw_mutex);
return retry;
}
/*
* Write a block of data to a given array CSR. All calls will be in
* multiples of 8 bytes.
*/
static void write_rsa_data(struct hfi1_devdata *dd, int what,
const u8 *data, int nbytes)
{
int qw_size = nbytes / 8;
int i;
if (((unsigned long)data & 0x7) == 0) {
/* aligned */
u64 *ptr = (u64 *)data;
for (i = 0; i < qw_size; i++, ptr++)
write_csr(dd, what + (8 * i), *ptr);
} else {
/* not aligned */
for (i = 0; i < qw_size; i++, data += 8) {
u64 value;
memcpy(&value, data, 8);
write_csr(dd, what + (8 * i), value);
}
}
}
/*
* Write a block of data to a given CSR as a stream of writes. All calls will
* be in multiples of 8 bytes.
*/
static void write_streamed_rsa_data(struct hfi1_devdata *dd, int what,
const u8 *data, int nbytes)
{
u64 *ptr = (u64 *)data;
int qw_size = nbytes / 8;
for (; qw_size > 0; qw_size--, ptr++)
write_csr(dd, what, *ptr);
}
/*
* Download the signature and start the RSA mechanism. Wait for
* RSA_ENGINE_TIMEOUT before giving up.
*/
static int run_rsa(struct hfi1_devdata *dd, const char *who,
const u8 *signature)
{
unsigned long timeout;
u64 reg;
u32 status;
int ret = 0;
/* write the signature */
write_rsa_data(dd, MISC_CFG_RSA_SIGNATURE, signature, KEY_SIZE);
/* initialize RSA */
write_csr(dd, MISC_CFG_RSA_CMD, RSA_CMD_INIT);
/*
* Make sure the engine is idle and insert a delay between the two
* writes to MISC_CFG_RSA_CMD.
*/
status = (read_csr(dd, MISC_CFG_FW_CTRL)
& MISC_CFG_FW_CTRL_RSA_STATUS_SMASK)
>> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT;
if (status != RSA_STATUS_IDLE) {
dd_dev_err(dd, "%s security engine not idle - giving up\n",
who);
return -EBUSY;
}
/* start RSA */
write_csr(dd, MISC_CFG_RSA_CMD, RSA_CMD_START);
/*
* Look for the result.
*
* The RSA engine is hooked up to two MISC errors. The driver
* masks these errors as they do not respond to the standard
* error "clear down" mechanism. Look for these errors here and
* clear them when possible. This routine will exit with the
* errors of the current run still set.
*
* MISC_FW_AUTH_FAILED_ERR
* Firmware authorization failed. This can be cleared by
* re-initializing the RSA engine, then clearing the status bit.
* Do not re-init the RSA angine immediately after a successful
* run - this will reset the current authorization.
*
* MISC_KEY_MISMATCH_ERR
* Key does not match. The only way to clear this is to load
* a matching key then clear the status bit. If this error
* is raised, it will persist outside of this routine until a
* matching key is loaded.
*/
timeout = msecs_to_jiffies(RSA_ENGINE_TIMEOUT) + jiffies;
while (1) {
status = (read_csr(dd, MISC_CFG_FW_CTRL)
& MISC_CFG_FW_CTRL_RSA_STATUS_SMASK)
>> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT;
if (status == RSA_STATUS_IDLE) {
/* should not happen */
dd_dev_err(dd, "%s firmware security bad idle state\n",
who);
ret = -EINVAL;
break;
} else if (status == RSA_STATUS_DONE) {
/* finished successfully */
break;
} else if (status == RSA_STATUS_FAILED) {
/* finished unsuccessfully */
ret = -EINVAL;
break;
}
/* else still active */
if (time_after(jiffies, timeout)) {
/*
* Timed out while active. We can't reset the engine
* if it is stuck active, but run through the
* error code to see what error bits are set.
*/
dd_dev_err(dd, "%s firmware security time out\n", who);
ret = -ETIMEDOUT;
break;
}
msleep(20);
}
/*
* Arrive here on success or failure. Clear all RSA engine
* errors. All current errors will stick - the RSA logic is keeping
* error high. All previous errors will clear - the RSA logic
* is not keeping the error high.
*/
write_csr(dd, MISC_ERR_CLEAR,
MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK |
MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK);
/*
* All that is left are the current errors. Print warnings on
* authorization failure details, if any. Firmware authorization
* can be retried, so these are only warnings.
*/
reg = read_csr(dd, MISC_ERR_STATUS);
if (ret) {
if (reg & MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK)
dd_dev_warn(dd, "%s firmware authorization failed\n",
who);
if (reg & MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK)
dd_dev_warn(dd, "%s firmware key mismatch\n", who);
}
return ret;
}
static void load_security_variables(struct hfi1_devdata *dd,
struct firmware_details *fdet)
{
/* Security variables a. Write the modulus */
write_rsa_data(dd, MISC_CFG_RSA_MODULUS, fdet->modulus, KEY_SIZE);
/* Security variables b. Write the r2 */
write_rsa_data(dd, MISC_CFG_RSA_R2, fdet->r2, KEY_SIZE);
/* Security variables c. Write the mu */
write_rsa_data(dd, MISC_CFG_RSA_MU, fdet->mu, MU_SIZE);
/* Security variables d. Write the header */
write_streamed_rsa_data(dd, MISC_CFG_SHA_PRELOAD,
(u8 *)fdet->css_header,
sizeof(struct css_header));
}
/* return the 8051 firmware state */
static inline u32 get_firmware_state(struct hfi1_devdata *dd)
{
u64 reg = read_csr(dd, DC_DC8051_STS_CUR_STATE);
return (reg >> DC_DC8051_STS_CUR_STATE_FIRMWARE_SHIFT)
& DC_DC8051_STS_CUR_STATE_FIRMWARE_MASK;
}
/*
* Wait until the firmware is up and ready to take host requests.
* Return 0 on success, -ETIMEDOUT on timeout.
*/
int wait_fm_ready(struct hfi1_devdata *dd, u32 mstimeout)
{
unsigned long timeout;
/* in the simulator, the fake 8051 is always ready */
if (dd->icode == ICODE_FUNCTIONAL_SIMULATOR)
return 0;
timeout = msecs_to_jiffies(mstimeout) + jiffies;
while (1) {
if (get_firmware_state(dd) == 0xa0) /* ready */
return 0;
if (time_after(jiffies, timeout)) /* timed out */
return -ETIMEDOUT;
usleep_range(1950, 2050); /* sleep 2ms-ish */
}
}
/*
* Load the 8051 firmware.
*/
static int load_8051_firmware(struct hfi1_devdata *dd,
struct firmware_details *fdet)
{
u64 reg;
int ret;
u8 ver_a, ver_b;
/*