2 * Copyright(c) 2015, 2016 Intel Corporation.
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include <linux/firmware.h>
49 #include <linux/mutex.h>
50 #include <linux/module.h>
51 #include <linux/delay.h>
52 #include <linux/crc32.h>
58 * Make it easy to toggle firmware file name and if it gets loaded by
59 * editing the following. This may be something we do while in development
60 * but not necessarily something a user would ever need to use.
62 #define DEFAULT_FW_8051_NAME_FPGA "/*(DEBLOBBED)*/"
63 #define DEFAULT_FW_8051_NAME_ASIC "/*(DEBLOBBED)*/"
64 #define DEFAULT_FW_FABRIC_NAME "/*(DEBLOBBED)*/"
65 #define DEFAULT_FW_SBUS_NAME "/*(DEBLOBBED)*/"
66 #define DEFAULT_FW_PCIE_NAME "/*(DEBLOBBED)*/"
67 #define DEFAULT_PLATFORM_CONFIG_NAME "/*(DEBLOBBED)*/"
68 #define ALT_FW_8051_NAME_ASIC "/*(DEBLOBBED)*/"
69 #define ALT_FW_FABRIC_NAME "/*(DEBLOBBED)*/"
70 #define ALT_FW_SBUS_NAME "/*(DEBLOBBED)*/"
71 #define ALT_FW_PCIE_NAME "/*(DEBLOBBED)*/"
73 static uint fw_8051_load = 1;
74 static uint fw_fabric_serdes_load = 1;
75 static uint fw_pcie_serdes_load = 1;
76 static uint fw_sbus_load = 1;
79 * Access required in platform.c
80 * Maintains state of whether the platform config was fetched via the
83 uint platform_config_load;
85 /* Firmware file names get set in hfi1_firmware_init() based on the above */
86 static char *fw_8051_name;
87 static char *fw_fabric_serdes_name;
88 static char *fw_sbus_name;
89 static char *fw_pcie_serdes_name;
90 static char *platform_config_name;
92 #define SBUS_MAX_POLL_COUNT 100
93 #define SBUS_COUNTER(reg, name) \
94 (((reg) >> ASIC_STS_SBUS_COUNTERS_##name##_CNT_SHIFT) & \
95 ASIC_STS_SBUS_COUNTERS_##name##_CNT_MASK)
98 * Firmware security header.
106 u32 date; /* BCD yyyymmdd */
107 u32 size; /* in DWORDs */
108 u32 key_size; /* in DWORDs */
109 u32 modulus_size; /* in DWORDs */
110 u32 exponent_size; /* in DWORDs */
114 /* expected field values */
115 #define CSS_MODULE_TYPE 0x00000006
116 #define CSS_HEADER_LEN 0x000000a1
117 #define CSS_HEADER_VERSION 0x00010000
118 #define CSS_MODULE_VENDOR 0x00008086
122 #define EXPONENT_SIZE 4
124 /* the file itself */
125 struct firmware_file {
126 struct css_header css_header;
127 u8 modulus[KEY_SIZE];
128 u8 exponent[EXPONENT_SIZE];
129 u8 signature[KEY_SIZE];
133 struct augmented_firmware_file {
134 struct css_header css_header;
135 u8 modulus[KEY_SIZE];
136 u8 exponent[EXPONENT_SIZE];
137 u8 signature[KEY_SIZE];
143 /* augmented file size difference */
144 #define AUGMENT_SIZE (sizeof(struct augmented_firmware_file) - \
145 sizeof(struct firmware_file))
147 struct firmware_details {
148 /* Linux core piece */
149 const struct firmware *fw;
151 struct css_header *css_header;
152 u8 *firmware_ptr; /* pointer to binary data */
153 u32 firmware_len; /* length in bytes */
154 u8 *modulus; /* pointer to the modulus */
155 u8 *exponent; /* pointer to the exponent */
156 u8 *signature; /* pointer to the signature */
157 u8 *r2; /* pointer to r2 */
158 u8 *mu; /* pointer to mu */
159 struct augmented_firmware_file dummy_header;
163 * The mutex protects fw_state, fw_err, and all of the firmware_details
166 static DEFINE_MUTEX(fw_mutex);
174 static enum fw_state fw_state = FW_EMPTY;
176 static struct firmware_details fw_8051;
177 static struct firmware_details fw_fabric;
178 static struct firmware_details fw_pcie;
179 static struct firmware_details fw_sbus;
180 static const struct firmware *platform_config;
182 /* flags for turn_off_spicos() */
183 #define SPICO_SBUS 0x1
184 #define SPICO_FABRIC 0x2
185 #define ENABLE_SPICO_SMASK 0x1
187 /* security block commands */
188 #define RSA_CMD_INIT 0x1
189 #define RSA_CMD_START 0x2
191 /* security block status */
192 #define RSA_STATUS_IDLE 0x0
193 #define RSA_STATUS_ACTIVE 0x1
194 #define RSA_STATUS_DONE 0x2
195 #define RSA_STATUS_FAILED 0x3
197 /* RSA engine timeout, in ms */
198 #define RSA_ENGINE_TIMEOUT 100 /* ms */
200 /* hardware mutex timeout, in ms */
201 #define HM_TIMEOUT 10 /* ms */
203 /* 8051 memory access timeout, in us */
204 #define DC8051_ACCESS_TIMEOUT 100 /* us */
206 /* the number of fabric SerDes on the SBus */
207 #define NUM_FABRIC_SERDES 4
209 /* ASIC_STS_SBUS_RESULT.RESULT_CODE value */
210 #define SBUS_READ_COMPLETE 0x4
212 /* SBus fabric SerDes addresses, one set per HFI */
213 static const u8 fabric_serdes_addrs[2][NUM_FABRIC_SERDES] = {
214 { 0x01, 0x02, 0x03, 0x04 },
215 { 0x28, 0x29, 0x2a, 0x2b }
218 /* SBus PCIe SerDes addresses, one set per HFI */
219 static const u8 pcie_serdes_addrs[2][NUM_PCIE_SERDES] = {
220 { 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16,
221 0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26 },
222 { 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d,
223 0x3f, 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d }
226 /* SBus PCIe PCS addresses, one set per HFI */
227 const u8 pcie_pcs_addrs[2][NUM_PCIE_SERDES] = {
228 { 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17,
229 0x19, 0x1b, 0x1d, 0x1f, 0x21, 0x23, 0x25, 0x27 },
230 { 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
231 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e }
234 /* SBus fabric SerDes broadcast addresses, one per HFI */
235 static const u8 fabric_serdes_broadcast[2] = { 0xe4, 0xe5 };
236 static const u8 all_fabric_serdes_broadcast = 0xe1;
238 /* SBus PCIe SerDes broadcast addresses, one per HFI */
239 const u8 pcie_serdes_broadcast[2] = { 0xe2, 0xe3 };
240 static const u8 all_pcie_serdes_broadcast = 0xe0;
243 static void dispose_one_firmware(struct firmware_details *fdet);
244 static int load_fabric_serdes_firmware(struct hfi1_devdata *dd,
245 struct firmware_details *fdet);
246 static void dump_fw_version(struct hfi1_devdata *dd);
249 * Read a single 64-bit value from 8051 data memory.
252 * o caller to have already set up data read, no auto increment
253 * o caller to turn off read enable when finished
255 * The address argument is a byte offset. Bits 0:2 in the address are
256 * ignored - i.e. the hardware will always do aligned 8-byte reads as if
257 * the lower bits are zero.
259 * Return 0 on success, -ENXIO on a read error (timeout).
261 static int __read_8051_data(struct hfi1_devdata *dd, u32 addr, u64 *result)
266 /* start the read at the given address */
267 reg = ((addr & DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK)
268 << DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT)
269 | DC_DC8051_CFG_RAM_ACCESS_CTRL_READ_ENA_SMASK;
270 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, reg);
272 /* wait until ACCESS_COMPLETED is set */
274 while ((read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_STATUS)
275 & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK)
278 if (count > DC8051_ACCESS_TIMEOUT) {
279 dd_dev_err(dd, "timeout reading 8051 data\n");
285 /* gather the data */
286 *result = read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_RD_DATA);
292 * Read 8051 data starting at addr, for len bytes. Will read in 8-byte chunks.
293 * Return 0 on success, -errno on error.
295 int read_8051_data(struct hfi1_devdata *dd, u32 addr, u32 len, u64 *result)
301 spin_lock_irqsave(&dd->dc8051_memlock, flags);
303 /* data read set-up, no auto-increment */
304 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, 0);
306 for (done = 0; done < len; addr += 8, done += 8, result++) {
307 ret = __read_8051_data(dd, addr, result);
312 /* turn off read enable */
313 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, 0);
315 spin_unlock_irqrestore(&dd->dc8051_memlock, flags);
321 * Write data or code to the 8051 code or data RAM.
323 static int write_8051(struct hfi1_devdata *dd, int code, u32 start,
324 const u8 *data, u32 len)
330 /* check alignment */
331 aligned = ((unsigned long)data & 0x7) == 0;
334 reg = (code ? DC_DC8051_CFG_RAM_ACCESS_SETUP_RAM_SEL_SMASK : 0ull)
335 | DC_DC8051_CFG_RAM_ACCESS_SETUP_AUTO_INCR_ADDR_SMASK;
336 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, reg);
338 reg = ((start & DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK)
339 << DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT)
340 | DC_DC8051_CFG_RAM_ACCESS_CTRL_WRITE_ENA_SMASK;
341 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, reg);
344 for (offset = 0; offset < len; offset += 8) {
345 int bytes = len - offset;
349 memcpy(®, &data[offset], bytes);
350 } else if (aligned) {
351 reg = *(u64 *)&data[offset];
353 memcpy(®, &data[offset], 8);
355 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_WR_DATA, reg);
357 /* wait until ACCESS_COMPLETED is set */
359 while ((read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_STATUS)
360 & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK)
363 if (count > DC8051_ACCESS_TIMEOUT) {
364 dd_dev_err(dd, "timeout writing 8051 data\n");
371 /* turn off write access, auto increment (also sets to data access) */
372 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, 0);
373 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, 0);
378 /* return 0 if values match, non-zero and complain otherwise */
379 static int invalid_header(struct hfi1_devdata *dd, const char *what,
380 u32 actual, u32 expected)
382 if (actual == expected)
386 "invalid firmware header field %s: expected 0x%x, actual 0x%x\n",
387 what, expected, actual);
392 * Verify that the static fields in the CSS header match.
394 static int verify_css_header(struct hfi1_devdata *dd, struct css_header *css)
396 /* verify CSS header fields (most sizes are in DW, so add /4) */
397 if (invalid_header(dd, "module_type", css->module_type,
399 invalid_header(dd, "header_len", css->header_len,
400 (sizeof(struct firmware_file) / 4)) ||
401 invalid_header(dd, "header_version", css->header_version,
402 CSS_HEADER_VERSION) ||
403 invalid_header(dd, "module_vendor", css->module_vendor,
404 CSS_MODULE_VENDOR) ||
405 invalid_header(dd, "key_size", css->key_size, KEY_SIZE / 4) ||
406 invalid_header(dd, "modulus_size", css->modulus_size,
408 invalid_header(dd, "exponent_size", css->exponent_size,
409 EXPONENT_SIZE / 4)) {
416 * Make sure there are at least some bytes after the prefix.
418 static int payload_check(struct hfi1_devdata *dd, const char *name,
419 long file_size, long prefix_size)
421 /* make sure we have some payload */
422 if (prefix_size >= file_size) {
424 "firmware \"%s\", size %ld, must be larger than %ld bytes\n",
425 name, file_size, prefix_size);
433 * Request the firmware from the system. Extract the pieces and fill in
434 * fdet. If successful, the caller will need to call dispose_one_firmware().
435 * Returns 0 on success, -ERRNO on error.
437 static int obtain_one_firmware(struct hfi1_devdata *dd, const char *name,
438 struct firmware_details *fdet)
440 struct css_header *css;
443 memset(fdet, 0, sizeof(*fdet));
445 ret = reject_firmware(&fdet->fw, name, &dd->pcidev->dev);
447 dd_dev_warn(dd, "cannot find firmware \"%s\", err %d\n",
452 /* verify the firmware */
453 if (fdet->fw->size < sizeof(struct css_header)) {
454 dd_dev_err(dd, "firmware \"%s\" is too small\n", name);
458 css = (struct css_header *)fdet->fw->data;
460 hfi1_cdbg(FIRMWARE, "Firmware %s details:", name);
461 hfi1_cdbg(FIRMWARE, "file size: 0x%lx bytes", fdet->fw->size);
462 hfi1_cdbg(FIRMWARE, "CSS structure:");
463 hfi1_cdbg(FIRMWARE, " module_type 0x%x", css->module_type);
464 hfi1_cdbg(FIRMWARE, " header_len 0x%03x (0x%03x bytes)",
465 css->header_len, 4 * css->header_len);
466 hfi1_cdbg(FIRMWARE, " header_version 0x%x", css->header_version);
467 hfi1_cdbg(FIRMWARE, " module_id 0x%x", css->module_id);
468 hfi1_cdbg(FIRMWARE, " module_vendor 0x%x", css->module_vendor);
469 hfi1_cdbg(FIRMWARE, " date 0x%x", css->date);
470 hfi1_cdbg(FIRMWARE, " size 0x%03x (0x%03x bytes)",
471 css->size, 4 * css->size);
472 hfi1_cdbg(FIRMWARE, " key_size 0x%03x (0x%03x bytes)",
473 css->key_size, 4 * css->key_size);
474 hfi1_cdbg(FIRMWARE, " modulus_size 0x%03x (0x%03x bytes)",
475 css->modulus_size, 4 * css->modulus_size);
476 hfi1_cdbg(FIRMWARE, " exponent_size 0x%03x (0x%03x bytes)",
477 css->exponent_size, 4 * css->exponent_size);
478 hfi1_cdbg(FIRMWARE, "firmware size: 0x%lx bytes",
479 fdet->fw->size - sizeof(struct firmware_file));
482 * If the file does not have a valid CSS header, fail.
483 * Otherwise, check the CSS size field for an expected size.
484 * The augmented file has r2 and mu inserted after the header
485 * was generated, so there will be a known difference between
486 * the CSS header size and the actual file size. Use this
487 * difference to identify an augmented file.
489 * Note: css->size is in DWORDs, multiply by 4 to get bytes.
491 ret = verify_css_header(dd, css);
493 dd_dev_info(dd, "Invalid CSS header for \"%s\"\n", name);
494 } else if ((css->size * 4) == fdet->fw->size) {
495 /* non-augmented firmware file */
496 struct firmware_file *ff = (struct firmware_file *)
499 /* make sure there are bytes in the payload */
500 ret = payload_check(dd, name, fdet->fw->size,
501 sizeof(struct firmware_file));
503 fdet->css_header = css;
504 fdet->modulus = ff->modulus;
505 fdet->exponent = ff->exponent;
506 fdet->signature = ff->signature;
507 fdet->r2 = fdet->dummy_header.r2; /* use dummy space */
508 fdet->mu = fdet->dummy_header.mu; /* use dummy space */
509 fdet->firmware_ptr = ff->firmware;
510 fdet->firmware_len = fdet->fw->size -
511 sizeof(struct firmware_file);
513 * Header does not include r2 and mu - generate here.
516 dd_dev_err(dd, "driver is unable to validate firmware without r2 and mu (not in firmware file)\n");
519 } else if ((css->size * 4) + AUGMENT_SIZE == fdet->fw->size) {
520 /* augmented firmware file */
521 struct augmented_firmware_file *aff =
522 (struct augmented_firmware_file *)fdet->fw->data;
524 /* make sure there are bytes in the payload */
525 ret = payload_check(dd, name, fdet->fw->size,
526 sizeof(struct augmented_firmware_file));
528 fdet->css_header = css;
529 fdet->modulus = aff->modulus;
530 fdet->exponent = aff->exponent;
531 fdet->signature = aff->signature;
534 fdet->firmware_ptr = aff->firmware;
535 fdet->firmware_len = fdet->fw->size -
536 sizeof(struct augmented_firmware_file);
539 /* css->size check failed */
541 "invalid firmware header field size: expected 0x%lx or 0x%lx, actual 0x%x\n",
543 (fdet->fw->size - AUGMENT_SIZE) / 4,
550 /* if returning an error, clean up after ourselves */
552 dispose_one_firmware(fdet);
556 static void dispose_one_firmware(struct firmware_details *fdet)
558 release_firmware(fdet->fw);
559 /* erase all previous information */
560 memset(fdet, 0, sizeof(*fdet));
564 * Obtain the 4 firmwares from the OS. All must be obtained at once or not
565 * at all. If called with the firmware state in FW_TRY, use alternate names.
566 * On exit, this routine will have set the firmware state to one of FW_TRY,
567 * FW_FINAL, or FW_ERR.
569 * Must be holding fw_mutex.
571 static void __obtain_firmware(struct hfi1_devdata *dd)
575 if (fw_state == FW_FINAL) /* nothing more to obtain */
577 if (fw_state == FW_ERR) /* already in error */
580 /* fw_state is FW_EMPTY or FW_TRY */
582 if (fw_state == FW_TRY) {
584 * We tried the original and it failed. Move to the
587 dd_dev_warn(dd, "using alternate firmware names\n");
589 * Let others run. Some systems, when missing firmware, does
590 * something that holds for 30 seconds. If we do that twice
591 * in a row it triggers task blocked warning.
595 dispose_one_firmware(&fw_8051);
596 if (fw_fabric_serdes_load)
597 dispose_one_firmware(&fw_fabric);
599 dispose_one_firmware(&fw_sbus);
600 if (fw_pcie_serdes_load)
601 dispose_one_firmware(&fw_pcie);
602 fw_8051_name = ALT_FW_8051_NAME_ASIC;
603 fw_fabric_serdes_name = ALT_FW_FABRIC_NAME;
604 fw_sbus_name = ALT_FW_SBUS_NAME;
605 fw_pcie_serdes_name = ALT_FW_PCIE_NAME;
609 err = obtain_one_firmware(dd, fw_sbus_name, &fw_sbus);
614 if (fw_pcie_serdes_load) {
615 err = obtain_one_firmware(dd, fw_pcie_serdes_name, &fw_pcie);
620 if (fw_fabric_serdes_load) {
621 err = obtain_one_firmware(dd, fw_fabric_serdes_name,
628 err = obtain_one_firmware(dd, fw_8051_name, &fw_8051);
635 /* oops, had problems obtaining a firmware */
636 if (fw_state == FW_EMPTY && dd->icode == ICODE_RTL_SILICON) {
637 /* retry with alternate (RTL only) */
641 dd_dev_err(dd, "unable to obtain working firmware\n");
646 if (fw_state == FW_EMPTY &&
647 dd->icode != ICODE_FUNCTIONAL_SIMULATOR)
648 fw_state = FW_TRY; /* may retry later */
650 fw_state = FW_FINAL; /* cannot try again */
655 * Called by all HFIs when loading their firmware - i.e. device probe time.
656 * The first one will do the actual firmware load. Use a mutex to resolve
657 * any possible race condition.
659 * The call to this routine cannot be moved to driver load because the kernel
660 * call reject_firmware() requires a device which is only available after
661 * the first device probe.
663 static int obtain_firmware(struct hfi1_devdata *dd)
665 unsigned long timeout;
668 mutex_lock(&fw_mutex);
670 /* 40s delay due to long delay on missing firmware on some systems */
671 timeout = jiffies + msecs_to_jiffies(40000);
672 while (fw_state == FW_TRY) {
674 * Another device is trying the firmware. Wait until it
675 * decides what works (or not).
677 if (time_after(jiffies, timeout)) {
678 /* waited too long */
679 dd_dev_err(dd, "Timeout waiting for firmware try");
684 mutex_unlock(&fw_mutex);
685 msleep(20); /* arbitrary delay */
686 mutex_lock(&fw_mutex);
688 /* not in FW_TRY state */
690 if (fw_state == FW_FINAL) {
691 if (platform_config) {
692 dd->platform_config.data = platform_config->data;
693 dd->platform_config.size = platform_config->size;
695 goto done; /* already acquired */
696 } else if (fw_state == FW_ERR) {
697 goto done; /* already tried and failed */
699 /* fw_state is FW_EMPTY */
701 /* set fw_state to FW_TRY, FW_FINAL, or FW_ERR, and fw_err */
702 __obtain_firmware(dd);
704 if (platform_config_load) {
705 platform_config = NULL;
706 err = reject_firmware(&platform_config, platform_config_name,
709 platform_config = NULL;
712 dd->platform_config.data = platform_config->data;
713 dd->platform_config.size = platform_config->size;
717 mutex_unlock(&fw_mutex);
723 * Called when the driver unloads. The timing is asymmetric with its
724 * counterpart, obtain_firmware(). If called at device remove time,
725 * then it is conceivable that another device could probe while the
726 * firmware is being disposed. The mutexes can be moved to do that
727 * safely, but then the firmware would be requested from the OS multiple
730 * No mutex is needed as the driver is unloading and there cannot be any
733 void dispose_firmware(void)
735 dispose_one_firmware(&fw_8051);
736 dispose_one_firmware(&fw_fabric);
737 dispose_one_firmware(&fw_pcie);
738 dispose_one_firmware(&fw_sbus);
740 release_firmware(platform_config);
741 platform_config = NULL;
743 /* retain the error state, otherwise revert to empty */
744 if (fw_state != FW_ERR)
749 * Called with the result of a firmware download.
751 * Return 1 to retry loading the firmware, 0 to stop.
753 static int retry_firmware(struct hfi1_devdata *dd, int load_result)
757 mutex_lock(&fw_mutex);
759 if (load_result == 0) {
761 * The load succeeded, so expect all others to do the same.
762 * Do not retry again.
764 if (fw_state == FW_TRY)
766 retry = 0; /* do NOT retry */
767 } else if (fw_state == FW_TRY) {
768 /* load failed, obtain alternate firmware */
769 __obtain_firmware(dd);
770 retry = (fw_state == FW_FINAL);
772 /* else in FW_FINAL or FW_ERR, no retry in either case */
776 mutex_unlock(&fw_mutex);
781 * Write a block of data to a given array CSR. All calls will be in
782 * multiples of 8 bytes.
784 static void write_rsa_data(struct hfi1_devdata *dd, int what,
785 const u8 *data, int nbytes)
787 int qw_size = nbytes / 8;
790 if (((unsigned long)data & 0x7) == 0) {
792 u64 *ptr = (u64 *)data;
794 for (i = 0; i < qw_size; i++, ptr++)
795 write_csr(dd, what + (8 * i), *ptr);
798 for (i = 0; i < qw_size; i++, data += 8) {
801 memcpy(&value, data, 8);
802 write_csr(dd, what + (8 * i), value);
808 * Write a block of data to a given CSR as a stream of writes. All calls will
809 * be in multiples of 8 bytes.
811 static void write_streamed_rsa_data(struct hfi1_devdata *dd, int what,
812 const u8 *data, int nbytes)
814 u64 *ptr = (u64 *)data;
815 int qw_size = nbytes / 8;
817 for (; qw_size > 0; qw_size--, ptr++)
818 write_csr(dd, what, *ptr);
822 * Download the signature and start the RSA mechanism. Wait for
823 * RSA_ENGINE_TIMEOUT before giving up.
825 static int run_rsa(struct hfi1_devdata *dd, const char *who,
828 unsigned long timeout;
833 /* write the signature */
834 write_rsa_data(dd, MISC_CFG_RSA_SIGNATURE, signature, KEY_SIZE);
837 write_csr(dd, MISC_CFG_RSA_CMD, RSA_CMD_INIT);
840 * Make sure the engine is idle and insert a delay between the two
841 * writes to MISC_CFG_RSA_CMD.
843 status = (read_csr(dd, MISC_CFG_FW_CTRL)
844 & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK)
845 >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT;
846 if (status != RSA_STATUS_IDLE) {
847 dd_dev_err(dd, "%s security engine not idle - giving up\n",
853 write_csr(dd, MISC_CFG_RSA_CMD, RSA_CMD_START);
856 * Look for the result.
858 * The RSA engine is hooked up to two MISC errors. The driver
859 * masks these errors as they do not respond to the standard
860 * error "clear down" mechanism. Look for these errors here and
861 * clear them when possible. This routine will exit with the
862 * errors of the current run still set.
864 * MISC_FW_AUTH_FAILED_ERR
865 * Firmware authorization failed. This can be cleared by
866 * re-initializing the RSA engine, then clearing the status bit.
867 * Do not re-init the RSA angine immediately after a successful
868 * run - this will reset the current authorization.
870 * MISC_KEY_MISMATCH_ERR
871 * Key does not match. The only way to clear this is to load
872 * a matching key then clear the status bit. If this error
873 * is raised, it will persist outside of this routine until a
874 * matching key is loaded.
876 timeout = msecs_to_jiffies(RSA_ENGINE_TIMEOUT) + jiffies;
878 status = (read_csr(dd, MISC_CFG_FW_CTRL)
879 & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK)
880 >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT;
882 if (status == RSA_STATUS_IDLE) {
883 /* should not happen */
884 dd_dev_err(dd, "%s firmware security bad idle state\n",
888 } else if (status == RSA_STATUS_DONE) {
889 /* finished successfully */
891 } else if (status == RSA_STATUS_FAILED) {
892 /* finished unsuccessfully */
896 /* else still active */
898 if (time_after(jiffies, timeout)) {
900 * Timed out while active. We can't reset the engine
901 * if it is stuck active, but run through the
902 * error code to see what error bits are set.
904 dd_dev_err(dd, "%s firmware security time out\n", who);
913 * Arrive here on success or failure. Clear all RSA engine
914 * errors. All current errors will stick - the RSA logic is keeping
915 * error high. All previous errors will clear - the RSA logic
916 * is not keeping the error high.
918 write_csr(dd, MISC_ERR_CLEAR,
919 MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK |
920 MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK);
922 * All that is left are the current errors. Print warnings on
923 * authorization failure details, if any. Firmware authorization
924 * can be retried, so these are only warnings.
926 reg = read_csr(dd, MISC_ERR_STATUS);
928 if (reg & MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK)
929 dd_dev_warn(dd, "%s firmware authorization failed\n",
931 if (reg & MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK)
932 dd_dev_warn(dd, "%s firmware key mismatch\n", who);
938 static void load_security_variables(struct hfi1_devdata *dd,
939 struct firmware_details *fdet)
941 /* Security variables a. Write the modulus */
942 write_rsa_data(dd, MISC_CFG_RSA_MODULUS, fdet->modulus, KEY_SIZE);
943 /* Security variables b. Write the r2 */
944 write_rsa_data(dd, MISC_CFG_RSA_R2, fdet->r2, KEY_SIZE);
945 /* Security variables c. Write the mu */
946 write_rsa_data(dd, MISC_CFG_RSA_MU, fdet->mu, MU_SIZE);
947 /* Security variables d. Write the header */
948 write_streamed_rsa_data(dd, MISC_CFG_SHA_PRELOAD,
949 (u8 *)fdet->css_header,
950 sizeof(struct css_header));
953 /* return the 8051 firmware state */
954 static inline u32 get_firmware_state(struct hfi1_devdata *dd)
956 u64 reg = read_csr(dd, DC_DC8051_STS_CUR_STATE);
958 return (reg >> DC_DC8051_STS_CUR_STATE_FIRMWARE_SHIFT)
959 & DC_DC8051_STS_CUR_STATE_FIRMWARE_MASK;
963 * Wait until the firmware is up and ready to take host requests.
964 * Return 0 on success, -ETIMEDOUT on timeout.
966 int wait_fm_ready(struct hfi1_devdata *dd, u32 mstimeout)
968 unsigned long timeout;
970 /* in the simulator, the fake 8051 is always ready */
971 if (dd->icode == ICODE_FUNCTIONAL_SIMULATOR)
974 timeout = msecs_to_jiffies(mstimeout) + jiffies;
976 if (get_firmware_state(dd) == 0xa0) /* ready */
978 if (time_after(jiffies, timeout)) /* timed out */
980 usleep_range(1950, 2050); /* sleep 2ms-ish */
985 * Load the 8051 firmware.
987 static int load_8051_firmware(struct hfi1_devdata *dd,
988 struct firmware_details *fdet)
996 * Load DC 8051 firmware
999 * DC reset step 1: Reset DC8051
1001 reg = DC_DC8051_CFG_RST_M8051W_SMASK
1002 | DC_DC8051_CFG_RST_CRAM_SMASK
1003 | DC_DC8051_CFG_RST_DRAM_SMASK
1004 | DC_DC8051_CFG_RST_IRAM_SMASK
1005 | DC_DC8051_CFG_RST_SFR_SMASK;
1006 write_csr(dd, DC_DC8051_CFG_RST, reg);
1009 * DC reset step 2 (optional): Load 8051 data memory with link
1014 * DC reset step 3: Load DC8051 firmware
1016 /* release all but the core reset */
1017 reg = DC_DC8051_CFG_RST_M8051W_SMASK;
1018 write_csr(dd, DC_DC8051_CFG_RST, reg);
1020 /* Firmware load step 1 */
1021 load_security_variables(dd, fdet);
1024 * Firmware load step 2. Clear MISC_CFG_FW_CTRL.FW_8051_LOADED
1026 write_csr(dd, MISC_CFG_FW_CTRL, 0);
1028 /* Firmware load steps 3-5 */
1029 ret = write_8051(dd, 1/*code*/, 0, fdet->firmware_ptr,
1030 fdet->firmware_len);
1035 * DC reset step 4. Host starts the DC8051 firmware
1038 * Firmware load step 6. Set MISC_CFG_FW_CTRL.FW_8051_LOADED
1040 write_csr(dd, MISC_CFG_FW_CTRL, MISC_CFG_FW_CTRL_FW_8051_LOADED_SMASK);
1042 /* Firmware load steps 7-10 */
1043 ret = run_rsa(dd, "8051", fdet->signature);
1047 /* clear all reset bits, releasing the 8051 */
1048 write_csr(dd, DC_DC8051_CFG_RST, 0ull);
1051 * DC reset step 5. Wait for firmware to be ready to accept host
1054 ret = wait_fm_ready(dd, TIMEOUT_8051_START);
1055 if (ret) { /* timed out */
1056 dd_dev_err(dd, "8051 start timeout, current state 0x%x\n",
1057 get_firmware_state(dd));
1061 read_misc_status(dd, &ver_a, &ver_b);
1062 dd_dev_info(dd, "8051 firmware version %d.%d\n",
1063 (int)ver_b, (int)ver_a);
1064 dd->dc8051_ver = dc8051_ver(ver_b, ver_a);
1070 * Write the SBus request register
1072 * No need for masking - the arguments are sized exactly.
1074 void sbus_request(struct hfi1_devdata *dd,
1075 u8 receiver_addr, u8 data_addr, u8 command, u32 data_in)
1077 write_csr(dd, ASIC_CFG_SBUS_REQUEST,
1078 ((u64)data_in << ASIC_CFG_SBUS_REQUEST_DATA_IN_SHIFT) |
1079 ((u64)command << ASIC_CFG_SBUS_REQUEST_COMMAND_SHIFT) |
1080 ((u64)data_addr << ASIC_CFG_SBUS_REQUEST_DATA_ADDR_SHIFT) |
1081 ((u64)receiver_addr <<
1082 ASIC_CFG_SBUS_REQUEST_RECEIVER_ADDR_SHIFT));
1086 * Read a value from the SBus.
1088 * Requires the caller to be in fast mode
1090 static u32 sbus_read(struct hfi1_devdata *dd, u8 receiver_addr, u8 data_addr,
1097 u32 result_code = 0;
1099 sbus_request(dd, receiver_addr, data_addr, READ_SBUS_RECEIVER, data_in);
1101 for (retries = 0; retries < 100; retries++) {
1102 usleep_range(1000, 1200); /* arbitrary */
1103 reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
1104 result_code = (reg >> ASIC_STS_SBUS_RESULT_RESULT_CODE_SHIFT)
1105 & ASIC_STS_SBUS_RESULT_RESULT_CODE_MASK;
1106 if (result_code != SBUS_READ_COMPLETE)
1110 result = (reg >> ASIC_STS_SBUS_RESULT_DATA_OUT_SHIFT)
1111 & ASIC_STS_SBUS_RESULT_DATA_OUT_MASK;
1116 dd_dev_err(dd, "%s: read failed, result code 0x%x\n", __func__,
1124 * Turn off the SBus and fabric serdes spicos.
1126 * + Must be called with Sbus fast mode turned on.
1127 * + Must be called after fabric serdes broadcast is set up.
1128 * + Must be called before the 8051 is loaded - assumes 8051 is not loaded
1129 * when using MISC_CFG_FW_CTRL.
1131 static void turn_off_spicos(struct hfi1_devdata *dd, int flags)
1133 /* only needed on A0 */
1137 dd_dev_info(dd, "Turning off spicos:%s%s\n",
1138 flags & SPICO_SBUS ? " SBus" : "",
1139 flags & SPICO_FABRIC ? " fabric" : "");
1141 write_csr(dd, MISC_CFG_FW_CTRL, ENABLE_SPICO_SMASK);
1142 /* disable SBus spico */
1143 if (flags & SPICO_SBUS)
1144 sbus_request(dd, SBUS_MASTER_BROADCAST, 0x01,
1145 WRITE_SBUS_RECEIVER, 0x00000040);
1147 /* disable the fabric serdes spicos */
1148 if (flags & SPICO_FABRIC)
1149 sbus_request(dd, fabric_serdes_broadcast[dd->hfi1_id],
1150 0x07, WRITE_SBUS_RECEIVER, 0x00000000);
1151 write_csr(dd, MISC_CFG_FW_CTRL, 0);
1155 * Reset all of the fabric serdes for this HFI in preparation to take the
1158 * To do a reset, we need to write to to the serdes registers. Unfortunately,
1159 * the fabric serdes download to the other HFI on the ASIC will have turned
1160 * off the firmware validation on this HFI. This means we can't write to the
1161 * registers to reset the serdes. Work around this by performing a complete
1162 * re-download and validation of the fabric serdes firmware. This, as a
1163 * by-product, will reset the serdes. NOTE: the re-download requires that
1164 * the 8051 be in the Offline state. I.e. not actively trying to use the
1165 * serdes. This routine is called at the point where the link is Offline and
1166 * is getting ready to go to Polling.
1168 void fabric_serdes_reset(struct hfi1_devdata *dd)
1172 if (!fw_fabric_serdes_load)
1175 ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
1178 "Cannot acquire SBus resource to reset fabric SerDes - perhaps you should reboot\n");
1181 set_sbus_fast_mode(dd);
1184 /* A0 serdes do not work with a re-download */
1185 u8 ra = fabric_serdes_broadcast[dd->hfi1_id];
1187 /* place SerDes in reset and disable SPICO */
1188 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000011);
1189 /* wait 100 refclk cycles @ 156.25MHz => 640ns */
1191 /* remove SerDes reset */
1192 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000010);
1193 /* turn SPICO enable on */
1194 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000002);
1196 turn_off_spicos(dd, SPICO_FABRIC);
1198 * No need for firmware retry - what to download has already
1200 * No need to pay attention to the load return - the only
1201 * failure is a validation failure, which has already been
1202 * checked by the initial download.
1204 (void)load_fabric_serdes_firmware(dd, &fw_fabric);
1207 clear_sbus_fast_mode(dd);
1208 release_chip_resource(dd, CR_SBUS);
1211 /* Access to the SBus in this routine should probably be serialized */
1212 int sbus_request_slow(struct hfi1_devdata *dd,
1213 u8 receiver_addr, u8 data_addr, u8 command, u32 data_in)
1217 /* make sure fast mode is clear */
1218 clear_sbus_fast_mode(dd);
1220 sbus_request(dd, receiver_addr, data_addr, command, data_in);
1221 write_csr(dd, ASIC_CFG_SBUS_EXECUTE,
1222 ASIC_CFG_SBUS_EXECUTE_EXECUTE_SMASK);
1223 /* Wait for both DONE and RCV_DATA_VALID to go high */
1224 reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
1225 while (!((reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) &&
1226 (reg & ASIC_STS_SBUS_RESULT_RCV_DATA_VALID_SMASK))) {
1227 if (count++ >= SBUS_MAX_POLL_COUNT) {
1228 u64 counts = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
1230 * If the loop has timed out, we are OK if DONE bit
1231 * is set and RCV_DATA_VALID and EXECUTE counters
1232 * are the same. If not, we cannot proceed.
1234 if ((reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) &&
1235 (SBUS_COUNTER(counts, RCV_DATA_VALID) ==
1236 SBUS_COUNTER(counts, EXECUTE)))
1241 reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
1244 write_csr(dd, ASIC_CFG_SBUS_EXECUTE, 0);
1245 /* Wait for DONE to clear after EXECUTE is cleared */
1246 reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
1247 while (reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) {
1248 if (count++ >= SBUS_MAX_POLL_COUNT)
1251 reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
1256 static int load_fabric_serdes_firmware(struct hfi1_devdata *dd,
1257 struct firmware_details *fdet)
1260 const u8 ra = fabric_serdes_broadcast[dd->hfi1_id]; /* receiver addr */
1262 dd_dev_info(dd, "Downloading fabric firmware\n");
1264 /* step 1: load security variables */
1265 load_security_variables(dd, fdet);
1266 /* step 2: place SerDes in reset and disable SPICO */
1267 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000011);
1268 /* wait 100 refclk cycles @ 156.25MHz => 640ns */
1270 /* step 3: remove SerDes reset */
1271 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000010);
1272 /* step 4: assert IMEM override */
1273 sbus_request(dd, ra, 0x00, WRITE_SBUS_RECEIVER, 0x40000000);
1274 /* step 5: download SerDes machine code */
1275 for (i = 0; i < fdet->firmware_len; i += 4) {
1276 sbus_request(dd, ra, 0x0a, WRITE_SBUS_RECEIVER,
1277 *(u32 *)&fdet->firmware_ptr[i]);
1279 /* step 6: IMEM override off */
1280 sbus_request(dd, ra, 0x00, WRITE_SBUS_RECEIVER, 0x00000000);
1281 /* step 7: turn ECC on */
1282 sbus_request(dd, ra, 0x0b, WRITE_SBUS_RECEIVER, 0x000c0000);
1284 /* steps 8-11: run the RSA engine */
1285 err = run_rsa(dd, "fabric serdes", fdet->signature);
1289 /* step 12: turn SPICO enable on */
1290 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000002);
1291 /* step 13: enable core hardware interrupts */
1292 sbus_request(dd, ra, 0x08, WRITE_SBUS_RECEIVER, 0x00000000);
1297 static int load_sbus_firmware(struct hfi1_devdata *dd,
1298 struct firmware_details *fdet)
1301 const u8 ra = SBUS_MASTER_BROADCAST; /* receiver address */
1303 dd_dev_info(dd, "Downloading SBus firmware\n");
1305 /* step 1: load security variables */
1306 load_security_variables(dd, fdet);
1307 /* step 2: place SPICO into reset and enable off */
1308 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x000000c0);
1309 /* step 3: remove reset, enable off, IMEM_CNTRL_EN on */
1310 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000240);
1311 /* step 4: set starting IMEM address for burst download */
1312 sbus_request(dd, ra, 0x03, WRITE_SBUS_RECEIVER, 0x80000000);
1313 /* step 5: download the SBus Master machine code */
1314 for (i = 0; i < fdet->firmware_len; i += 4) {
1315 sbus_request(dd, ra, 0x14, WRITE_SBUS_RECEIVER,
1316 *(u32 *)&fdet->firmware_ptr[i]);
1318 /* step 6: set IMEM_CNTL_EN off */
1319 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000040);
1320 /* step 7: turn ECC on */
1321 sbus_request(dd, ra, 0x16, WRITE_SBUS_RECEIVER, 0x000c0000);
1323 /* steps 8-11: run the RSA engine */
1324 err = run_rsa(dd, "SBus", fdet->signature);
1328 /* step 12: set SPICO_ENABLE on */
1329 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000140);
1334 static int load_pcie_serdes_firmware(struct hfi1_devdata *dd,
1335 struct firmware_details *fdet)
1338 const u8 ra = SBUS_MASTER_BROADCAST; /* receiver address */
1340 dd_dev_info(dd, "Downloading PCIe firmware\n");
1342 /* step 1: load security variables */
1343 load_security_variables(dd, fdet);
1344 /* step 2: assert single step (halts the SBus Master spico) */
1345 sbus_request(dd, ra, 0x05, WRITE_SBUS_RECEIVER, 0x00000001);
1346 /* step 3: enable XDMEM access */
1347 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000d40);
1348 /* step 4: load firmware into SBus Master XDMEM */
1350 * NOTE: the dmem address, write_en, and wdata are all pre-packed,
1351 * we only need to pick up the bytes and write them
1353 for (i = 0; i < fdet->firmware_len; i += 4) {
1354 sbus_request(dd, ra, 0x04, WRITE_SBUS_RECEIVER,
1355 *(u32 *)&fdet->firmware_ptr[i]);
1357 /* step 5: disable XDMEM access */
1358 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000140);
1359 /* step 6: allow SBus Spico to run */
1360 sbus_request(dd, ra, 0x05, WRITE_SBUS_RECEIVER, 0x00000000);
1363 * steps 7-11: run RSA, if it succeeds, firmware is available to
1366 return run_rsa(dd, "PCIe serdes", fdet->signature);
1370 * Set the given broadcast values on the given list of devices.
1372 static void set_serdes_broadcast(struct hfi1_devdata *dd, u8 bg1, u8 bg2,
1373 const u8 *addrs, int count)
1375 while (--count >= 0) {
1377 * Set BROADCAST_GROUP_1 and BROADCAST_GROUP_2, leave
1378 * defaults for everything else. Do not read-modify-write,
1379 * per instruction from the manufacturer.
1383 * ----- ---------------------------------
1384 * 0 IGNORE_BROADCAST (default 0)
1385 * 11:4 BROADCAST_GROUP_1 (default 0xff)
1386 * 23:16 BROADCAST_GROUP_2 (default 0xff)
1388 sbus_request(dd, addrs[count], 0xfd, WRITE_SBUS_RECEIVER,
1389 (u32)bg1 << 4 | (u32)bg2 << 16);
1393 int acquire_hw_mutex(struct hfi1_devdata *dd)
1395 unsigned long timeout;
1397 u8 mask = 1 << dd->hfi1_id;
1401 timeout = msecs_to_jiffies(HM_TIMEOUT) + jiffies;
1403 write_csr(dd, ASIC_CFG_MUTEX, mask);
1404 user = (u8)read_csr(dd, ASIC_CFG_MUTEX);
1406 return 0; /* success */
1407 if (time_after(jiffies, timeout))
1408 break; /* timed out */
1414 "Unable to acquire hardware mutex, mutex mask %u, my mask %u (%s)\n",
1415 (u32)user, (u32)mask, (try == 0) ? "retrying" : "giving up");
1418 /* break mutex and retry */
1419 write_csr(dd, ASIC_CFG_MUTEX, 0);
1427 void release_hw_mutex(struct hfi1_devdata *dd)
1429 write_csr(dd, ASIC_CFG_MUTEX, 0);
1432 /* return the given resource bit(s) as a mask for the given HFI */
1433 static inline u64 resource_mask(u32 hfi1_id, u32 resource)
1435 return ((u64)resource) << (hfi1_id ? CR_DYN_SHIFT : 0);
1438 static void fail_mutex_acquire_message(struct hfi1_devdata *dd,
1442 "%s: hardware mutex stuck - suggest rebooting the machine\n",
1447 * Acquire access to a chip resource.
1449 * Return 0 on success, -EBUSY if resource busy, -EIO if mutex acquire failed.
1451 static int __acquire_chip_resource(struct hfi1_devdata *dd, u32 resource)
1453 u64 scratch0, all_bits, my_bit;
1456 if (resource & CR_DYN_MASK) {
1457 /* a dynamic resource is in use if either HFI has set the bit */
1458 if (dd->pcidev->device == PCI_DEVICE_ID_INTEL0 &&
1459 (resource & (CR_I2C1 | CR_I2C2))) {
1460 /* discrete devices must serialize across both chains */
1461 all_bits = resource_mask(0, CR_I2C1 | CR_I2C2) |
1462 resource_mask(1, CR_I2C1 | CR_I2C2);
1464 all_bits = resource_mask(0, resource) |
1465 resource_mask(1, resource);
1467 my_bit = resource_mask(dd->hfi1_id, resource);
1469 /* non-dynamic resources are not split between HFIs */
1470 all_bits = resource;
1474 /* lock against other callers within the driver wanting a resource */
1475 mutex_lock(&dd->asic_data->asic_resource_mutex);
1477 ret = acquire_hw_mutex(dd);
1479 fail_mutex_acquire_message(dd, __func__);
1484 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
1485 if (scratch0 & all_bits) {
1488 write_csr(dd, ASIC_CFG_SCRATCH, scratch0 | my_bit);
1489 /* force write to be visible to other HFI on another OS */
1490 (void)read_csr(dd, ASIC_CFG_SCRATCH);
1493 release_hw_mutex(dd);
1496 mutex_unlock(&dd->asic_data->asic_resource_mutex);
1501 * Acquire access to a chip resource, wait up to mswait milliseconds for
1502 * the resource to become available.
1504 * Return 0 on success, -EBUSY if busy (even after wait), -EIO if mutex
1507 int acquire_chip_resource(struct hfi1_devdata *dd, u32 resource, u32 mswait)
1509 unsigned long timeout;
1512 timeout = jiffies + msecs_to_jiffies(mswait);
1514 ret = __acquire_chip_resource(dd, resource);
1517 /* resource is busy, check our timeout */
1518 if (time_after_eq(jiffies, timeout))
1520 usleep_range(80, 120); /* arbitrary delay */
1525 * Release access to a chip resource
1527 void release_chip_resource(struct hfi1_devdata *dd, u32 resource)
1531 /* only dynamic resources should ever be cleared */
1532 if (!(resource & CR_DYN_MASK)) {
1533 dd_dev_err(dd, "%s: invalid resource 0x%x\n", __func__,
1537 bit = resource_mask(dd->hfi1_id, resource);
1539 /* lock against other callers within the driver wanting a resource */
1540 mutex_lock(&dd->asic_data->asic_resource_mutex);
1542 if (acquire_hw_mutex(dd)) {
1543 fail_mutex_acquire_message(dd, __func__);
1547 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
1548 if ((scratch0 & bit) != 0) {
1550 write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
1551 /* force write to be visible to other HFI on another OS */
1552 (void)read_csr(dd, ASIC_CFG_SCRATCH);
1554 dd_dev_warn(dd, "%s: id %d, resource 0x%x: bit not set\n",
1555 __func__, dd->hfi1_id, resource);
1558 release_hw_mutex(dd);
1561 mutex_unlock(&dd->asic_data->asic_resource_mutex);
1565 * Return true if resource is set, false otherwise. Print a warning
1566 * if not set and a function is supplied.
1568 bool check_chip_resource(struct hfi1_devdata *dd, u32 resource,
1573 if (resource & CR_DYN_MASK)
1574 bit = resource_mask(dd->hfi1_id, resource);
1578 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
1579 if ((scratch0 & bit) == 0) {
1582 "%s: id %d, resource 0x%x, not acquired!\n",
1583 func, dd->hfi1_id, resource);
1589 static void clear_chip_resources(struct hfi1_devdata *dd, const char *func)
1593 /* lock against other callers within the driver wanting a resource */
1594 mutex_lock(&dd->asic_data->asic_resource_mutex);
1596 if (acquire_hw_mutex(dd)) {
1597 fail_mutex_acquire_message(dd, func);
1601 /* clear all dynamic access bits for this HFI */
1602 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
1603 scratch0 &= ~resource_mask(dd->hfi1_id, CR_DYN_MASK);
1604 write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
1605 /* force write to be visible to other HFI on another OS */
1606 (void)read_csr(dd, ASIC_CFG_SCRATCH);
1608 release_hw_mutex(dd);
1611 mutex_unlock(&dd->asic_data->asic_resource_mutex);
1614 void init_chip_resources(struct hfi1_devdata *dd)
1616 /* clear any holds left by us */
1617 clear_chip_resources(dd, __func__);
1620 void finish_chip_resources(struct hfi1_devdata *dd)
1622 /* clear any holds left by us */
1623 clear_chip_resources(dd, __func__);
1626 void set_sbus_fast_mode(struct hfi1_devdata *dd)
1628 write_csr(dd, ASIC_CFG_SBUS_EXECUTE,
1629 ASIC_CFG_SBUS_EXECUTE_FAST_MODE_SMASK);
1632 void clear_sbus_fast_mode(struct hfi1_devdata *dd)
1636 reg = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
1637 while (SBUS_COUNTER(reg, EXECUTE) !=
1638 SBUS_COUNTER(reg, RCV_DATA_VALID)) {
1639 if (count++ >= SBUS_MAX_POLL_COUNT)
1642 reg = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
1644 write_csr(dd, ASIC_CFG_SBUS_EXECUTE, 0);
1647 int load_firmware(struct hfi1_devdata *dd)
1651 if (fw_fabric_serdes_load) {
1652 ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
1656 set_sbus_fast_mode(dd);
1658 set_serdes_broadcast(dd, all_fabric_serdes_broadcast,
1659 fabric_serdes_broadcast[dd->hfi1_id],
1660 fabric_serdes_addrs[dd->hfi1_id],
1662 turn_off_spicos(dd, SPICO_FABRIC);
1664 ret = load_fabric_serdes_firmware(dd, &fw_fabric);
1665 } while (retry_firmware(dd, ret));
1667 clear_sbus_fast_mode(dd);
1668 release_chip_resource(dd, CR_SBUS);
1675 ret = load_8051_firmware(dd, &fw_8051);
1676 } while (retry_firmware(dd, ret));
1681 dump_fw_version(dd);
1685 int hfi1_firmware_init(struct hfi1_devdata *dd)
1687 /* only RTL can use these */
1688 if (dd->icode != ICODE_RTL_SILICON) {
1689 fw_fabric_serdes_load = 0;
1690 fw_pcie_serdes_load = 0;
1694 /* no 8051 or QSFP on simulator */
1695 if (dd->icode == ICODE_FUNCTIONAL_SIMULATOR) {
1697 platform_config_load = 0;
1700 if (!fw_8051_name) {
1701 if (dd->icode == ICODE_RTL_SILICON)
1702 fw_8051_name = DEFAULT_FW_8051_NAME_ASIC;
1704 fw_8051_name = DEFAULT_FW_8051_NAME_FPGA;
1706 if (!fw_fabric_serdes_name)
1707 fw_fabric_serdes_name = DEFAULT_FW_FABRIC_NAME;
1709 fw_sbus_name = DEFAULT_FW_SBUS_NAME;
1710 if (!fw_pcie_serdes_name)
1711 fw_pcie_serdes_name = DEFAULT_FW_PCIE_NAME;
1712 if (!platform_config_name)
1713 platform_config_name = DEFAULT_PLATFORM_CONFIG_NAME;
1715 return obtain_firmware(dd);
1719 * This function is a helper function for parse_platform_config(...) and
1720 * does not check for validity of the platform configuration cache
1721 * (because we know it is invalid as we are building up the cache).
1722 * As such, this should not be called from anywhere other than
1723 * parse_platform_config
1725 static int check_meta_version(struct hfi1_devdata *dd, u32 *system_table)
1727 u32 meta_ver, meta_ver_meta, ver_start, ver_len, mask;
1728 struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
1734 *(pcfgcache->config_tables[PLATFORM_CONFIG_SYSTEM_TABLE].table_metadata
1735 + SYSTEM_TABLE_META_VERSION);
1737 mask = ((1 << METADATA_TABLE_FIELD_START_LEN_BITS) - 1);
1738 ver_start = meta_ver_meta & mask;
1740 meta_ver_meta >>= METADATA_TABLE_FIELD_LEN_SHIFT;
1742 mask = ((1 << METADATA_TABLE_FIELD_LEN_LEN_BITS) - 1);
1743 ver_len = meta_ver_meta & mask;
1746 meta_ver = *((u8 *)system_table + ver_start) & ((1 << ver_len) - 1);
1750 dd, "%s:Please update platform config\n", __func__);
1756 int parse_platform_config(struct hfi1_devdata *dd)
1758 struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
1760 u32 header1 = 0, header2 = 0, magic_num = 0, crc = 0, file_length = 0;
1761 u32 record_idx = 0, table_type = 0, table_length_dwords = 0;
1762 int ret = -EINVAL; /* assume failure */
1764 if (!dd->platform_config.data) {
1765 dd_dev_info(dd, "%s: Missing config file\n", __func__);
1768 ptr = (u32 *)dd->platform_config.data;
1772 if (magic_num != PLATFORM_CONFIG_MAGIC_NUM) {
1773 dd_dev_info(dd, "%s: Bad config file\n", __func__);
1777 /* Field is file size in DWORDs */
1778 file_length = (*ptr) * 4;
1781 if (file_length > dd->platform_config.size) {
1782 dd_dev_info(dd, "%s:File claims to be larger than read size\n",
1785 } else if (file_length < dd->platform_config.size) {
1787 "%s:File claims to be smaller than read size, continuing\n",
1790 /* exactly equal, perfection */
1793 * In both cases where we proceed, using the self-reported file length
1794 * is the safer option
1796 while (ptr < (u32 *)(dd->platform_config.data + file_length)) {
1798 header2 = *(ptr + 1);
1799 if (header1 != ~header2) {
1800 dd_dev_info(dd, "%s: Failed validation at offset %ld\n",
1801 __func__, (ptr - (u32 *)
1802 dd->platform_config.data));
1807 ((1 << PLATFORM_CONFIG_HEADER_RECORD_IDX_LEN_BITS) - 1);
1809 table_length_dwords = (*ptr >>
1810 PLATFORM_CONFIG_HEADER_TABLE_LENGTH_SHIFT) &
1811 ((1 << PLATFORM_CONFIG_HEADER_TABLE_LENGTH_LEN_BITS) - 1);
1813 table_type = (*ptr >> PLATFORM_CONFIG_HEADER_TABLE_TYPE_SHIFT) &
1814 ((1 << PLATFORM_CONFIG_HEADER_TABLE_TYPE_LEN_BITS) - 1);
1816 /* Done with this set of headers */
1821 switch (table_type) {
1822 case PLATFORM_CONFIG_SYSTEM_TABLE:
1823 pcfgcache->config_tables[table_type].num_table =
1825 ret = check_meta_version(dd, ptr);
1829 case PLATFORM_CONFIG_PORT_TABLE:
1830 pcfgcache->config_tables[table_type].num_table =
1833 case PLATFORM_CONFIG_RX_PRESET_TABLE:
1835 case PLATFORM_CONFIG_TX_PRESET_TABLE:
1837 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
1839 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
1840 pcfgcache->config_tables[table_type].num_table =
1841 table_length_dwords;
1845 "%s: Unknown data table %d, offset %ld\n",
1846 __func__, table_type,
1848 dd->platform_config.data));
1849 goto bail; /* We don't trust this file now */
1851 pcfgcache->config_tables[table_type].table = ptr;
1853 /* metadata table */
1854 switch (table_type) {
1855 case PLATFORM_CONFIG_SYSTEM_TABLE:
1857 case PLATFORM_CONFIG_PORT_TABLE:
1859 case PLATFORM_CONFIG_RX_PRESET_TABLE:
1861 case PLATFORM_CONFIG_TX_PRESET_TABLE:
1863 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
1865 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
1869 "%s: Unknown meta table %d, offset %ld\n",
1870 __func__, table_type,
1872 (u32 *)dd->platform_config.data));
1873 goto bail; /* We don't trust this file now */
1875 pcfgcache->config_tables[table_type].table_metadata =
1879 /* Calculate and check table crc */
1880 crc = crc32_le(~(u32)0, (unsigned char const *)ptr,
1881 (table_length_dwords * 4));
1884 /* Jump the table */
1885 ptr += table_length_dwords;
1887 dd_dev_info(dd, "%s: Failed CRC check at offset %ld\n",
1890 dd->platform_config.data));
1893 /* Jump the CRC DWORD */
1897 pcfgcache->cache_valid = 1;
1900 memset(pcfgcache, 0, sizeof(struct platform_config_cache));
1904 static int get_platform_fw_field_metadata(struct hfi1_devdata *dd, int table,
1905 int field, u32 *field_len_bits,
1906 u32 *field_start_bits)
1908 struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
1909 u32 *src_ptr = NULL;
1911 if (!pcfgcache->cache_valid)
1915 case PLATFORM_CONFIG_SYSTEM_TABLE:
1917 case PLATFORM_CONFIG_PORT_TABLE:
1919 case PLATFORM_CONFIG_RX_PRESET_TABLE:
1921 case PLATFORM_CONFIG_TX_PRESET_TABLE:
1923 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
1925 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
1926 if (field && field < platform_config_table_limits[table])
1928 pcfgcache->config_tables[table].table_metadata + field;
1931 dd_dev_info(dd, "%s: Unknown table\n", __func__);
1938 if (field_start_bits)
1939 *field_start_bits = *src_ptr &
1940 ((1 << METADATA_TABLE_FIELD_START_LEN_BITS) - 1);
1943 *field_len_bits = (*src_ptr >> METADATA_TABLE_FIELD_LEN_SHIFT)
1944 & ((1 << METADATA_TABLE_FIELD_LEN_LEN_BITS) - 1);
1949 /* This is the central interface to getting data out of the platform config
1950 * file. It depends on parse_platform_config() having populated the
1951 * platform_config_cache in hfi1_devdata, and checks the cache_valid member to
1952 * validate the sanity of the cache.
1954 * The non-obvious parameters:
1955 * @table_index: Acts as a look up key into which instance of the tables the
1956 * relevant field is fetched from.
1958 * This applies to the data tables that have multiple instances. The port table
1959 * is an exception to this rule as each HFI only has one port and thus the
1960 * relevant table can be distinguished by hfi_id.
1962 * @data: pointer to memory that will be populated with the field requested.
1963 * @len: length of memory pointed by @data in bytes.
1965 int get_platform_config_field(struct hfi1_devdata *dd,
1966 enum platform_config_table_type_encoding
1967 table_type, int table_index, int field_index,
1970 int ret = 0, wlen = 0, seek = 0;
1971 u32 field_len_bits = 0, field_start_bits = 0, *src_ptr = NULL;
1972 struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
1975 memset(data, 0, len);
1979 ret = get_platform_fw_field_metadata(dd, table_type, field_index,
1985 /* Convert length to bits */
1988 /* Our metadata function checked cache_valid and field_index for us */
1989 switch (table_type) {
1990 case PLATFORM_CONFIG_SYSTEM_TABLE:
1991 src_ptr = pcfgcache->config_tables[table_type].table;
1993 if (field_index != SYSTEM_TABLE_QSFP_POWER_CLASS_MAX) {
1994 if (len < field_len_bits)
1997 seek = field_start_bits / 8;
1998 wlen = field_len_bits / 8;
2000 src_ptr = (u32 *)((u8 *)src_ptr + seek);
2003 * We expect the field to be byte aligned and whole byte
2004 * lengths if we are here
2006 memcpy(data, src_ptr, wlen);
2010 case PLATFORM_CONFIG_PORT_TABLE:
2011 /* Port table is 4 DWORDS */
2012 src_ptr = dd->hfi1_id ?
2013 pcfgcache->config_tables[table_type].table + 4 :
2014 pcfgcache->config_tables[table_type].table;
2016 case PLATFORM_CONFIG_RX_PRESET_TABLE:
2018 case PLATFORM_CONFIG_TX_PRESET_TABLE:
2020 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
2022 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
2023 src_ptr = pcfgcache->config_tables[table_type].table;
2026 pcfgcache->config_tables[table_type].num_table)
2027 src_ptr += table_index;
2032 dd_dev_info(dd, "%s: Unknown table\n", __func__);
2036 if (!src_ptr || len < field_len_bits)
2039 src_ptr += (field_start_bits / 32);
2040 *data = (*src_ptr >> (field_start_bits % 32)) &
2041 ((1 << field_len_bits) - 1);
2047 * Download the firmware needed for the Gen3 PCIe SerDes. An update
2048 * to the SBus firmware is needed before updating the PCIe firmware.
2050 * Note: caller must be holding the SBus resource.
2052 int load_pcie_firmware(struct hfi1_devdata *dd)
2056 /* both firmware loads below use the SBus */
2057 set_sbus_fast_mode(dd);
2060 turn_off_spicos(dd, SPICO_SBUS);
2062 ret = load_sbus_firmware(dd, &fw_sbus);
2063 } while (retry_firmware(dd, ret));
2068 if (fw_pcie_serdes_load) {
2069 dd_dev_info(dd, "Setting PCIe SerDes broadcast\n");
2070 set_serdes_broadcast(dd, all_pcie_serdes_broadcast,
2071 pcie_serdes_broadcast[dd->hfi1_id],
2072 pcie_serdes_addrs[dd->hfi1_id],
2075 ret = load_pcie_serdes_firmware(dd, &fw_pcie);
2076 } while (retry_firmware(dd, ret));
2082 clear_sbus_fast_mode(dd);
2088 * Read the GUID from the hardware, store it in dd.
2090 void read_guid(struct hfi1_devdata *dd)
2092 /* Take the DC out of reset to get a valid GUID value */
2093 write_csr(dd, CCE_DC_CTRL, 0);
2094 (void)read_csr(dd, CCE_DC_CTRL);
2096 dd->base_guid = read_csr(dd, DC_DC8051_CFG_LOCAL_GUID);
2097 dd_dev_info(dd, "GUID %llx",
2098 (unsigned long long)dd->base_guid);
2101 /* read and display firmware version info */
2102 static void dump_fw_version(struct hfi1_devdata *dd)
2104 u32 pcie_vers[NUM_PCIE_SERDES];
2105 u32 fabric_vers[NUM_FABRIC_SERDES];
2112 ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
2114 dd_dev_err(dd, "Unable to acquire SBus to read firmware versions\n");
2119 set_sbus_fast_mode(dd);
2121 /* read version for SBus Master */
2122 sbus_request(dd, SBUS_MASTER_BROADCAST, 0x02, WRITE_SBUS_RECEIVER, 0);
2123 sbus_request(dd, SBUS_MASTER_BROADCAST, 0x07, WRITE_SBUS_RECEIVER, 0x1);
2124 /* wait for interrupt to be processed */
2125 usleep_range(10000, 11000);
2126 sbus_vers = sbus_read(dd, SBUS_MASTER_BROADCAST, 0x08, 0x1);
2127 dd_dev_info(dd, "SBus Master firmware version 0x%08x\n", sbus_vers);
2129 /* read version for PCIe SerDes */
2132 for (i = 0; i < NUM_PCIE_SERDES; i++) {
2133 rcv_addr = pcie_serdes_addrs[dd->hfi1_id][i];
2134 sbus_request(dd, rcv_addr, 0x03, WRITE_SBUS_RECEIVER, 0);
2135 /* wait for interrupt to be processed */
2136 usleep_range(10000, 11000);
2137 pcie_vers[i] = sbus_read(dd, rcv_addr, 0x04, 0x0);
2138 if (i > 0 && pcie_vers[0] != pcie_vers[i])
2143 dd_dev_info(dd, "PCIe SerDes firmware version 0x%x\n",
2146 dd_dev_warn(dd, "PCIe SerDes do not have the same firmware version\n");
2147 for (i = 0; i < NUM_PCIE_SERDES; i++) {
2149 "PCIe SerDes lane %d firmware version 0x%x\n",
2154 /* read version for fabric SerDes */
2157 for (i = 0; i < NUM_FABRIC_SERDES; i++) {
2158 rcv_addr = fabric_serdes_addrs[dd->hfi1_id][i];
2159 sbus_request(dd, rcv_addr, 0x03, WRITE_SBUS_RECEIVER, 0);
2160 /* wait for interrupt to be processed */
2161 usleep_range(10000, 11000);
2162 fabric_vers[i] = sbus_read(dd, rcv_addr, 0x04, 0x0);
2163 if (i > 0 && fabric_vers[0] != fabric_vers[i])
2168 dd_dev_info(dd, "Fabric SerDes firmware version 0x%x\n",
2171 dd_dev_warn(dd, "Fabric SerDes do not have the same firmware version\n");
2172 for (i = 0; i < NUM_FABRIC_SERDES; i++) {
2174 "Fabric SerDes lane %d firmware version 0x%x\n",
2179 clear_sbus_fast_mode(dd);
2180 release_chip_resource(dd, CR_SBUS);