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"
71 #define ALT_FW_8051_NAME_ASIC "hfi1_dc8051_d.fw"
72 #define ALT_FW_FABRIC_NAME "hfi1_fabric_d.fw"
73 #define ALT_FW_SBUS_NAME "hfi1_sbus_d.fw"
74 #define ALT_FW_PCIE_NAME "hfi1_pcie_d.fw"
76 static uint fw_8051_load
= 1;
77 static uint fw_fabric_serdes_load
= 1;
78 static uint fw_pcie_serdes_load
= 1;
79 static uint fw_sbus_load
= 1;
80 static uint platform_config_load
= 1;
82 /* Firmware file names get set in hfi1_firmware_init() based on the above */
83 static char *fw_8051_name
;
84 static char *fw_fabric_serdes_name
;
85 static char *fw_sbus_name
;
86 static char *fw_pcie_serdes_name
;
87 static char *platform_config_name
;
89 #define SBUS_MAX_POLL_COUNT 100
90 #define SBUS_COUNTER(reg, name) \
91 (((reg) >> ASIC_STS_SBUS_COUNTERS_##name##_CNT_SHIFT) & \
92 ASIC_STS_SBUS_COUNTERS_##name##_CNT_MASK)
95 * Firmware security header.
103 u32 date
; /* BCD yyyymmdd */
104 u32 size
; /* in DWORDs */
105 u32 key_size
; /* in DWORDs */
106 u32 modulus_size
; /* in DWORDs */
107 u32 exponent_size
; /* in DWORDs */
110 /* expected field values */
111 #define CSS_MODULE_TYPE 0x00000006
112 #define CSS_HEADER_LEN 0x000000a1
113 #define CSS_HEADER_VERSION 0x00010000
114 #define CSS_MODULE_VENDOR 0x00008086
118 #define EXPONENT_SIZE 4
120 /* the file itself */
121 struct firmware_file
{
122 struct css_header css_header
;
123 u8 modulus
[KEY_SIZE
];
124 u8 exponent
[EXPONENT_SIZE
];
125 u8 signature
[KEY_SIZE
];
129 struct augmented_firmware_file
{
130 struct css_header css_header
;
131 u8 modulus
[KEY_SIZE
];
132 u8 exponent
[EXPONENT_SIZE
];
133 u8 signature
[KEY_SIZE
];
139 /* augmented file size difference */
140 #define AUGMENT_SIZE (sizeof(struct augmented_firmware_file) - \
141 sizeof(struct firmware_file))
143 struct firmware_details
{
144 /* Linux core piece */
145 const struct firmware
*fw
;
147 struct css_header
*css_header
;
148 u8
*firmware_ptr
; /* pointer to binary data */
149 u32 firmware_len
; /* length in bytes */
150 u8
*modulus
; /* pointer to the modulus */
151 u8
*exponent
; /* pointer to the exponent */
152 u8
*signature
; /* pointer to the signature */
153 u8
*r2
; /* pointer to r2 */
154 u8
*mu
; /* pointer to mu */
155 struct augmented_firmware_file dummy_header
;
159 * The mutex protects fw_state, fw_err, and all of the firmware_details
162 static DEFINE_MUTEX(fw_mutex
);
169 static enum fw_state fw_state
= FW_EMPTY
;
171 static struct firmware_details fw_8051
;
172 static struct firmware_details fw_fabric
;
173 static struct firmware_details fw_pcie
;
174 static struct firmware_details fw_sbus
;
175 static const struct firmware
*platform_config
;
177 /* flags for turn_off_spicos() */
178 #define SPICO_SBUS 0x1
179 #define SPICO_FABRIC 0x2
180 #define ENABLE_SPICO_SMASK 0x1
182 /* security block commands */
183 #define RSA_CMD_INIT 0x1
184 #define RSA_CMD_START 0x2
186 /* security block status */
187 #define RSA_STATUS_IDLE 0x0
188 #define RSA_STATUS_ACTIVE 0x1
189 #define RSA_STATUS_DONE 0x2
190 #define RSA_STATUS_FAILED 0x3
192 /* RSA engine timeout, in ms */
193 #define RSA_ENGINE_TIMEOUT 100 /* ms */
195 /* hardware mutex timeout, in ms */
196 #define HM_TIMEOUT 4000 /* 4 s */
198 /* 8051 memory access timeout, in us */
199 #define DC8051_ACCESS_TIMEOUT 100 /* us */
201 /* the number of fabric SerDes on the SBus */
202 #define NUM_FABRIC_SERDES 4
204 /* SBus fabric SerDes addresses, one set per HFI */
205 static const u8 fabric_serdes_addrs
[2][NUM_FABRIC_SERDES
] = {
206 { 0x01, 0x02, 0x03, 0x04 },
207 { 0x28, 0x29, 0x2a, 0x2b }
210 /* SBus PCIe SerDes addresses, one set per HFI */
211 static const u8 pcie_serdes_addrs
[2][NUM_PCIE_SERDES
] = {
212 { 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16,
213 0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26 },
214 { 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d,
215 0x3f, 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d }
218 /* SBus PCIe PCS addresses, one set per HFI */
219 const u8 pcie_pcs_addrs
[2][NUM_PCIE_SERDES
] = {
220 { 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17,
221 0x19, 0x1b, 0x1d, 0x1f, 0x21, 0x23, 0x25, 0x27 },
222 { 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
223 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e }
226 /* SBus fabric SerDes broadcast addresses, one per HFI */
227 static const u8 fabric_serdes_broadcast
[2] = { 0xe4, 0xe5 };
228 static const u8 all_fabric_serdes_broadcast
= 0xe1;
230 /* SBus PCIe SerDes broadcast addresses, one per HFI */
231 const u8 pcie_serdes_broadcast
[2] = { 0xe2, 0xe3 };
232 static const u8 all_pcie_serdes_broadcast
= 0xe0;
235 static void dispose_one_firmware(struct firmware_details
*fdet
);
238 * Read a single 64-bit value from 8051 data memory.
241 * o caller to have already set up data read, no auto increment
242 * o caller to turn off read enable when finished
244 * The address argument is a byte offset. Bits 0:2 in the address are
245 * ignored - i.e. the hardware will always do aligned 8-byte reads as if
246 * the lower bits are zero.
248 * Return 0 on success, -ENXIO on a read error (timeout).
250 static int __read_8051_data(struct hfi1_devdata
*dd
, u32 addr
, u64
*result
)
255 /* start the read at the given address */
256 reg
= ((addr
& DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK
)
257 << DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT
)
258 | DC_DC8051_CFG_RAM_ACCESS_CTRL_READ_ENA_SMASK
;
259 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_CTRL
, reg
);
261 /* wait until ACCESS_COMPLETED is set */
263 while ((read_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_STATUS
)
264 & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK
)
267 if (count
> DC8051_ACCESS_TIMEOUT
) {
268 dd_dev_err(dd
, "timeout reading 8051 data\n");
274 /* gather the data */
275 *result
= read_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_RD_DATA
);
281 * Read 8051 data starting at addr, for len bytes. Will read in 8-byte chunks.
282 * Return 0 on success, -errno on error.
284 int read_8051_data(struct hfi1_devdata
*dd
, u32 addr
, u32 len
, u64
*result
)
290 spin_lock_irqsave(&dd
->dc8051_memlock
, flags
);
292 /* data read set-up, no auto-increment */
293 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_SETUP
, 0);
295 for (done
= 0; done
< len
; addr
+= 8, done
+= 8, result
++) {
296 ret
= __read_8051_data(dd
, addr
, result
);
301 /* turn off read enable */
302 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_CTRL
, 0);
304 spin_unlock_irqrestore(&dd
->dc8051_memlock
, flags
);
310 * Write data or code to the 8051 code or data RAM.
312 static int write_8051(struct hfi1_devdata
*dd
, int code
, u32 start
,
313 const u8
*data
, u32 len
)
319 /* check alignment */
320 aligned
= ((unsigned long)data
& 0x7) == 0;
323 reg
= (code
? DC_DC8051_CFG_RAM_ACCESS_SETUP_RAM_SEL_SMASK
: 0ull)
324 | DC_DC8051_CFG_RAM_ACCESS_SETUP_AUTO_INCR_ADDR_SMASK
;
325 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_SETUP
, reg
);
327 reg
= ((start
& DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK
)
328 << DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT
)
329 | DC_DC8051_CFG_RAM_ACCESS_CTRL_WRITE_ENA_SMASK
;
330 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_CTRL
, reg
);
333 for (offset
= 0; offset
< len
; offset
+= 8) {
334 int bytes
= len
- offset
;
338 memcpy(®
, &data
[offset
], bytes
);
339 } else if (aligned
) {
340 reg
= *(u64
*)&data
[offset
];
342 memcpy(®
, &data
[offset
], 8);
344 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_WR_DATA
, reg
);
346 /* wait until ACCESS_COMPLETED is set */
348 while ((read_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_STATUS
)
349 & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK
)
352 if (count
> DC8051_ACCESS_TIMEOUT
) {
353 dd_dev_err(dd
, "timeout writing 8051 data\n");
360 /* turn off write access, auto increment (also sets to data access) */
361 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_CTRL
, 0);
362 write_csr(dd
, DC_DC8051_CFG_RAM_ACCESS_SETUP
, 0);
367 /* return 0 if values match, non-zero and complain otherwise */
368 static int invalid_header(struct hfi1_devdata
*dd
, const char *what
,
369 u32 actual
, u32 expected
)
371 if (actual
== expected
)
375 "invalid firmware header field %s: expected 0x%x, actual 0x%x\n",
376 what
, expected
, actual
);
381 * Verify that the static fields in the CSS header match.
383 static int verify_css_header(struct hfi1_devdata
*dd
, struct css_header
*css
)
385 /* verify CSS header fields (most sizes are in DW, so add /4) */
386 if (invalid_header(dd
, "module_type", css
->module_type
, CSS_MODULE_TYPE
)
387 || invalid_header(dd
, "header_len", css
->header_len
,
388 (sizeof(struct firmware_file
)/4))
389 || invalid_header(dd
, "header_version",
390 css
->header_version
, CSS_HEADER_VERSION
)
391 || invalid_header(dd
, "module_vendor",
392 css
->module_vendor
, CSS_MODULE_VENDOR
)
393 || invalid_header(dd
, "key_size",
394 css
->key_size
, KEY_SIZE
/4)
395 || invalid_header(dd
, "modulus_size",
396 css
->modulus_size
, KEY_SIZE
/4)
397 || invalid_header(dd
, "exponent_size",
398 css
->exponent_size
, EXPONENT_SIZE
/4)) {
405 * Make sure there are at least some bytes after the prefix.
407 static int payload_check(struct hfi1_devdata
*dd
, const char *name
,
408 long file_size
, long prefix_size
)
410 /* make sure we have some payload */
411 if (prefix_size
>= file_size
) {
413 "firmware \"%s\", size %ld, must be larger than %ld bytes\n",
414 name
, file_size
, prefix_size
);
422 * Request the firmware from the system. Extract the pieces and fill in
423 * fdet. If successful, the caller will need to call dispose_one_firmware().
424 * Returns 0 on success, -ERRNO on error.
426 static int obtain_one_firmware(struct hfi1_devdata
*dd
, const char *name
,
427 struct firmware_details
*fdet
)
429 struct css_header
*css
;
432 memset(fdet
, 0, sizeof(*fdet
));
434 ret
= request_firmware(&fdet
->fw
, name
, &dd
->pcidev
->dev
);
436 dd_dev_err(dd
, "cannot find firmware \"%s\", err %d\n",
441 /* verify the firmware */
442 if (fdet
->fw
->size
< sizeof(struct css_header
)) {
443 dd_dev_err(dd
, "firmware \"%s\" is too small\n", name
);
447 css
= (struct css_header
*)fdet
->fw
->data
;
449 hfi1_cdbg(FIRMWARE
, "Firmware %s details:", name
);
450 hfi1_cdbg(FIRMWARE
, "file size: 0x%lx bytes", fdet
->fw
->size
);
451 hfi1_cdbg(FIRMWARE
, "CSS structure:");
452 hfi1_cdbg(FIRMWARE
, " module_type 0x%x", css
->module_type
);
453 hfi1_cdbg(FIRMWARE
, " header_len 0x%03x (0x%03x bytes)",
454 css
->header_len
, 4 * css
->header_len
);
455 hfi1_cdbg(FIRMWARE
, " header_version 0x%x", css
->header_version
);
456 hfi1_cdbg(FIRMWARE
, " module_id 0x%x", css
->module_id
);
457 hfi1_cdbg(FIRMWARE
, " module_vendor 0x%x", css
->module_vendor
);
458 hfi1_cdbg(FIRMWARE
, " date 0x%x", css
->date
);
459 hfi1_cdbg(FIRMWARE
, " size 0x%03x (0x%03x bytes)",
460 css
->size
, 4 * css
->size
);
461 hfi1_cdbg(FIRMWARE
, " key_size 0x%03x (0x%03x bytes)",
462 css
->key_size
, 4 * css
->key_size
);
463 hfi1_cdbg(FIRMWARE
, " modulus_size 0x%03x (0x%03x bytes)",
464 css
->modulus_size
, 4 * css
->modulus_size
);
465 hfi1_cdbg(FIRMWARE
, " exponent_size 0x%03x (0x%03x bytes)",
466 css
->exponent_size
, 4 * css
->exponent_size
);
467 hfi1_cdbg(FIRMWARE
, "firmware size: 0x%lx bytes",
468 fdet
->fw
->size
- sizeof(struct firmware_file
));
471 * If the file does not have a valid CSS header, fail.
472 * Otherwise, check the CSS size field for an expected size.
473 * The augmented file has r2 and mu inserted after the header
474 * was generated, so there will be a known difference between
475 * the CSS header size and the actual file size. Use this
476 * difference to identify an augmented file.
478 * Note: css->size is in DWORDs, multiply by 4 to get bytes.
480 ret
= verify_css_header(dd
, css
);
482 dd_dev_info(dd
, "Invalid CSS header for \"%s\"\n", name
);
483 } else if ((css
->size
*4) == fdet
->fw
->size
) {
484 /* non-augmented firmware file */
485 struct firmware_file
*ff
= (struct firmware_file
*)
488 /* make sure there are bytes in the payload */
489 ret
= payload_check(dd
, name
, fdet
->fw
->size
,
490 sizeof(struct firmware_file
));
492 fdet
->css_header
= css
;
493 fdet
->modulus
= ff
->modulus
;
494 fdet
->exponent
= ff
->exponent
;
495 fdet
->signature
= ff
->signature
;
496 fdet
->r2
= fdet
->dummy_header
.r2
; /* use dummy space */
497 fdet
->mu
= fdet
->dummy_header
.mu
; /* use dummy space */
498 fdet
->firmware_ptr
= ff
->firmware
;
499 fdet
->firmware_len
= fdet
->fw
->size
-
500 sizeof(struct firmware_file
);
502 * Header does not include r2 and mu - generate here.
505 dd_dev_err(dd
, "driver is unable to validate firmware without r2 and mu (not in firmware file)\n");
508 } else if ((css
->size
*4) + AUGMENT_SIZE
== fdet
->fw
->size
) {
509 /* augmented firmware file */
510 struct augmented_firmware_file
*aff
=
511 (struct augmented_firmware_file
*)fdet
->fw
->data
;
513 /* make sure there are bytes in the payload */
514 ret
= payload_check(dd
, name
, fdet
->fw
->size
,
515 sizeof(struct augmented_firmware_file
));
517 fdet
->css_header
= css
;
518 fdet
->modulus
= aff
->modulus
;
519 fdet
->exponent
= aff
->exponent
;
520 fdet
->signature
= aff
->signature
;
523 fdet
->firmware_ptr
= aff
->firmware
;
524 fdet
->firmware_len
= fdet
->fw
->size
-
525 sizeof(struct augmented_firmware_file
);
528 /* css->size check failed */
530 "invalid firmware header field size: expected 0x%lx or 0x%lx, actual 0x%x\n",
531 fdet
->fw
->size
/4, (fdet
->fw
->size
- AUGMENT_SIZE
)/4,
538 /* if returning an error, clean up after ourselves */
540 dispose_one_firmware(fdet
);
544 static void dispose_one_firmware(struct firmware_details
*fdet
)
546 release_firmware(fdet
->fw
);
547 /* erase all previous information */
548 memset(fdet
, 0, sizeof(*fdet
));
552 * Obtain the 4 firmwares from the OS. All must be obtained at once or not
553 * at all. If called with the firmware state in FW_TRY, use alternate names.
554 * On exit, this routine will have set the firmware state to one of FW_TRY,
555 * FW_FINAL, or FW_ERR.
557 * Must be holding fw_mutex.
559 static void __obtain_firmware(struct hfi1_devdata
*dd
)
563 if (fw_state
== FW_FINAL
) /* nothing more to obtain */
565 if (fw_state
== FW_ERR
) /* already in error */
568 /* fw_state is FW_EMPTY or FW_TRY */
570 if (fw_state
== FW_TRY
) {
572 * We tried the original and it failed. Move to the
575 dd_dev_info(dd
, "using alternate firmware names\n");
577 * Let others run. Some systems, when missing firmware, does
578 * something that holds for 30 seconds. If we do that twice
579 * in a row it triggers task blocked warning.
583 dispose_one_firmware(&fw_8051
);
584 if (fw_fabric_serdes_load
)
585 dispose_one_firmware(&fw_fabric
);
587 dispose_one_firmware(&fw_sbus
);
588 if (fw_pcie_serdes_load
)
589 dispose_one_firmware(&fw_pcie
);
590 fw_8051_name
= ALT_FW_8051_NAME_ASIC
;
591 fw_fabric_serdes_name
= ALT_FW_FABRIC_NAME
;
592 fw_sbus_name
= ALT_FW_SBUS_NAME
;
593 fw_pcie_serdes_name
= ALT_FW_PCIE_NAME
;
597 err
= obtain_one_firmware(dd
, fw_8051_name
, &fw_8051
);
602 if (fw_fabric_serdes_load
) {
603 err
= obtain_one_firmware(dd
, fw_fabric_serdes_name
,
610 err
= obtain_one_firmware(dd
, fw_sbus_name
, &fw_sbus
);
615 if (fw_pcie_serdes_load
) {
616 err
= obtain_one_firmware(dd
, fw_pcie_serdes_name
, &fw_pcie
);
623 /* oops, had problems obtaining a firmware */
624 if (fw_state
== FW_EMPTY
) {
625 /* retry with alternate */
633 if (fw_state
== FW_EMPTY
)
634 fw_state
= FW_TRY
; /* may retry later */
636 fw_state
= FW_FINAL
; /* cannot try again */
641 * Called by all HFIs when loading their firmware - i.e. device probe time.
642 * The first one will do the actual firmware load. Use a mutex to resolve
643 * any possible race condition.
645 * The call to this routine cannot be moved to driver load because the kernel
646 * call request_firmware() requires a device which is only available after
647 * the first device probe.
649 static int obtain_firmware(struct hfi1_devdata
*dd
)
651 unsigned long timeout
;
654 mutex_lock(&fw_mutex
);
656 /* 40s delay due to long delay on missing firmware on some systems */
657 timeout
= jiffies
+ msecs_to_jiffies(40000);
658 while (fw_state
== FW_TRY
) {
660 * Another device is trying the firmware. Wait until it
661 * decides what works (or not).
663 if (time_after(jiffies
, timeout
)) {
664 /* waited too long */
665 dd_dev_err(dd
, "Timeout waiting for firmware try");
670 mutex_unlock(&fw_mutex
);
671 msleep(20); /* arbitrary delay */
672 mutex_lock(&fw_mutex
);
674 /* not in FW_TRY state */
676 if (fw_state
== FW_FINAL
)
677 goto done
; /* already acquired */
678 else if (fw_state
== FW_ERR
)
679 goto done
; /* already tried and failed */
680 /* fw_state is FW_EMPTY */
682 /* set fw_state to FW_TRY, FW_FINAL, or FW_ERR, and fw_err */
683 __obtain_firmware(dd
);
685 if (platform_config_load
) {
686 platform_config
= NULL
;
687 err
= request_firmware(&platform_config
, platform_config_name
,
690 platform_config
= NULL
;
694 mutex_unlock(&fw_mutex
);
700 * Called when the driver unloads. The timing is asymmetric with its
701 * counterpart, obtain_firmware(). If called at device remove time,
702 * then it is conceivable that another device could probe while the
703 * firmware is being disposed. The mutexes can be moved to do that
704 * safely, but then the firmware would be requested from the OS multiple
707 * No mutex is needed as the driver is unloading and there cannot be any
710 void dispose_firmware(void)
712 dispose_one_firmware(&fw_8051
);
713 dispose_one_firmware(&fw_fabric
);
714 dispose_one_firmware(&fw_pcie
);
715 dispose_one_firmware(&fw_sbus
);
717 release_firmware(platform_config
);
718 platform_config
= NULL
;
720 /* retain the error state, otherwise revert to empty */
721 if (fw_state
!= FW_ERR
)
726 * Called with the result of a firmware download.
728 * Return 1 to retry loading the firmware, 0 to stop.
730 static int retry_firmware(struct hfi1_devdata
*dd
, int load_result
)
734 mutex_lock(&fw_mutex
);
736 if (load_result
== 0) {
738 * The load succeeded, so expect all others to do the same.
739 * Do not retry again.
741 if (fw_state
== FW_TRY
)
743 retry
= 0; /* do NOT retry */
744 } else if (fw_state
== FW_TRY
) {
745 /* load failed, obtain alternate firmware */
746 __obtain_firmware(dd
);
747 retry
= (fw_state
== FW_FINAL
);
749 /* else in FW_FINAL or FW_ERR, no retry in either case */
753 mutex_unlock(&fw_mutex
);
758 * Write a block of data to a given array CSR. All calls will be in
759 * multiples of 8 bytes.
761 static void write_rsa_data(struct hfi1_devdata
*dd
, int what
,
762 const u8
*data
, int nbytes
)
764 int qw_size
= nbytes
/8;
767 if (((unsigned long)data
& 0x7) == 0) {
769 u64
*ptr
= (u64
*)data
;
771 for (i
= 0; i
< qw_size
; i
++, ptr
++)
772 write_csr(dd
, what
+ (8*i
), *ptr
);
775 for (i
= 0; i
< qw_size
; i
++, data
+= 8) {
778 memcpy(&value
, data
, 8);
779 write_csr(dd
, what
+ (8*i
), value
);
785 * Write a block of data to a given CSR as a stream of writes. All calls will
786 * be in multiples of 8 bytes.
788 static void write_streamed_rsa_data(struct hfi1_devdata
*dd
, int what
,
789 const u8
*data
, int nbytes
)
791 u64
*ptr
= (u64
*)data
;
792 int qw_size
= nbytes
/8;
794 for (; qw_size
> 0; qw_size
--, ptr
++)
795 write_csr(dd
, what
, *ptr
);
799 * Download the signature and start the RSA mechanism. Wait for
800 * RSA_ENGINE_TIMEOUT before giving up.
802 static int run_rsa(struct hfi1_devdata
*dd
, const char *who
,
805 unsigned long timeout
;
810 /* write the signature */
811 write_rsa_data(dd
, MISC_CFG_RSA_SIGNATURE
, signature
, KEY_SIZE
);
814 write_csr(dd
, MISC_CFG_RSA_CMD
, RSA_CMD_INIT
);
817 * Make sure the engine is idle and insert a delay between the two
818 * writes to MISC_CFG_RSA_CMD.
820 status
= (read_csr(dd
, MISC_CFG_FW_CTRL
)
821 & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK
)
822 >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT
;
823 if (status
!= RSA_STATUS_IDLE
) {
824 dd_dev_err(dd
, "%s security engine not idle - giving up\n",
830 write_csr(dd
, MISC_CFG_RSA_CMD
, RSA_CMD_START
);
833 * Look for the result.
835 * The RSA engine is hooked up to two MISC errors. The driver
836 * masks these errors as they do not respond to the standard
837 * error "clear down" mechanism. Look for these errors here and
838 * clear them when possible. This routine will exit with the
839 * errors of the current run still set.
841 * MISC_FW_AUTH_FAILED_ERR
842 * Firmware authorization failed. This can be cleared by
843 * re-initializing the RSA engine, then clearing the status bit.
844 * Do not re-init the RSA angine immediately after a successful
845 * run - this will reset the current authorization.
847 * MISC_KEY_MISMATCH_ERR
848 * Key does not match. The only way to clear this is to load
849 * a matching key then clear the status bit. If this error
850 * is raised, it will persist outside of this routine until a
851 * matching key is loaded.
853 timeout
= msecs_to_jiffies(RSA_ENGINE_TIMEOUT
) + jiffies
;
855 status
= (read_csr(dd
, MISC_CFG_FW_CTRL
)
856 & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK
)
857 >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT
;
859 if (status
== RSA_STATUS_IDLE
) {
860 /* should not happen */
861 dd_dev_err(dd
, "%s firmware security bad idle state\n",
865 } else if (status
== RSA_STATUS_DONE
) {
866 /* finished successfully */
868 } else if (status
== RSA_STATUS_FAILED
) {
869 /* finished unsuccessfully */
873 /* else still active */
875 if (time_after(jiffies
, timeout
)) {
877 * Timed out while active. We can't reset the engine
878 * if it is stuck active, but run through the
879 * error code to see what error bits are set.
881 dd_dev_err(dd
, "%s firmware security time out\n", who
);
890 * Arrive here on success or failure. Clear all RSA engine
891 * errors. All current errors will stick - the RSA logic is keeping
892 * error high. All previous errors will clear - the RSA logic
893 * is not keeping the error high.
895 write_csr(dd
, MISC_ERR_CLEAR
,
896 MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK
897 | MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK
);
899 * All that is left are the current errors. Print failure details,
902 reg
= read_csr(dd
, MISC_ERR_STATUS
);
904 if (reg
& MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK
)
905 dd_dev_err(dd
, "%s firmware authorization failed\n",
907 if (reg
& MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK
)
908 dd_dev_err(dd
, "%s firmware key mismatch\n", who
);
914 static void load_security_variables(struct hfi1_devdata
*dd
,
915 struct firmware_details
*fdet
)
917 /* Security variables a. Write the modulus */
918 write_rsa_data(dd
, MISC_CFG_RSA_MODULUS
, fdet
->modulus
, KEY_SIZE
);
919 /* Security variables b. Write the r2 */
920 write_rsa_data(dd
, MISC_CFG_RSA_R2
, fdet
->r2
, KEY_SIZE
);
921 /* Security variables c. Write the mu */
922 write_rsa_data(dd
, MISC_CFG_RSA_MU
, fdet
->mu
, MU_SIZE
);
923 /* Security variables d. Write the header */
924 write_streamed_rsa_data(dd
, MISC_CFG_SHA_PRELOAD
,
925 (u8
*)fdet
->css_header
, sizeof(struct css_header
));
928 /* return the 8051 firmware state */
929 static inline u32
get_firmware_state(struct hfi1_devdata
*dd
)
931 u64 reg
= read_csr(dd
, DC_DC8051_STS_CUR_STATE
);
933 return (reg
>> DC_DC8051_STS_CUR_STATE_FIRMWARE_SHIFT
)
934 & DC_DC8051_STS_CUR_STATE_FIRMWARE_MASK
;
938 * Wait until the firmware is up and ready to take host requests.
939 * Return 0 on success, -ETIMEDOUT on timeout.
941 int wait_fm_ready(struct hfi1_devdata
*dd
, u32 mstimeout
)
943 unsigned long timeout
;
945 /* in the simulator, the fake 8051 is always ready */
946 if (dd
->icode
== ICODE_FUNCTIONAL_SIMULATOR
)
949 timeout
= msecs_to_jiffies(mstimeout
) + jiffies
;
951 if (get_firmware_state(dd
) == 0xa0) /* ready */
953 if (time_after(jiffies
, timeout
)) /* timed out */
955 usleep_range(1950, 2050); /* sleep 2ms-ish */
960 * Load the 8051 firmware.
962 static int load_8051_firmware(struct hfi1_devdata
*dd
,
963 struct firmware_details
*fdet
)
971 * Load DC 8051 firmware
974 * DC reset step 1: Reset DC8051
976 reg
= DC_DC8051_CFG_RST_M8051W_SMASK
977 | DC_DC8051_CFG_RST_CRAM_SMASK
978 | DC_DC8051_CFG_RST_DRAM_SMASK
979 | DC_DC8051_CFG_RST_IRAM_SMASK
980 | DC_DC8051_CFG_RST_SFR_SMASK
;
981 write_csr(dd
, DC_DC8051_CFG_RST
, reg
);
984 * DC reset step 2 (optional): Load 8051 data memory with link
989 * DC reset step 3: Load DC8051 firmware
991 /* release all but the core reset */
992 reg
= DC_DC8051_CFG_RST_M8051W_SMASK
;
993 write_csr(dd
, DC_DC8051_CFG_RST
, reg
);
995 /* Firmware load step 1 */
996 load_security_variables(dd
, fdet
);
999 * Firmware load step 2. Clear MISC_CFG_FW_CTRL.FW_8051_LOADED
1001 write_csr(dd
, MISC_CFG_FW_CTRL
, 0);
1003 /* Firmware load steps 3-5 */
1004 ret
= write_8051(dd
, 1/*code*/, 0, fdet
->firmware_ptr
,
1005 fdet
->firmware_len
);
1010 * DC reset step 4. Host starts the DC8051 firmware
1013 * Firmware load step 6. Set MISC_CFG_FW_CTRL.FW_8051_LOADED
1015 write_csr(dd
, MISC_CFG_FW_CTRL
, MISC_CFG_FW_CTRL_FW_8051_LOADED_SMASK
);
1017 /* Firmware load steps 7-10 */
1018 ret
= run_rsa(dd
, "8051", fdet
->signature
);
1022 /* clear all reset bits, releasing the 8051 */
1023 write_csr(dd
, DC_DC8051_CFG_RST
, 0ull);
1026 * DC reset step 5. Wait for firmware to be ready to accept host
1029 ret
= wait_fm_ready(dd
, TIMEOUT_8051_START
);
1030 if (ret
) { /* timed out */
1031 dd_dev_err(dd
, "8051 start timeout, current state 0x%x\n",
1032 get_firmware_state(dd
));
1036 read_misc_status(dd
, &ver_a
, &ver_b
);
1037 dd_dev_info(dd
, "8051 firmware version %d.%d\n",
1038 (int)ver_b
, (int)ver_a
);
1039 dd
->dc8051_ver
= dc8051_ver(ver_b
, ver_a
);
1045 * Write the SBus request register
1047 * No need for masking - the arguments are sized exactly.
1049 void sbus_request(struct hfi1_devdata
*dd
,
1050 u8 receiver_addr
, u8 data_addr
, u8 command
, u32 data_in
)
1052 write_csr(dd
, ASIC_CFG_SBUS_REQUEST
,
1053 ((u64
)data_in
<< ASIC_CFG_SBUS_REQUEST_DATA_IN_SHIFT
)
1054 | ((u64
)command
<< ASIC_CFG_SBUS_REQUEST_COMMAND_SHIFT
)
1055 | ((u64
)data_addr
<< ASIC_CFG_SBUS_REQUEST_DATA_ADDR_SHIFT
)
1056 | ((u64
)receiver_addr
1057 << ASIC_CFG_SBUS_REQUEST_RECEIVER_ADDR_SHIFT
));
1061 * Turn off the SBus and fabric serdes spicos.
1063 * + Must be called with Sbus fast mode turned on.
1064 * + Must be called after fabric serdes broadcast is set up.
1065 * + Must be called before the 8051 is loaded - assumes 8051 is not loaded
1066 * when using MISC_CFG_FW_CTRL.
1068 static void turn_off_spicos(struct hfi1_devdata
*dd
, int flags
)
1070 /* only needed on A0 */
1074 dd_dev_info(dd
, "Turning off spicos:%s%s\n",
1075 flags
& SPICO_SBUS
? " SBus" : "",
1076 flags
& SPICO_FABRIC
? " fabric" : "");
1078 write_csr(dd
, MISC_CFG_FW_CTRL
, ENABLE_SPICO_SMASK
);
1079 /* disable SBus spico */
1080 if (flags
& SPICO_SBUS
)
1081 sbus_request(dd
, SBUS_MASTER_BROADCAST
, 0x01,
1082 WRITE_SBUS_RECEIVER
, 0x00000040);
1084 /* disable the fabric serdes spicos */
1085 if (flags
& SPICO_FABRIC
)
1086 sbus_request(dd
, fabric_serdes_broadcast
[dd
->hfi1_id
],
1087 0x07, WRITE_SBUS_RECEIVER
, 0x00000000);
1088 write_csr(dd
, MISC_CFG_FW_CTRL
, 0);
1092 * Reset all of the fabric serdes for our HFI.
1094 void fabric_serdes_reset(struct hfi1_devdata
*dd
)
1098 if (dd
->icode
!= ICODE_RTL_SILICON
) /* only for RTL */
1101 ra
= fabric_serdes_broadcast
[dd
->hfi1_id
];
1103 acquire_hw_mutex(dd
);
1104 set_sbus_fast_mode(dd
);
1105 /* place SerDes in reset and disable SPICO */
1106 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000011);
1107 /* wait 100 refclk cycles @ 156.25MHz => 640ns */
1109 /* remove SerDes reset */
1110 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000010);
1111 /* turn SPICO enable on */
1112 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000002);
1113 clear_sbus_fast_mode(dd
);
1114 release_hw_mutex(dd
);
1117 /* Access to the SBus in this routine should probably be serialized */
1118 int sbus_request_slow(struct hfi1_devdata
*dd
,
1119 u8 receiver_addr
, u8 data_addr
, u8 command
, u32 data_in
)
1123 sbus_request(dd
, receiver_addr
, data_addr
, command
, data_in
);
1124 write_csr(dd
, ASIC_CFG_SBUS_EXECUTE
,
1125 ASIC_CFG_SBUS_EXECUTE_EXECUTE_SMASK
);
1126 /* Wait for both DONE and RCV_DATA_VALID to go high */
1127 reg
= read_csr(dd
, ASIC_STS_SBUS_RESULT
);
1128 while (!((reg
& ASIC_STS_SBUS_RESULT_DONE_SMASK
) &&
1129 (reg
& ASIC_STS_SBUS_RESULT_RCV_DATA_VALID_SMASK
))) {
1130 if (count
++ >= SBUS_MAX_POLL_COUNT
) {
1131 u64 counts
= read_csr(dd
, ASIC_STS_SBUS_COUNTERS
);
1133 * If the loop has timed out, we are OK if DONE bit
1134 * is set and RCV_DATA_VALID and EXECUTE counters
1135 * are the same. If not, we cannot proceed.
1137 if ((reg
& ASIC_STS_SBUS_RESULT_DONE_SMASK
) &&
1138 (SBUS_COUNTER(counts
, RCV_DATA_VALID
) ==
1139 SBUS_COUNTER(counts
, EXECUTE
)))
1144 reg
= read_csr(dd
, ASIC_STS_SBUS_RESULT
);
1147 write_csr(dd
, ASIC_CFG_SBUS_EXECUTE
, 0);
1148 /* Wait for DONE to clear after EXECUTE is cleared */
1149 reg
= read_csr(dd
, ASIC_STS_SBUS_RESULT
);
1150 while (reg
& ASIC_STS_SBUS_RESULT_DONE_SMASK
) {
1151 if (count
++ >= SBUS_MAX_POLL_COUNT
)
1154 reg
= read_csr(dd
, ASIC_STS_SBUS_RESULT
);
1159 static int load_fabric_serdes_firmware(struct hfi1_devdata
*dd
,
1160 struct firmware_details
*fdet
)
1163 const u8 ra
= fabric_serdes_broadcast
[dd
->hfi1_id
]; /* receiver addr */
1165 dd_dev_info(dd
, "Downloading fabric firmware\n");
1167 /* step 1: load security variables */
1168 load_security_variables(dd
, fdet
);
1169 /* step 2: place SerDes in reset and disable SPICO */
1170 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000011);
1171 /* wait 100 refclk cycles @ 156.25MHz => 640ns */
1173 /* step 3: remove SerDes reset */
1174 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000010);
1175 /* step 4: assert IMEM override */
1176 sbus_request(dd
, ra
, 0x00, WRITE_SBUS_RECEIVER
, 0x40000000);
1177 /* step 5: download SerDes machine code */
1178 for (i
= 0; i
< fdet
->firmware_len
; i
+= 4) {
1179 sbus_request(dd
, ra
, 0x0a, WRITE_SBUS_RECEIVER
,
1180 *(u32
*)&fdet
->firmware_ptr
[i
]);
1182 /* step 6: IMEM override off */
1183 sbus_request(dd
, ra
, 0x00, WRITE_SBUS_RECEIVER
, 0x00000000);
1184 /* step 7: turn ECC on */
1185 sbus_request(dd
, ra
, 0x0b, WRITE_SBUS_RECEIVER
, 0x000c0000);
1187 /* steps 8-11: run the RSA engine */
1188 err
= run_rsa(dd
, "fabric serdes", fdet
->signature
);
1192 /* step 12: turn SPICO enable on */
1193 sbus_request(dd
, ra
, 0x07, WRITE_SBUS_RECEIVER
, 0x00000002);
1194 /* step 13: enable core hardware interrupts */
1195 sbus_request(dd
, ra
, 0x08, WRITE_SBUS_RECEIVER
, 0x00000000);
1200 static int load_sbus_firmware(struct hfi1_devdata
*dd
,
1201 struct firmware_details
*fdet
)
1204 const u8 ra
= SBUS_MASTER_BROADCAST
; /* receiver address */
1206 dd_dev_info(dd
, "Downloading SBus firmware\n");
1208 /* step 1: load security variables */
1209 load_security_variables(dd
, fdet
);
1210 /* step 2: place SPICO into reset and enable off */
1211 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x000000c0);
1212 /* step 3: remove reset, enable off, IMEM_CNTRL_EN on */
1213 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x00000240);
1214 /* step 4: set starting IMEM address for burst download */
1215 sbus_request(dd
, ra
, 0x03, WRITE_SBUS_RECEIVER
, 0x80000000);
1216 /* step 5: download the SBus Master machine code */
1217 for (i
= 0; i
< fdet
->firmware_len
; i
+= 4) {
1218 sbus_request(dd
, ra
, 0x14, WRITE_SBUS_RECEIVER
,
1219 *(u32
*)&fdet
->firmware_ptr
[i
]);
1221 /* step 6: set IMEM_CNTL_EN off */
1222 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x00000040);
1223 /* step 7: turn ECC on */
1224 sbus_request(dd
, ra
, 0x16, WRITE_SBUS_RECEIVER
, 0x000c0000);
1226 /* steps 8-11: run the RSA engine */
1227 err
= run_rsa(dd
, "SBus", fdet
->signature
);
1231 /* step 12: set SPICO_ENABLE on */
1232 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x00000140);
1237 static int load_pcie_serdes_firmware(struct hfi1_devdata
*dd
,
1238 struct firmware_details
*fdet
)
1241 const u8 ra
= SBUS_MASTER_BROADCAST
; /* receiver address */
1243 dd_dev_info(dd
, "Downloading PCIe firmware\n");
1245 /* step 1: load security variables */
1246 load_security_variables(dd
, fdet
);
1247 /* step 2: assert single step (halts the SBus Master spico) */
1248 sbus_request(dd
, ra
, 0x05, WRITE_SBUS_RECEIVER
, 0x00000001);
1249 /* step 3: enable XDMEM access */
1250 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x00000d40);
1251 /* step 4: load firmware into SBus Master XDMEM */
1252 /* NOTE: the dmem address, write_en, and wdata are all pre-packed,
1253 we only need to pick up the bytes and write them */
1254 for (i
= 0; i
< fdet
->firmware_len
; i
+= 4) {
1255 sbus_request(dd
, ra
, 0x04, WRITE_SBUS_RECEIVER
,
1256 *(u32
*)&fdet
->firmware_ptr
[i
]);
1258 /* step 5: disable XDMEM access */
1259 sbus_request(dd
, ra
, 0x01, WRITE_SBUS_RECEIVER
, 0x00000140);
1260 /* step 6: allow SBus Spico to run */
1261 sbus_request(dd
, ra
, 0x05, WRITE_SBUS_RECEIVER
, 0x00000000);
1263 /* steps 7-11: run RSA, if it succeeds, firmware is available to
1265 return run_rsa(dd
, "PCIe serdes", fdet
->signature
);
1269 * Set the given broadcast values on the given list of devices.
1271 static void set_serdes_broadcast(struct hfi1_devdata
*dd
, u8 bg1
, u8 bg2
,
1272 const u8
*addrs
, int count
)
1274 while (--count
>= 0) {
1276 * Set BROADCAST_GROUP_1 and BROADCAST_GROUP_2, leave
1277 * defaults for everything else. Do not read-modify-write,
1278 * per instruction from the manufacturer.
1282 * ----- ---------------------------------
1283 * 0 IGNORE_BROADCAST (default 0)
1284 * 11:4 BROADCAST_GROUP_1 (default 0xff)
1285 * 23:16 BROADCAST_GROUP_2 (default 0xff)
1287 sbus_request(dd
, addrs
[count
], 0xfd, WRITE_SBUS_RECEIVER
,
1288 (u32
)bg1
<< 4 | (u32
)bg2
<< 16);
1292 int acquire_hw_mutex(struct hfi1_devdata
*dd
)
1294 unsigned long timeout
;
1296 u8 mask
= 1 << dd
->hfi1_id
;
1300 timeout
= msecs_to_jiffies(HM_TIMEOUT
) + jiffies
;
1302 write_csr(dd
, ASIC_CFG_MUTEX
, mask
);
1303 user
= (u8
)read_csr(dd
, ASIC_CFG_MUTEX
);
1305 return 0; /* success */
1306 if (time_after(jiffies
, timeout
))
1307 break; /* timed out */
1313 "Unable to acquire hardware mutex, mutex mask %u, my mask %u (%s)\n",
1314 (u32
)user
, (u32
)mask
, (try == 0) ? "retrying" : "giving up");
1317 /* break mutex and retry */
1318 write_csr(dd
, ASIC_CFG_MUTEX
, 0);
1326 void release_hw_mutex(struct hfi1_devdata
*dd
)
1328 write_csr(dd
, ASIC_CFG_MUTEX
, 0);
1331 void set_sbus_fast_mode(struct hfi1_devdata
*dd
)
1333 write_csr(dd
, ASIC_CFG_SBUS_EXECUTE
,
1334 ASIC_CFG_SBUS_EXECUTE_FAST_MODE_SMASK
);
1337 void clear_sbus_fast_mode(struct hfi1_devdata
*dd
)
1341 reg
= read_csr(dd
, ASIC_STS_SBUS_COUNTERS
);
1342 while (SBUS_COUNTER(reg
, EXECUTE
) !=
1343 SBUS_COUNTER(reg
, RCV_DATA_VALID
)) {
1344 if (count
++ >= SBUS_MAX_POLL_COUNT
)
1347 reg
= read_csr(dd
, ASIC_STS_SBUS_COUNTERS
);
1349 write_csr(dd
, ASIC_CFG_SBUS_EXECUTE
, 0);
1352 int load_firmware(struct hfi1_devdata
*dd
)
1356 if (fw_fabric_serdes_load
) {
1357 ret
= acquire_hw_mutex(dd
);
1361 set_sbus_fast_mode(dd
);
1363 set_serdes_broadcast(dd
, all_fabric_serdes_broadcast
,
1364 fabric_serdes_broadcast
[dd
->hfi1_id
],
1365 fabric_serdes_addrs
[dd
->hfi1_id
],
1367 turn_off_spicos(dd
, SPICO_FABRIC
);
1369 ret
= load_fabric_serdes_firmware(dd
, &fw_fabric
);
1370 } while (retry_firmware(dd
, ret
));
1372 clear_sbus_fast_mode(dd
);
1373 release_hw_mutex(dd
);
1380 ret
= load_8051_firmware(dd
, &fw_8051
);
1381 } while (retry_firmware(dd
, ret
));
1389 int hfi1_firmware_init(struct hfi1_devdata
*dd
)
1391 /* only RTL can use these */
1392 if (dd
->icode
!= ICODE_RTL_SILICON
) {
1393 fw_fabric_serdes_load
= 0;
1394 fw_pcie_serdes_load
= 0;
1398 /* no 8051 or QSFP on simulator */
1399 if (dd
->icode
== ICODE_FUNCTIONAL_SIMULATOR
) {
1401 platform_config_load
= 0;
1404 if (!fw_8051_name
) {
1405 if (dd
->icode
== ICODE_RTL_SILICON
)
1406 fw_8051_name
= DEFAULT_FW_8051_NAME_ASIC
;
1408 fw_8051_name
= DEFAULT_FW_8051_NAME_FPGA
;
1410 if (!fw_fabric_serdes_name
)
1411 fw_fabric_serdes_name
= DEFAULT_FW_FABRIC_NAME
;
1413 fw_sbus_name
= DEFAULT_FW_SBUS_NAME
;
1414 if (!fw_pcie_serdes_name
)
1415 fw_pcie_serdes_name
= DEFAULT_FW_PCIE_NAME
;
1416 if (!platform_config_name
)
1417 platform_config_name
= DEFAULT_PLATFORM_CONFIG_NAME
;
1419 return obtain_firmware(dd
);
1422 int parse_platform_config(struct hfi1_devdata
*dd
)
1424 struct platform_config_cache
*pcfgcache
= &dd
->pcfg_cache
;
1426 u32 header1
= 0, header2
= 0, magic_num
= 0, crc
= 0;
1427 u32 record_idx
= 0, table_type
= 0, table_length_dwords
= 0;
1429 if (platform_config
== NULL
) {
1430 dd_dev_info(dd
, "%s: Missing config file\n", __func__
);
1433 ptr
= (u32
*)platform_config
->data
;
1437 if (magic_num
!= PLATFORM_CONFIG_MAGIC_NUM
) {
1438 dd_dev_info(dd
, "%s: Bad config file\n", __func__
);
1442 while (ptr
< (u32
*)(platform_config
->data
+ platform_config
->size
)) {
1444 header2
= *(ptr
+ 1);
1445 if (header1
!= ~header2
) {
1446 dd_dev_info(dd
, "%s: Failed validation at offset %ld\n",
1447 __func__
, (ptr
- (u32
*)platform_config
->data
));
1452 ((1 << PLATFORM_CONFIG_HEADER_RECORD_IDX_LEN_BITS
) - 1);
1454 table_length_dwords
= (*ptr
>>
1455 PLATFORM_CONFIG_HEADER_TABLE_LENGTH_SHIFT
) &
1456 ((1 << PLATFORM_CONFIG_HEADER_TABLE_LENGTH_LEN_BITS
) - 1);
1458 table_type
= (*ptr
>> PLATFORM_CONFIG_HEADER_TABLE_TYPE_SHIFT
) &
1459 ((1 << PLATFORM_CONFIG_HEADER_TABLE_TYPE_LEN_BITS
) - 1);
1461 /* Done with this set of headers */
1466 switch (table_type
) {
1467 case PLATFORM_CONFIG_SYSTEM_TABLE
:
1468 pcfgcache
->config_tables
[table_type
].num_table
=
1471 case PLATFORM_CONFIG_PORT_TABLE
:
1472 pcfgcache
->config_tables
[table_type
].num_table
=
1475 case PLATFORM_CONFIG_RX_PRESET_TABLE
:
1477 case PLATFORM_CONFIG_TX_PRESET_TABLE
:
1479 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE
:
1481 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE
:
1482 pcfgcache
->config_tables
[table_type
].num_table
=
1483 table_length_dwords
;
1487 "%s: Unknown data table %d, offset %ld\n",
1488 __func__
, table_type
,
1489 (ptr
- (u32
*)platform_config
->data
));
1490 goto bail
; /* We don't trust this file now */
1492 pcfgcache
->config_tables
[table_type
].table
= ptr
;
1494 /* metadata table */
1495 switch (table_type
) {
1496 case PLATFORM_CONFIG_SYSTEM_TABLE
:
1498 case PLATFORM_CONFIG_PORT_TABLE
:
1500 case PLATFORM_CONFIG_RX_PRESET_TABLE
:
1502 case PLATFORM_CONFIG_TX_PRESET_TABLE
:
1504 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE
:
1506 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE
:
1510 "%s: Unknown metadata table %d, offset %ld\n",
1511 __func__
, table_type
,
1512 (ptr
- (u32
*)platform_config
->data
));
1513 goto bail
; /* We don't trust this file now */
1515 pcfgcache
->config_tables
[table_type
].table_metadata
=
1519 /* Calculate and check table crc */
1520 crc
= crc32_le(~(u32
)0, (unsigned char const *)ptr
,
1521 (table_length_dwords
* 4));
1524 /* Jump the table */
1525 ptr
+= table_length_dwords
;
1527 dd_dev_info(dd
, "%s: Failed CRC check at offset %ld\n",
1528 __func__
, (ptr
- (u32
*)platform_config
->data
));
1531 /* Jump the CRC DWORD */
1535 pcfgcache
->cache_valid
= 1;
1538 memset(pcfgcache
, 0, sizeof(struct platform_config_cache
));
1542 static int get_platform_fw_field_metadata(struct hfi1_devdata
*dd
, int table
,
1543 int field
, u32
*field_len_bits
, u32
*field_start_bits
)
1545 struct platform_config_cache
*pcfgcache
= &dd
->pcfg_cache
;
1546 u32
*src_ptr
= NULL
;
1548 if (!pcfgcache
->cache_valid
)
1552 case PLATFORM_CONFIG_SYSTEM_TABLE
:
1554 case PLATFORM_CONFIG_PORT_TABLE
:
1556 case PLATFORM_CONFIG_RX_PRESET_TABLE
:
1558 case PLATFORM_CONFIG_TX_PRESET_TABLE
:
1560 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE
:
1562 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE
:
1563 if (field
&& field
< platform_config_table_limits
[table
])
1565 pcfgcache
->config_tables
[table
].table_metadata
+ field
;
1568 dd_dev_info(dd
, "%s: Unknown table\n", __func__
);
1575 if (field_start_bits
)
1576 *field_start_bits
= *src_ptr
&
1577 ((1 << METADATA_TABLE_FIELD_START_LEN_BITS
) - 1);
1580 *field_len_bits
= (*src_ptr
>> METADATA_TABLE_FIELD_LEN_SHIFT
)
1581 & ((1 << METADATA_TABLE_FIELD_LEN_LEN_BITS
) - 1);
1586 /* This is the central interface to getting data out of the platform config
1587 * file. It depends on parse_platform_config() having populated the
1588 * platform_config_cache in hfi1_devdata, and checks the cache_valid member to
1589 * validate the sanity of the cache.
1591 * The non-obvious parameters:
1592 * @table_index: Acts as a look up key into which instance of the tables the
1593 * relevant field is fetched from.
1595 * This applies to the data tables that have multiple instances. The port table
1596 * is an exception to this rule as each HFI only has one port and thus the
1597 * relevant table can be distinguished by hfi_id.
1599 * @data: pointer to memory that will be populated with the field requested.
1600 * @len: length of memory pointed by @data in bytes.
1602 int get_platform_config_field(struct hfi1_devdata
*dd
,
1603 enum platform_config_table_type_encoding table_type
,
1604 int table_index
, int field_index
, u32
*data
, u32 len
)
1606 int ret
= 0, wlen
= 0, seek
= 0;
1607 u32 field_len_bits
= 0, field_start_bits
= 0, *src_ptr
= NULL
;
1608 struct platform_config_cache
*pcfgcache
= &dd
->pcfg_cache
;
1611 memset(data
, 0, len
);
1615 ret
= get_platform_fw_field_metadata(dd
, table_type
, field_index
,
1616 &field_len_bits
, &field_start_bits
);
1620 /* Convert length to bits */
1623 /* Our metadata function checked cache_valid and field_index for us */
1624 switch (table_type
) {
1625 case PLATFORM_CONFIG_SYSTEM_TABLE
:
1626 src_ptr
= pcfgcache
->config_tables
[table_type
].table
;
1628 if (field_index
!= SYSTEM_TABLE_QSFP_POWER_CLASS_MAX
) {
1629 if (len
< field_len_bits
)
1632 seek
= field_start_bits
/8;
1633 wlen
= field_len_bits
/8;
1635 src_ptr
= (u32
*)((u8
*)src_ptr
+ seek
);
1637 /* We expect the field to be byte aligned and whole byte
1638 * lengths if we are here */
1639 memcpy(data
, src_ptr
, wlen
);
1643 case PLATFORM_CONFIG_PORT_TABLE
:
1644 /* Port table is 4 DWORDS in META_VERSION 0 */
1645 src_ptr
= dd
->hfi1_id
?
1646 pcfgcache
->config_tables
[table_type
].table
+ 4 :
1647 pcfgcache
->config_tables
[table_type
].table
;
1649 case PLATFORM_CONFIG_RX_PRESET_TABLE
:
1651 case PLATFORM_CONFIG_TX_PRESET_TABLE
:
1653 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE
:
1655 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE
:
1656 src_ptr
= pcfgcache
->config_tables
[table_type
].table
;
1659 pcfgcache
->config_tables
[table_type
].num_table
)
1660 src_ptr
+= table_index
;
1665 dd_dev_info(dd
, "%s: Unknown table\n", __func__
);
1669 if (!src_ptr
|| len
< field_len_bits
)
1672 src_ptr
+= (field_start_bits
/32);
1673 *data
= (*src_ptr
>> (field_start_bits
% 32)) &
1674 ((1 << field_len_bits
) - 1);
1680 * Download the firmware needed for the Gen3 PCIe SerDes. An update
1681 * to the SBus firmware is needed before updating the PCIe firmware.
1683 * Note: caller must be holding the HW mutex.
1685 int load_pcie_firmware(struct hfi1_devdata
*dd
)
1689 /* both firmware loads below use the SBus */
1690 set_sbus_fast_mode(dd
);
1693 turn_off_spicos(dd
, SPICO_SBUS
);
1695 ret
= load_sbus_firmware(dd
, &fw_sbus
);
1696 } while (retry_firmware(dd
, ret
));
1701 if (fw_pcie_serdes_load
) {
1702 dd_dev_info(dd
, "Setting PCIe SerDes broadcast\n");
1703 set_serdes_broadcast(dd
, all_pcie_serdes_broadcast
,
1704 pcie_serdes_broadcast
[dd
->hfi1_id
],
1705 pcie_serdes_addrs
[dd
->hfi1_id
],
1708 ret
= load_pcie_serdes_firmware(dd
, &fw_pcie
);
1709 } while (retry_firmware(dd
, ret
));
1715 clear_sbus_fast_mode(dd
);
1721 * Read the GUID from the hardware, store it in dd.
1723 void read_guid(struct hfi1_devdata
*dd
)
1725 /* Take the DC out of reset to get a valid GUID value */
1726 write_csr(dd
, CCE_DC_CTRL
, 0);
1727 (void) read_csr(dd
, CCE_DC_CTRL
);
1729 dd
->base_guid
= read_csr(dd
, DC_DC8051_CFG_LOCAL_GUID
);
1730 dd_dev_info(dd
, "GUID %llx",
1731 (unsigned long long)dd
->base_guid
);