3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2015 Intel Corporation.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
21 * Copyright(c) 2015 Intel Corporation.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include <linux/firmware.h>
52 #include <linux/mutex.h>
53 #include <linux/module.h>
54 #include <linux/delay.h>
55 #include <linux/crc32.h>
61 * Make it easy to toggle firmware file name and if it gets loaded by
62 * editing the following. This may be something we do while in development
63 * but not necessarily something a user would ever need to use.
65 #define DEFAULT_FW_8051_NAME_FPGA "hfi_dc8051.bin"
66 #define DEFAULT_FW_8051_NAME_ASIC "hfi1_dc8051.fw"
67 #define DEFAULT_FW_FABRIC_NAME "hfi1_fabric.fw"
68 #define DEFAULT_FW_SBUS_NAME "hfi1_sbus.fw"
69 #define DEFAULT_FW_PCIE_NAME "hfi1_pcie.fw"
70 #define DEFAULT_PLATFORM_CONFIG_NAME "hfi1_platform.dat"
72 static uint fw_8051_load
= 1;
73 static uint fw_fabric_serdes_load
= 1;
74 static uint fw_pcie_serdes_load
= 1;
75 static uint fw_sbus_load
= 1;
76 static uint platform_config_load
= 1;
78 /* Firmware file names get set in hfi1_firmware_init() based on the above */
79 static char *fw_8051_name
;
80 static char *fw_fabric_serdes_name
;
81 static char *fw_sbus_name
;
82 static char *fw_pcie_serdes_name
;
83 static char *platform_config_name
;
85 #define SBUS_MAX_POLL_COUNT 100
86 #define SBUS_COUNTER(reg, name) \
87 (((reg) >> ASIC_STS_SBUS_COUNTERS_##name##_CNT_SHIFT) & \
88 ASIC_STS_SBUS_COUNTERS_##name##_CNT_MASK)
91 * Firmware security header.
99 u32 date
; /* BCD yyyymmdd */
100 u32 size
; /* in DWORDs */
101 u32 key_size
; /* in DWORDs */
102 u32 modulus_size
; /* in DWORDs */
103 u32 exponent_size
; /* in DWORDs */
106 /* expected field values */
107 #define CSS_MODULE_TYPE 0x00000006
108 #define CSS_HEADER_LEN 0x000000a1
109 #define CSS_HEADER_VERSION 0x00010000
110 #define CSS_MODULE_VENDOR 0x00008086
114 #define EXPONENT_SIZE 4
116 /* the file itself */
117 struct firmware_file
{
118 struct css_header css_header
;
119 u8 modulus
[KEY_SIZE
];
120 u8 exponent
[EXPONENT_SIZE
];
121 u8 signature
[KEY_SIZE
];
125 struct augmented_firmware_file
{
126 struct css_header css_header
;
127 u8 modulus
[KEY_SIZE
];
128 u8 exponent
[EXPONENT_SIZE
];
129 u8 signature
[KEY_SIZE
];
135 /* augmented file size difference */
136 #define AUGMENT_SIZE (sizeof(struct augmented_firmware_file) - \
137 sizeof(struct firmware_file))
139 struct firmware_details
{
140 /* Linux core piece */
141 const struct firmware
*fw
;
143 struct css_header
*css_header
;
144 u8
*firmware_ptr
; /* pointer to binary data */
145 u32 firmware_len
; /* length in bytes */
146 u8
*modulus
; /* pointer to the modulus */
147 u8
*exponent
; /* pointer to the exponent */
148 u8
*signature
; /* pointer to the signature */
149 u8
*r2
; /* pointer to r2 */
150 u8
*mu
; /* pointer to mu */
151 struct augmented_firmware_file dummy_header
;
155 * The mutex protects fw_state, fw_err, and all of the firmware_details
158 static DEFINE_MUTEX(fw_mutex
);
164 static enum fw_state fw_state
= FW_EMPTY
;
166 static struct firmware_details fw_8051
;
167 static struct firmware_details fw_fabric
;
168 static struct firmware_details fw_pcie
;
169 static struct firmware_details fw_sbus
;
170 static const struct firmware
*platform_config
;
172 /* flags for turn_off_spicos() */
173 #define SPICO_SBUS 0x1
174 #define SPICO_FABRIC 0x2
175 #define ENABLE_SPICO_SMASK 0x1
177 /* security block commands */
178 #define RSA_CMD_INIT 0x1
179 #define RSA_CMD_START 0x2
181 /* security block status */
182 #define RSA_STATUS_IDLE 0x0
183 #define RSA_STATUS_ACTIVE 0x1
184 #define RSA_STATUS_DONE 0x2
185 #define RSA_STATUS_FAILED 0x3
187 /* RSA engine timeout, in ms */
188 #define RSA_ENGINE_TIMEOUT 100 /* ms */
190 /* hardware mutex timeout, in ms */
191 #define HM_TIMEOUT 4000 /* 4 s */
193 /* 8051 memory access timeout, in us */
194 #define DC8051_ACCESS_TIMEOUT 100 /* us */
196 /* the number of fabric SerDes on the SBus */
197 #define NUM_FABRIC_SERDES 4
199 /* SBus fabric SerDes addresses, one set per HFI */
200 static const u8 fabric_serdes_addrs
[2][NUM_FABRIC_SERDES
] = {
201 { 0x01, 0x02, 0x03, 0x04 },
202 { 0x28, 0x29, 0x2a, 0x2b }
205 /* SBus PCIe SerDes addresses, one set per HFI */
206 static const u8 pcie_serdes_addrs
[2][NUM_PCIE_SERDES
] = {
207 { 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16,
208 0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26 },
209 { 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d,
210 0x3f, 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d }
213 /* SBus PCIe PCS addresses, one set per HFI */
214 const u8 pcie_pcs_addrs
[2][NUM_PCIE_SERDES
] = {
215 { 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17,
216 0x19, 0x1b, 0x1d, 0x1f, 0x21, 0x23, 0x25, 0x27 },
217 { 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
218 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e }
221 /* SBus fabric SerDes broadcast addresses, one per HFI */
222 static const u8 fabric_serdes_broadcast
[2] = { 0xe4, 0xe5 };
223 static const u8 all_fabric_serdes_broadcast
= 0xe1;
225 /* SBus PCIe SerDes broadcast addresses, one per HFI */
226 const u8 pcie_serdes_broadcast
[2] = { 0xe2, 0xe3 };
227 static const u8 all_pcie_serdes_broadcast
= 0xe0;
230 static void dispose_one_firmware(struct firmware_details
*fdet
);
233 * Read a single 64-bit value from 8051 data memory.
236 * o caller to have already set up data read, no auto increment
237 * o caller to turn off read enable when finished
239 * The address argument is a byte offset. Bits 0:2 in the address are
240 * ignored - i.e. the hardware will always do aligned 8-byte reads as if
241 * the lower bits are zero.
243 * Return 0 on success, -ENXIO on a read error (timeout).
245 static int __read_8051_data(struct hfi1_devdata
*dd
, u32 addr
, u64
*result
)
250 /* start the read at the given address */
251 reg
= ((addr
& DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK
)
252 << DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT
)
253 | DC_DC8051_CFG_RAM_ACCESS_CTRL_READ_ENA_SMASK
;
254 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_CTRL
, reg
);
256 /* wait until ACCESS_COMPLETED is set */
258 while ((read_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_STATUS
)
259 & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK
)
262 if (count
> DC8051_ACCESS_TIMEOUT
) {
263 dd_dev_err(dd
, "timeout reading 8051 data\n");
269 /* gather the data */
270 *result
= read_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_RD_DATA
);
276 * Read 8051 data starting at addr, for len bytes. Will read in 8-byte chunks.
277 * Return 0 on success, -errno on error.
279 int read_8051_data(struct hfi1_devdata
*dd
, u32 addr
, u32 len
, u64
*result
)
285 spin_lock_irqsave(&dd
->dc8051_memlock
, flags
);
287 /* data read set-up, no auto-increment */
288 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_SETUP
, 0);
290 for (done
= 0; done
< len
; addr
+= 8, done
+= 8, result
++) {
291 ret
= __read_8051_data(dd
, addr
, result
);
296 /* turn off read enable */
297 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_CTRL
, 0);
299 spin_unlock_irqrestore(&dd
->dc8051_memlock
, flags
);
305 * Write data or code to the 8051 code or data RAM.
307 static int write_8051(struct hfi1_devdata
*dd
, int code
, u32 start
,
308 const u8
*data
, u32 len
)
314 /* check alignment */
315 aligned
= ((unsigned long)data
& 0x7) == 0;
318 reg
= (code
? DC_DC8051_CFG_RAM_ACCESS_SETUP_RAM_SEL_SMASK
: 0ull)
319 | DC_DC8051_CFG_RAM_ACCESS_SETUP_AUTO_INCR_ADDR_SMASK
;
320 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_SETUP
, reg
);
322 reg
= ((start
& DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK
)
323 << DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT
)
324 | DC_DC8051_CFG_RAM_ACCESS_CTRL_WRITE_ENA_SMASK
;
325 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_CTRL
, reg
);
328 for (offset
= 0; offset
< len
; offset
+= 8) {
329 int bytes
= len
- offset
;
333 memcpy(®
, &data
[offset
], bytes
);
334 } else if (aligned
) {
335 reg
= *(u64
*)&data
[offset
];
337 memcpy(®
, &data
[offset
], 8);
339 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_WR_DATA
, reg
);
341 /* wait until ACCESS_COMPLETED is set */
343 while ((read_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_STATUS
)
344 & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK
)
347 if (count
> DC8051_ACCESS_TIMEOUT
) {
348 dd_dev_err(dd
, "timeout writing 8051 data\n");
355 /* turn off write access, auto increment (also sets to data access) */
356 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_CTRL
, 0);
357 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_SETUP
, 0);
362 /* return 0 if values match, non-zero and complain otherwise */
363 static int invalid_header(struct hfi1_devdata
*dd
, const char *what
,
364 u32 actual
, u32 expected
)
366 if (actual
== expected
)
370 "invalid firmware header field %s: expected 0x%x, actual 0x%x\n",
371 what
, expected
, actual
);
376 * Verify that the static fields in the CSS header match.
378 static int verify_css_header(struct hfi1_devdata
*dd
, struct css_header
*css
)
380 /* verify CSS header fields (most sizes are in DW, so add /4) */
381 if (invalid_header(dd
, "module_type", css
->module_type
, CSS_MODULE_TYPE
)
382 || invalid_header(dd
, "header_len", css
->header_len
,
383 (sizeof(struct firmware_file
)/4))
384 || invalid_header(dd
, "header_version",
385 css
->header_version
, CSS_HEADER_VERSION
)
386 || invalid_header(dd
, "module_vendor",
387 css
->module_vendor
, CSS_MODULE_VENDOR
)
388 || invalid_header(dd
, "key_size",
389 css
->key_size
, KEY_SIZE
/4)
390 || invalid_header(dd
, "modulus_size",
391 css
->modulus_size
, KEY_SIZE
/4)
392 || invalid_header(dd
, "exponent_size",
393 css
->exponent_size
, EXPONENT_SIZE
/4)) {
400 * Make sure there are at least some bytes after the prefix.
402 static int payload_check(struct hfi1_devdata
*dd
, const char *name
,
403 long file_size
, long prefix_size
)
405 /* make sure we have some payload */
406 if (prefix_size
>= file_size
) {
408 "firmware \"%s\", size %ld, must be larger than %ld bytes\n",
409 name
, file_size
, prefix_size
);
417 * Request the firmware from the system. Extract the pieces and fill in
418 * fdet. If successful, the caller will need to call dispose_one_firmware().
419 * Returns 0 on success, -ERRNO on error.
421 static int obtain_one_firmware(struct hfi1_devdata
*dd
, const char *name
,
422 struct firmware_details
*fdet
)
424 struct css_header
*css
;
427 memset(fdet
, 0, sizeof(*fdet
));
429 ret
= request_firmware(&fdet
->fw
, name
, &dd
->pcidev
->dev
);
431 dd_dev_err(dd
, "cannot load firmware \"%s\", err %d\n",
436 /* verify the firmware */
437 if (fdet
->fw
->size
< sizeof(struct css_header
)) {
438 dd_dev_err(dd
, "firmware \"%s\" is too small\n", name
);
442 css
= (struct css_header
*)fdet
->fw
->data
;
444 hfi1_cdbg(FIRMWARE
, "Firmware %s details:", name
);
445 hfi1_cdbg(FIRMWARE
, "file size: 0x%lx bytes", fdet
->fw
->size
);
446 hfi1_cdbg(FIRMWARE
, "CSS structure:");
447 hfi1_cdbg(FIRMWARE
, " module_type 0x%x", css
->module_type
);
448 hfi1_cdbg(FIRMWARE
, " header_len 0x%03x (0x%03x bytes)",
449 css
->header_len
, 4 * css
->header_len
);
450 hfi1_cdbg(FIRMWARE
, " header_version 0x%x", css
->header_version
);
451 hfi1_cdbg(FIRMWARE
, " module_id 0x%x", css
->module_id
);
452 hfi1_cdbg(FIRMWARE
, " module_vendor 0x%x", css
->module_vendor
);
453 hfi1_cdbg(FIRMWARE
, " date 0x%x", css
->date
);
454 hfi1_cdbg(FIRMWARE
, " size 0x%03x (0x%03x bytes)",
455 css
->size
, 4 * css
->size
);
456 hfi1_cdbg(FIRMWARE
, " key_size 0x%03x (0x%03x bytes)",
457 css
->key_size
, 4 * css
->key_size
);
458 hfi1_cdbg(FIRMWARE
, " modulus_size 0x%03x (0x%03x bytes)",
459 css
->modulus_size
, 4 * css
->modulus_size
);
460 hfi1_cdbg(FIRMWARE
, " exponent_size 0x%03x (0x%03x bytes)",
461 css
->exponent_size
, 4 * css
->exponent_size
);
462 hfi1_cdbg(FIRMWARE
, "firmware size: 0x%lx bytes",
463 fdet
->fw
->size
- sizeof(struct firmware_file
));
466 * If the file does not have a valid CSS header, fail.
467 * Otherwise, check the CSS size field for an expected size.
468 * The augmented file has r2 and mu inserted after the header
469 * was generated, so there will be a known difference between
470 * the CSS header size and the actual file size. Use this
471 * difference to identify an augmented file.
473 * Note: css->size is in DWORDs, multiply by 4 to get bytes.
475 ret
= verify_css_header(dd
, css
);
477 dd_dev_info(dd
, "Invalid CSS header for \"%s\"\n", name
);
478 } else if ((css
->size
*4) == fdet
->fw
->size
) {
479 /* non-augmented firmware file */
480 struct firmware_file
*ff
= (struct firmware_file
*)
483 /* make sure there are bytes in the payload */
484 ret
= payload_check(dd
, name
, fdet
->fw
->size
,
485 sizeof(struct firmware_file
));
487 fdet
->css_header
= css
;
488 fdet
->modulus
= ff
->modulus
;
489 fdet
->exponent
= ff
->exponent
;
490 fdet
->signature
= ff
->signature
;
491 fdet
->r2
= fdet
->dummy_header
.r2
; /* use dummy space */
492 fdet
->mu
= fdet
->dummy_header
.mu
; /* use dummy space */
493 fdet
->firmware_ptr
= ff
->firmware
;
494 fdet
->firmware_len
= fdet
->fw
->size
-
495 sizeof(struct firmware_file
);
497 * Header does not include r2 and mu - generate here.
500 dd_dev_err(dd
, "driver is unable to validate firmware without r2 and mu (not in firmware file)\n");
503 } else if ((css
->size
*4) + AUGMENT_SIZE
== fdet
->fw
->size
) {
504 /* augmented firmware file */
505 struct augmented_firmware_file
*aff
=
506 (struct augmented_firmware_file
*)fdet
->fw
->data
;
508 /* make sure there are bytes in the payload */
509 ret
= payload_check(dd
, name
, fdet
->fw
->size
,
510 sizeof(struct augmented_firmware_file
));
512 fdet
->css_header
= css
;
513 fdet
->modulus
= aff
->modulus
;
514 fdet
->exponent
= aff
->exponent
;
515 fdet
->signature
= aff
->signature
;
518 fdet
->firmware_ptr
= aff
->firmware
;
519 fdet
->firmware_len
= fdet
->fw
->size
-
520 sizeof(struct augmented_firmware_file
);
523 /* css->size check failed */
525 "invalid firmware header field size: expected 0x%lx or 0x%lx, actual 0x%x\n",
526 fdet
->fw
->size
/4, (fdet
->fw
->size
- AUGMENT_SIZE
)/4,
533 /* if returning an error, clean up after ourselves */
535 dispose_one_firmware(fdet
);
539 static void dispose_one_firmware(struct firmware_details
*fdet
)
541 release_firmware(fdet
->fw
);
546 * Called by all HFIs when loading their firmware - i.e. device probe time.
547 * The first one will do the actual firmware load. Use a mutex to resolve
548 * any possible race condition.
550 * The call to this routine cannot be moved to driver load because the kernel
551 * call request_firmware() requires a device which is only available after
552 * the first device probe.
554 static int obtain_firmware(struct hfi1_devdata
*dd
)
558 mutex_lock(&fw_mutex
);
559 if (fw_state
== FW_ACQUIRED
) {
560 goto done
; /* already acquired */
561 } else if (fw_state
== FW_ERR
) {
563 goto done
; /* already tried and failed */
567 err
= obtain_one_firmware(dd
, fw_8051_name
, &fw_8051
);
572 if (fw_fabric_serdes_load
) {
573 err
= obtain_one_firmware(dd
, fw_fabric_serdes_name
,
580 err
= obtain_one_firmware(dd
, fw_sbus_name
, &fw_sbus
);
585 if (fw_pcie_serdes_load
) {
586 err
= obtain_one_firmware(dd
, fw_pcie_serdes_name
, &fw_pcie
);
591 if (platform_config_load
) {
592 platform_config
= NULL
;
593 err
= request_firmware(&platform_config
, platform_config_name
,
597 platform_config
= NULL
;
602 fw_state
= FW_ACQUIRED
;
609 mutex_unlock(&fw_mutex
);
615 * Called when the driver unloads. The timing is asymmetric with its
616 * counterpart, obtain_firmware(). If called at device remove time,
617 * then it is conceivable that another device could probe while the
618 * firmware is being disposed. The mutexes can be moved to do that
619 * safely, but then the firmware would be requested from the OS multiple
622 * No mutex is needed as the driver is unloading and there cannot be any
625 void dispose_firmware(void)
627 dispose_one_firmware(&fw_8051
);
628 dispose_one_firmware(&fw_fabric
);
629 dispose_one_firmware(&fw_pcie
);
630 dispose_one_firmware(&fw_sbus
);
632 release_firmware(platform_config
);
633 platform_config
= NULL
;
635 /* retain the error state, otherwise revert to empty */
636 if (fw_state
!= FW_ERR
)
641 * Write a block of data to a given array CSR. All calls will be in
642 * multiples of 8 bytes.
644 static void write_rsa_data(struct hfi1_devdata
*dd
, int what
,
645 const u8
*data
, int nbytes
)
647 int qw_size
= nbytes
/8;
650 if (((unsigned long)data
& 0x7) == 0) {
652 u64
*ptr
= (u64
*)data
;
654 for (i
= 0; i
< qw_size
; i
++, ptr
++)
655 write_csr(dd
, what
+ (8*i
), *ptr
);
658 for (i
= 0; i
< qw_size
; i
++, data
+= 8) {
661 memcpy(&value
, data
, 8);
662 write_csr(dd
, what
+ (8*i
), value
);
668 * Write a block of data to a given CSR as a stream of writes. All calls will
669 * be in multiples of 8 bytes.
671 static void write_streamed_rsa_data(struct hfi1_devdata
*dd
, int what
,
672 const u8
*data
, int nbytes
)
674 u64
*ptr
= (u64
*)data
;
675 int qw_size
= nbytes
/8;
677 for (; qw_size
> 0; qw_size
--, ptr
++)
678 write_csr(dd
, what
, *ptr
);
682 * Download the signature and start the RSA mechanism. Wait for
683 * RSA_ENGINE_TIMEOUT before giving up.
685 static int run_rsa(struct hfi1_devdata
*dd
, const char *who
,
688 unsigned long timeout
;
693 /* write the signature */
694 write_rsa_data(dd
, MISC_CFG_RSA_SIGNATURE
, signature
, KEY_SIZE
);
697 write_csr(dd
, MISC_CFG_RSA_CMD
, RSA_CMD_INIT
);
700 * Make sure the engine is idle and insert a delay between the two
701 * writes to MISC_CFG_RSA_CMD.
703 status
= (read_csr(dd
, MISC_CFG_FW_CTRL
)
704 & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK
)
705 >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT
;
706 if (status
!= RSA_STATUS_IDLE
) {
707 dd_dev_err(dd
, "%s security engine not idle - giving up\n",
713 write_csr(dd
, MISC_CFG_RSA_CMD
, RSA_CMD_START
);
716 * Look for the result.
718 * The RSA engine is hooked up to two MISC errors. The driver
719 * masks these errors as they do not respond to the standard
720 * error "clear down" mechanism. Look for these errors here and
721 * clear them when possible. This routine will exit with the
722 * errors of the current run still set.
724 * MISC_FW_AUTH_FAILED_ERR
725 * Firmware authorization failed. This can be cleared by
726 * re-initializing the RSA engine, then clearing the status bit.
727 * Do not re-init the RSA angine immediately after a successful
728 * run - this will reset the current authorization.
730 * MISC_KEY_MISMATCH_ERR
731 * Key does not match. The only way to clear this is to load
732 * a matching key then clear the status bit. If this error
733 * is raised, it will persist outside of this routine until a
734 * matching key is loaded.
736 timeout
= msecs_to_jiffies(RSA_ENGINE_TIMEOUT
) + jiffies
;
738 status
= (read_csr(dd
, MISC_CFG_FW_CTRL
)
739 & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK
)
740 >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT
;
742 if (status
== RSA_STATUS_IDLE
) {
743 /* should not happen */
744 dd_dev_err(dd
, "%s firmware security bad idle state\n",
748 } else if (status
== RSA_STATUS_DONE
) {
749 /* finished successfully */
751 } else if (status
== RSA_STATUS_FAILED
) {
752 /* finished unsuccessfully */
756 /* else still active */
758 if (time_after(jiffies
, timeout
)) {
760 * Timed out while active. We can't reset the engine
761 * if it is stuck active, but run through the
762 * error code to see what error bits are set.
764 dd_dev_err(dd
, "%s firmware security time out\n", who
);
773 * Arrive here on success or failure. Clear all RSA engine
774 * errors. All current errors will stick - the RSA logic is keeping
775 * error high. All previous errors will clear - the RSA logic
776 * is not keeping the error high.
778 write_csr(dd
, MISC_ERR_CLEAR
,
779 MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK
780 | MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK
);
782 * All that is left are the current errors. Print failure details,
785 reg
= read_csr(dd
, MISC_ERR_STATUS
);
787 if (reg
& MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK
)
788 dd_dev_err(dd
, "%s firmware authorization failed\n",
790 if (reg
& MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK
)
791 dd_dev_err(dd
, "%s firmware key mismatch\n", who
);
797 static void load_security_variables(struct hfi1_devdata
*dd
,
798 struct firmware_details
*fdet
)
800 /* Security variables a. Write the modulus */
801 write_rsa_data(dd
, MISC_CFG_RSA_MODULUS
, fdet
->modulus
, KEY_SIZE
);
802 /* Security variables b. Write the r2 */
803 write_rsa_data(dd
, MISC_CFG_RSA_R2
, fdet
->r2
, KEY_SIZE
);
804 /* Security variables c. Write the mu */
805 write_rsa_data(dd
, MISC_CFG_RSA_MU
, fdet
->mu
, MU_SIZE
);
806 /* Security variables d. Write the header */
807 write_streamed_rsa_data(dd
, MISC_CFG_SHA_PRELOAD
,
808 (u8
*)fdet
->css_header
, sizeof(struct css_header
));
811 /* return the 8051 firmware state */
812 static inline u32
get_firmware_state(struct hfi1_devdata
*dd
)
814 u64 reg
= read_csr(dd
, DC_DC8051_STS_CUR_STATE
);
816 return (reg
>> DC_DC8051_STS_CUR_STATE_FIRMWARE_SHIFT
)
817 & DC_DC8051_STS_CUR_STATE_FIRMWARE_MASK
;
821 * Wait until the firmware is up and ready to take host requests.
822 * Return 0 on success, -ETIMEDOUT on timeout.
824 int wait_fm_ready(struct hfi1_devdata
*dd
, u32 mstimeout
)
826 unsigned long timeout
;
828 /* in the simulator, the fake 8051 is always ready */
829 if (dd
->icode
== ICODE_FUNCTIONAL_SIMULATOR
)
832 timeout
= msecs_to_jiffies(mstimeout
) + jiffies
;
834 if (get_firmware_state(dd
) == 0xa0) /* ready */
836 if (time_after(jiffies
, timeout
)) /* timed out */
838 usleep_range(1950, 2050); /* sleep 2ms-ish */
843 * Load the 8051 firmware.
845 static int load_8051_firmware(struct hfi1_devdata
*dd
,
846 struct firmware_details
*fdet
)
854 * Load DC 8051 firmware
857 * DC reset step 1: Reset DC8051
859 reg
= DC_DC8051_CFG_RST_M8051W_SMASK
860 | DC_DC8051_CFG_RST_CRAM_SMASK
861 | DC_DC8051_CFG_RST_DRAM_SMASK
862 | DC_DC8051_CFG_RST_IRAM_SMASK
863 | DC_DC8051_CFG_RST_SFR_SMASK
;
864 write_csr(dd
, DC_DC8051_CFG_RST
, reg
);
867 * DC reset step 2 (optional): Load 8051 data memory with link
872 * DC reset step 3: Load DC8051 firmware
874 /* release all but the core reset */
875 reg
= DC_DC8051_CFG_RST_M8051W_SMASK
;
876 write_csr(dd
, DC_DC8051_CFG_RST
, reg
);
878 /* Firmware load step 1 */
879 load_security_variables(dd
, fdet
);
882 * Firmware load step 2. Clear MISC_CFG_FW_CTRL.FW_8051_LOADED
884 write_csr(dd
, MISC_CFG_FW_CTRL
, 0);
886 /* Firmware load steps 3-5 */
887 ret
= write_8051(dd
, 1/*code*/, 0, fdet
->firmware_ptr
,
893 * DC reset step 4. Host starts the DC8051 firmware
896 * Firmware load step 6. Set MISC_CFG_FW_CTRL.FW_8051_LOADED
898 write_csr(dd
, MISC_CFG_FW_CTRL
, MISC_CFG_FW_CTRL_FW_8051_LOADED_SMASK
);
900 /* Firmware load steps 7-10 */
901 ret
= run_rsa(dd
, "8051", fdet
->signature
);
905 /* clear all reset bits, releasing the 8051 */
906 write_csr(dd
, DC_DC8051_CFG_RST
, 0ull);
909 * DC reset step 5. Wait for firmware to be ready to accept host
912 ret
= wait_fm_ready(dd
, TIMEOUT_8051_START
);
913 if (ret
) { /* timed out */
914 dd_dev_err(dd
, "8051 start timeout, current state 0x%x\n",
915 get_firmware_state(dd
));
919 read_misc_status(dd
, &ver_a
, &ver_b
);
920 dd_dev_info(dd
, "8051 firmware version %d.%d\n",
921 (int)ver_b
, (int)ver_a
);
922 dd
->dc8051_ver
= dc8051_ver(ver_b
, ver_a
);
927 /* SBus Master broadcast address */
928 #define SBUS_MASTER_BROADCAST 0xfd
931 * Write the SBus request register
933 * No need for masking - the arguments are sized exactly.
935 void sbus_request(struct hfi1_devdata
*dd
,
936 u8 receiver_addr
, u8 data_addr
, u8 command
, u32 data_in
)
938 write_csr(dd
, ASIC_CFG_SBUS_REQUEST
,
939 ((u64
)data_in
<< ASIC_CFG_SBUS_REQUEST_DATA_IN_SHIFT
)
940 | ((u64
)command
<< ASIC_CFG_SBUS_REQUEST_COMMAND_SHIFT
)
941 | ((u64
)data_addr
<< ASIC_CFG_SBUS_REQUEST_DATA_ADDR_SHIFT
)
942 | ((u64
)receiver_addr
943 << ASIC_CFG_SBUS_REQUEST_RECEIVER_ADDR_SHIFT
));
947 * Turn off the SBus and fabric serdes spicos.
949 * + Must be called with Sbus fast mode turned on.
950 * + Must be called after fabric serdes broadcast is set up.
951 * + Must be called before the 8051 is loaded - assumes 8051 is not loaded
952 * when using MISC_CFG_FW_CTRL.
954 static void turn_off_spicos(struct hfi1_devdata
*dd
, int flags
)
956 /* only needed on A0 */
960 dd_dev_info(dd
, "Turning off spicos:%s%s\n",
961 flags
& SPICO_SBUS
? " SBus" : "",
962 flags
& SPICO_FABRIC
? " fabric" : "");
964 write_csr(dd
, MISC_CFG_FW_CTRL
, ENABLE_SPICO_SMASK
);
965 /* disable SBus spico */
966 if (flags
& SPICO_SBUS
)
967 sbus_request(dd
, SBUS_MASTER_BROADCAST
, 0x01,
968 WRITE_SBUS_RECEIVER
, 0x00000040);
970 /* disable the fabric serdes spicos */
971 if (flags
& SPICO_FABRIC
)
972 sbus_request(dd
, fabric_serdes_broadcast
[dd
->hfi1_id
],
973 0x07, WRITE_SBUS_RECEIVER
, 0x00000000);
974 write_csr(dd
, MISC_CFG_FW_CTRL
, 0);
978 * Reset all of the fabric serdes for our HFI.
980 void fabric_serdes_reset(struct hfi1_devdata
*dd
)
984 if (dd
->icode
!= ICODE_RTL_SILICON
) /* only for RTL */
987 ra
= fabric_serdes_broadcast
[dd
->hfi1_id
];
989 acquire_hw_mutex(dd
);
990 set_sbus_fast_mode(dd
);
991 /* place SerDes in reset and disable SPICO */
992 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000011);
993 /* wait 100 refclk cycles @ 156.25MHz => 640ns */
995 /* remove SerDes reset */
996 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000010);
997 /* turn SPICO enable on */
998 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000002);
999 clear_sbus_fast_mode(dd
);
1000 release_hw_mutex(dd
);
1003 /* Access to the SBus in this routine should probably be serialized */
1004 int sbus_request_slow(struct hfi1_devdata
*dd
,
1005 u8 receiver_addr
, u8 data_addr
, u8 command
, u32 data_in
)
1009 sbus_request(dd
, receiver_addr
, data_addr
, command
, data_in
);
1010 write_csr(dd
, ASIC_CFG_SBUS_EXECUTE
,
1011 ASIC_CFG_SBUS_EXECUTE_EXECUTE_SMASK
);
1012 /* Wait for both DONE and RCV_DATA_VALID to go high */
1013 reg
= read_csr(dd
, ASIC_STS_SBUS_RESULT
);
1014 while (!((reg
& ASIC_STS_SBUS_RESULT_DONE_SMASK
) &&
1015 (reg
& ASIC_STS_SBUS_RESULT_RCV_DATA_VALID_SMASK
))) {
1016 if (count
++ >= SBUS_MAX_POLL_COUNT
) {
1017 u64 counts
= read_csr(dd
, ASIC_STS_SBUS_COUNTERS
);
1019 * If the loop has timed out, we are OK if DONE bit
1020 * is set and RCV_DATA_VALID and EXECUTE counters
1021 * are the same. If not, we cannot proceed.
1023 if ((reg
& ASIC_STS_SBUS_RESULT_DONE_SMASK
) &&
1024 (SBUS_COUNTER(counts
, RCV_DATA_VALID
) ==
1025 SBUS_COUNTER(counts
, EXECUTE
)))
1030 reg
= read_csr(dd
, ASIC_STS_SBUS_RESULT
);
1033 write_csr(dd
, ASIC_CFG_SBUS_EXECUTE
, 0);
1034 /* Wait for DONE to clear after EXECUTE is cleared */
1035 reg
= read_csr(dd
, ASIC_STS_SBUS_RESULT
);
1036 while (reg
& ASIC_STS_SBUS_RESULT_DONE_SMASK
) {
1037 if (count
++ >= SBUS_MAX_POLL_COUNT
)
1040 reg
= read_csr(dd
, ASIC_STS_SBUS_RESULT
);
1045 static int load_fabric_serdes_firmware(struct hfi1_devdata
*dd
,
1046 struct firmware_details
*fdet
)
1049 const u8 ra
= fabric_serdes_broadcast
[dd
->hfi1_id
]; /* receiver addr */
1051 dd_dev_info(dd
, "Downloading fabric firmware\n");
1053 /* step 1: load security variables */
1054 load_security_variables(dd
, fdet
);
1055 /* step 2: place SerDes in reset and disable SPICO */
1056 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000011);
1057 /* wait 100 refclk cycles @ 156.25MHz => 640ns */
1059 /* step 3: remove SerDes reset */
1060 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000010);
1061 /* step 4: assert IMEM override */
1062 sbus_request(dd
, ra
, 0x00, WRITE_SBUS_RECEIVER
, 0x40000000);
1063 /* step 5: download SerDes machine code */
1064 for (i
= 0; i
< fdet
->firmware_len
; i
+= 4) {
1065 sbus_request(dd
, ra
, 0x0a, WRITE_SBUS_RECEIVER
,
1066 *(u32
*)&fdet
->firmware_ptr
[i
]);
1068 /* step 6: IMEM override off */
1069 sbus_request(dd
, ra
, 0x00, WRITE_SBUS_RECEIVER
, 0x00000000);
1070 /* step 7: turn ECC on */
1071 sbus_request(dd
, ra
, 0x0b, WRITE_SBUS_RECEIVER
, 0x000c0000);
1073 /* steps 8-11: run the RSA engine */
1074 err
= run_rsa(dd
, "fabric serdes", fdet
->signature
);
1078 /* step 12: turn SPICO enable on */
1079 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000002);
1080 /* step 13: enable core hardware interrupts */
1081 sbus_request(dd
, ra
, 0x08, WRITE_SBUS_RECEIVER
, 0x00000000);
1086 static int load_sbus_firmware(struct hfi1_devdata
*dd
,
1087 struct firmware_details
*fdet
)
1090 const u8 ra
= SBUS_MASTER_BROADCAST
; /* receiver address */
1092 dd_dev_info(dd
, "Downloading SBus firmware\n");
1094 /* step 1: load security variables */
1095 load_security_variables(dd
, fdet
);
1096 /* step 2: place SPICO into reset and enable off */
1097 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x000000c0);
1098 /* step 3: remove reset, enable off, IMEM_CNTRL_EN on */
1099 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x00000240);
1100 /* step 4: set starting IMEM address for burst download */
1101 sbus_request(dd
, ra
, 0x03, WRITE_SBUS_RECEIVER
, 0x80000000);
1102 /* step 5: download the SBus Master machine code */
1103 for (i
= 0; i
< fdet
->firmware_len
; i
+= 4) {
1104 sbus_request(dd
, ra
, 0x14, WRITE_SBUS_RECEIVER
,
1105 *(u32
*)&fdet
->firmware_ptr
[i
]);
1107 /* step 6: set IMEM_CNTL_EN off */
1108 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x00000040);
1109 /* step 7: turn ECC on */
1110 sbus_request(dd
, ra
, 0x16, WRITE_SBUS_RECEIVER
, 0x000c0000);
1112 /* steps 8-11: run the RSA engine */
1113 err
= run_rsa(dd
, "SBus", fdet
->signature
);
1117 /* step 12: set SPICO_ENABLE on */
1118 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x00000140);
1123 static int load_pcie_serdes_firmware(struct hfi1_devdata
*dd
,
1124 struct firmware_details
*fdet
)
1127 const u8 ra
= SBUS_MASTER_BROADCAST
; /* receiver address */
1129 dd_dev_info(dd
, "Downloading PCIe firmware\n");
1131 /* step 1: load security variables */
1132 load_security_variables(dd
, fdet
);
1133 /* step 2: assert single step (halts the SBus Master spico) */
1134 sbus_request(dd
, ra
, 0x05, WRITE_SBUS_RECEIVER
, 0x00000001);
1135 /* step 3: enable XDMEM access */
1136 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x00000d40);
1137 /* step 4: load firmware into SBus Master XDMEM */
1138 /* NOTE: the dmem address, write_en, and wdata are all pre-packed,
1139 we only need to pick up the bytes and write them */
1140 for (i
= 0; i
< fdet
->firmware_len
; i
+= 4) {
1141 sbus_request(dd
, ra
, 0x04, WRITE_SBUS_RECEIVER
,
1142 *(u32
*)&fdet
->firmware_ptr
[i
]);
1144 /* step 5: disable XDMEM access */
1145 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x00000140);
1146 /* step 6: allow SBus Spico to run */
1147 sbus_request(dd
, ra
, 0x05, WRITE_SBUS_RECEIVER
, 0x00000000);
1149 /* steps 7-11: run RSA, if it succeeds, firmware is available to
1151 return run_rsa(dd
, "PCIe serdes", fdet
->signature
);
1155 * Set the given broadcast values on the given list of devices.
1157 static void set_serdes_broadcast(struct hfi1_devdata
*dd
, u8 bg1
, u8 bg2
,
1158 const u8
*addrs
, int count
)
1160 while (--count
>= 0) {
1162 * Set BROADCAST_GROUP_1 and BROADCAST_GROUP_2, leave
1163 * defaults for everything else. Do not read-modify-write,
1164 * per instruction from the manufacturer.
1168 * ----- ---------------------------------
1169 * 0 IGNORE_BROADCAST (default 0)
1170 * 11:4 BROADCAST_GROUP_1 (default 0xff)
1171 * 23:16 BROADCAST_GROUP_2 (default 0xff)
1173 sbus_request(dd
, addrs
[count
], 0xfd, WRITE_SBUS_RECEIVER
,
1174 (u32
)bg1
<< 4 | (u32
)bg2
<< 16);
1178 int acquire_hw_mutex(struct hfi1_devdata
*dd
)
1180 unsigned long timeout
;
1182 u8 mask
= 1 << dd
->hfi1_id
;
1186 timeout
= msecs_to_jiffies(HM_TIMEOUT
) + jiffies
;
1188 write_csr(dd
, ASIC_CFG_MUTEX
, mask
);
1189 user
= (u8
)read_csr(dd
, ASIC_CFG_MUTEX
);
1191 return 0; /* success */
1192 if (time_after(jiffies
, timeout
))
1193 break; /* timed out */
1199 "Unable to acquire hardware mutex, mutex mask %u, my mask %u (%s)\n",
1200 (u32
)user
, (u32
)mask
, (try == 0) ? "retrying" : "giving up");
1203 /* break mutex and retry */
1204 write_csr(dd
, ASIC_CFG_MUTEX
, 0);
1212 void release_hw_mutex(struct hfi1_devdata
*dd
)
1214 write_csr(dd
, ASIC_CFG_MUTEX
, 0);
1217 void set_sbus_fast_mode(struct hfi1_devdata
*dd
)
1219 write_csr(dd
, ASIC_CFG_SBUS_EXECUTE
,
1220 ASIC_CFG_SBUS_EXECUTE_FAST_MODE_SMASK
);
1223 void clear_sbus_fast_mode(struct hfi1_devdata
*dd
)
1227 reg
= read_csr(dd
, ASIC_STS_SBUS_COUNTERS
);
1228 while (SBUS_COUNTER(reg
, EXECUTE
) !=
1229 SBUS_COUNTER(reg
, RCV_DATA_VALID
)) {
1230 if (count
++ >= SBUS_MAX_POLL_COUNT
)
1233 reg
= read_csr(dd
, ASIC_STS_SBUS_COUNTERS
);
1235 write_csr(dd
, ASIC_CFG_SBUS_EXECUTE
, 0);
1238 int load_firmware(struct hfi1_devdata
*dd
)
1242 if (fw_sbus_load
|| fw_fabric_serdes_load
) {
1243 ret
= acquire_hw_mutex(dd
);
1247 set_sbus_fast_mode(dd
);
1250 * The SBus contains part of the fabric firmware and so must
1251 * also be downloaded.
1254 turn_off_spicos(dd
, SPICO_SBUS
);
1255 ret
= load_sbus_firmware(dd
, &fw_sbus
);
1260 if (fw_fabric_serdes_load
) {
1261 set_serdes_broadcast(dd
, all_fabric_serdes_broadcast
,
1262 fabric_serdes_broadcast
[dd
->hfi1_id
],
1263 fabric_serdes_addrs
[dd
->hfi1_id
],
1265 turn_off_spicos(dd
, SPICO_FABRIC
);
1266 ret
= load_fabric_serdes_firmware(dd
, &fw_fabric
);
1270 clear_sbus_fast_mode(dd
);
1271 release_hw_mutex(dd
);
1277 ret
= load_8051_firmware(dd
, &fw_8051
);
1285 int hfi1_firmware_init(struct hfi1_devdata
*dd
)
1287 /* only RTL can use these */
1288 if (dd
->icode
!= ICODE_RTL_SILICON
) {
1289 fw_fabric_serdes_load
= 0;
1290 fw_pcie_serdes_load
= 0;
1294 /* no 8051 or QSFP on simulator */
1295 if (dd
->icode
== ICODE_FUNCTIONAL_SIMULATOR
) {
1297 platform_config_load
= 0;
1300 if (!fw_8051_name
) {
1301 if (dd
->icode
== ICODE_RTL_SILICON
)
1302 fw_8051_name
= DEFAULT_FW_8051_NAME_ASIC
;
1304 fw_8051_name
= DEFAULT_FW_8051_NAME_FPGA
;
1306 if (!fw_fabric_serdes_name
)
1307 fw_fabric_serdes_name
= DEFAULT_FW_FABRIC_NAME
;
1309 fw_sbus_name
= DEFAULT_FW_SBUS_NAME
;
1310 if (!fw_pcie_serdes_name
)
1311 fw_pcie_serdes_name
= DEFAULT_FW_PCIE_NAME
;
1312 if (!platform_config_name
)
1313 platform_config_name
= DEFAULT_PLATFORM_CONFIG_NAME
;
1315 return obtain_firmware(dd
);
1318 int parse_platform_config(struct hfi1_devdata
*dd
)
1320 struct platform_config_cache
*pcfgcache
= &dd
->pcfg_cache
;
1322 u32 header1
= 0, header2
= 0, magic_num
= 0, crc
= 0;
1323 u32 record_idx
= 0, table_type
= 0, table_length_dwords
= 0;
1325 if (platform_config
== NULL
) {
1326 dd_dev_info(dd
, "%s: Missing config file\n", __func__
);
1329 ptr
= (u32
*)platform_config
->data
;
1333 if (magic_num
!= PLATFORM_CONFIG_MAGIC_NUM
) {
1334 dd_dev_info(dd
, "%s: Bad config file\n", __func__
);
1338 while (ptr
< (u32
*)(platform_config
->data
+ platform_config
->size
)) {
1340 header2
= *(ptr
+ 1);
1341 if (header1
!= ~header2
) {
1342 dd_dev_info(dd
, "%s: Failed validation at offset %ld\n",
1343 __func__
, (ptr
- (u32
*)platform_config
->data
));
1348 ((1 << PLATFORM_CONFIG_HEADER_RECORD_IDX_LEN_BITS
) - 1);
1350 table_length_dwords
= (*ptr
>>
1351 PLATFORM_CONFIG_HEADER_TABLE_LENGTH_SHIFT
) &
1352 ((1 << PLATFORM_CONFIG_HEADER_TABLE_LENGTH_LEN_BITS
) - 1);
1354 table_type
= (*ptr
>> PLATFORM_CONFIG_HEADER_TABLE_TYPE_SHIFT
) &
1355 ((1 << PLATFORM_CONFIG_HEADER_TABLE_TYPE_LEN_BITS
) - 1);
1357 /* Done with this set of headers */
1362 switch (table_type
) {
1363 case PLATFORM_CONFIG_SYSTEM_TABLE
:
1364 pcfgcache
->config_tables
[table_type
].num_table
=
1367 case PLATFORM_CONFIG_PORT_TABLE
:
1368 pcfgcache
->config_tables
[table_type
].num_table
=
1371 case PLATFORM_CONFIG_RX_PRESET_TABLE
:
1373 case PLATFORM_CONFIG_TX_PRESET_TABLE
:
1375 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE
:
1377 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE
:
1378 pcfgcache
->config_tables
[table_type
].num_table
=
1379 table_length_dwords
;
1383 "%s: Unknown data table %d, offset %ld\n",
1384 __func__
, table_type
,
1385 (ptr
- (u32
*)platform_config
->data
));
1386 goto bail
; /* We don't trust this file now */
1388 pcfgcache
->config_tables
[table_type
].table
= ptr
;
1390 /* metadata table */
1391 switch (table_type
) {
1392 case PLATFORM_CONFIG_SYSTEM_TABLE
:
1394 case PLATFORM_CONFIG_PORT_TABLE
:
1396 case PLATFORM_CONFIG_RX_PRESET_TABLE
:
1398 case PLATFORM_CONFIG_TX_PRESET_TABLE
:
1400 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE
:
1402 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE
:
1406 "%s: Unknown metadata table %d, offset %ld\n",
1407 __func__
, table_type
,
1408 (ptr
- (u32
*)platform_config
->data
));
1409 goto bail
; /* We don't trust this file now */
1411 pcfgcache
->config_tables
[table_type
].table_metadata
=
1415 /* Calculate and check table crc */
1416 crc
= crc32_le(~(u32
)0, (unsigned char const *)ptr
,
1417 (table_length_dwords
* 4));
1420 /* Jump the table */
1421 ptr
+= table_length_dwords
;
1423 dd_dev_info(dd
, "%s: Failed CRC check at offset %ld\n",
1424 __func__
, (ptr
- (u32
*)platform_config
->data
));
1427 /* Jump the CRC DWORD */
1431 pcfgcache
->cache_valid
= 1;
1434 memset(pcfgcache
, 0, sizeof(struct platform_config_cache
));
1438 static int get_platform_fw_field_metadata(struct hfi1_devdata
*dd
, int table
,
1439 int field
, u32
*field_len_bits
, u32
*field_start_bits
)
1441 struct platform_config_cache
*pcfgcache
= &dd
->pcfg_cache
;
1442 u32
*src_ptr
= NULL
;
1444 if (!pcfgcache
->cache_valid
)
1448 case PLATFORM_CONFIG_SYSTEM_TABLE
:
1450 case PLATFORM_CONFIG_PORT_TABLE
:
1452 case PLATFORM_CONFIG_RX_PRESET_TABLE
:
1454 case PLATFORM_CONFIG_TX_PRESET_TABLE
:
1456 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE
:
1458 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE
:
1459 if (field
&& field
< platform_config_table_limits
[table
])
1461 pcfgcache
->config_tables
[table
].table_metadata
+ field
;
1464 dd_dev_info(dd
, "%s: Unknown table\n", __func__
);
1471 if (field_start_bits
)
1472 *field_start_bits
= *src_ptr
&
1473 ((1 << METADATA_TABLE_FIELD_START_LEN_BITS
) - 1);
1476 *field_len_bits
= (*src_ptr
>> METADATA_TABLE_FIELD_LEN_SHIFT
)
1477 & ((1 << METADATA_TABLE_FIELD_LEN_LEN_BITS
) - 1);
1482 /* This is the central interface to getting data out of the platform config
1483 * file. It depends on parse_platform_config() having populated the
1484 * platform_config_cache in hfi1_devdata, and checks the cache_valid member to
1485 * validate the sanity of the cache.
1487 * The non-obvious parameters:
1488 * @table_index: Acts as a look up key into which instance of the tables the
1489 * relevant field is fetched from.
1491 * This applies to the data tables that have multiple instances. The port table
1492 * is an exception to this rule as each HFI only has one port and thus the
1493 * relevant table can be distinguished by hfi_id.
1495 * @data: pointer to memory that will be populated with the field requested.
1496 * @len: length of memory pointed by @data in bytes.
1498 int get_platform_config_field(struct hfi1_devdata
*dd
,
1499 enum platform_config_table_type_encoding table_type
,
1500 int table_index
, int field_index
, u32
*data
, u32 len
)
1502 int ret
= 0, wlen
= 0, seek
= 0;
1503 u32 field_len_bits
= 0, field_start_bits
= 0, *src_ptr
= NULL
;
1504 struct platform_config_cache
*pcfgcache
= &dd
->pcfg_cache
;
1507 memset(data
, 0, len
);
1511 ret
= get_platform_fw_field_metadata(dd
, table_type
, field_index
,
1512 &field_len_bits
, &field_start_bits
);
1516 /* Convert length to bits */
1519 /* Our metadata function checked cache_valid and field_index for us */
1520 switch (table_type
) {
1521 case PLATFORM_CONFIG_SYSTEM_TABLE
:
1522 src_ptr
= pcfgcache
->config_tables
[table_type
].table
;
1524 if (field_index
!= SYSTEM_TABLE_QSFP_POWER_CLASS_MAX
) {
1525 if (len
< field_len_bits
)
1528 seek
= field_start_bits
/8;
1529 wlen
= field_len_bits
/8;
1531 src_ptr
= (u32
*)((u8
*)src_ptr
+ seek
);
1533 /* We expect the field to be byte aligned and whole byte
1534 * lengths if we are here */
1535 memcpy(data
, src_ptr
, wlen
);
1539 case PLATFORM_CONFIG_PORT_TABLE
:
1540 /* Port table is 4 DWORDS in META_VERSION 0 */
1541 src_ptr
= dd
->hfi1_id
?
1542 pcfgcache
->config_tables
[table_type
].table
+ 4 :
1543 pcfgcache
->config_tables
[table_type
].table
;
1545 case PLATFORM_CONFIG_RX_PRESET_TABLE
:
1547 case PLATFORM_CONFIG_TX_PRESET_TABLE
:
1549 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE
:
1551 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE
:
1552 src_ptr
= pcfgcache
->config_tables
[table_type
].table
;
1555 pcfgcache
->config_tables
[table_type
].num_table
)
1556 src_ptr
+= table_index
;
1561 dd_dev_info(dd
, "%s: Unknown table\n", __func__
);
1565 if (!src_ptr
|| len
< field_len_bits
)
1568 src_ptr
+= (field_start_bits
/32);
1569 *data
= (*src_ptr
>> (field_start_bits
% 32)) &
1570 ((1 << field_len_bits
) - 1);
1576 * Download the firmware needed for the Gen3 PCIe SerDes. An update
1577 * to the SBus firmware is needed before updating the PCIe firmware.
1579 * Note: caller must be holding the HW mutex.
1581 int load_pcie_firmware(struct hfi1_devdata
*dd
)
1585 /* both firmware loads below use the SBus */
1586 set_sbus_fast_mode(dd
);
1589 turn_off_spicos(dd
, SPICO_SBUS
);
1590 ret
= load_sbus_firmware(dd
, &fw_sbus
);
1595 if (fw_pcie_serdes_load
) {
1596 dd_dev_info(dd
, "Setting PCIe SerDes broadcast\n");
1597 set_serdes_broadcast(dd
, all_pcie_serdes_broadcast
,
1598 pcie_serdes_broadcast
[dd
->hfi1_id
],
1599 pcie_serdes_addrs
[dd
->hfi1_id
],
1601 ret
= load_pcie_serdes_firmware(dd
, &fw_pcie
);
1607 clear_sbus_fast_mode(dd
);
1613 * Read the GUID from the hardware, store it in dd.
1615 void read_guid(struct hfi1_devdata
*dd
)
1617 dd
->base_guid
= read_csr(dd
, DC_DC8051_CFG_LOCAL_GUID
);
1618 dd_dev_info(dd
, "GUID %llx",
1619 (unsigned long long)dd
->base_guid
);