blob: bcf7da4599c48824463ed0430047e34ca107bc6e [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Jubin John05d6ac12016-02-14 20:22:17 -08002 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
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.
6 *
7 * GPL LICENSE SUMMARY
8 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * 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.
12 *
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.
17 *
18 * BSD LICENSE
19 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
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
29 * distribution.
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.
33 *
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.
45 *
46 */
47
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>
53
54#include "hfi.h"
55#include "trace.h"
56
57/*
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.
61 */
62#define DEFAULT_FW_8051_NAME_FPGA "hfi_dc8051.bin"
63#define DEFAULT_FW_8051_NAME_ASIC "hfi1_dc8051.fw"
64#define DEFAULT_FW_FABRIC_NAME "hfi1_fabric.fw"
65#define DEFAULT_FW_SBUS_NAME "hfi1_sbus.fw"
66#define DEFAULT_FW_PCIE_NAME "hfi1_pcie.fw"
67#define DEFAULT_PLATFORM_CONFIG_NAME "hfi1_platform.dat"
Dean Luickb3de8422015-12-01 15:38:10 -050068#define ALT_FW_8051_NAME_ASIC "hfi1_dc8051_d.fw"
69#define ALT_FW_FABRIC_NAME "hfi1_fabric_d.fw"
70#define ALT_FW_SBUS_NAME "hfi1_sbus_d.fw"
71#define ALT_FW_PCIE_NAME "hfi1_pcie_d.fw"
Mike Marciniszyn77241052015-07-30 15:17:43 -040072
73static uint fw_8051_load = 1;
74static uint fw_fabric_serdes_load = 1;
75static uint fw_pcie_serdes_load = 1;
76static uint fw_sbus_load = 1;
Easwar Hariharanc3838b32016-02-09 14:29:13 -080077
78/*
79 * Access required in platform.c
80 * Maintains state of whether the platform config was fetched via the
81 * fallback option
82 */
83uint platform_config_load;
Mike Marciniszyn77241052015-07-30 15:17:43 -040084
85/* Firmware file names get set in hfi1_firmware_init() based on the above */
86static char *fw_8051_name;
87static char *fw_fabric_serdes_name;
88static char *fw_sbus_name;
89static char *fw_pcie_serdes_name;
90static char *platform_config_name;
91
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)
96
97/*
98 * Firmware security header.
99 */
100struct css_header {
101 u32 module_type;
102 u32 header_len;
103 u32 header_version;
104 u32 module_id;
105 u32 module_vendor;
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 */
111 u32 reserved[22];
112};
Jubin Johnf4d507c2016-02-14 20:20:25 -0800113
Mike Marciniszyn77241052015-07-30 15:17:43 -0400114/* 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
119
120#define KEY_SIZE 256
121#define MU_SIZE 8
122#define EXPONENT_SIZE 4
123
124/* the file itself */
125struct firmware_file {
126 struct css_header css_header;
127 u8 modulus[KEY_SIZE];
128 u8 exponent[EXPONENT_SIZE];
129 u8 signature[KEY_SIZE];
130 u8 firmware[];
131};
132
133struct augmented_firmware_file {
134 struct css_header css_header;
135 u8 modulus[KEY_SIZE];
136 u8 exponent[EXPONENT_SIZE];
137 u8 signature[KEY_SIZE];
138 u8 r2[KEY_SIZE];
139 u8 mu[MU_SIZE];
140 u8 firmware[];
141};
142
143/* augmented file size difference */
144#define AUGMENT_SIZE (sizeof(struct augmented_firmware_file) - \
145 sizeof(struct firmware_file))
146
147struct firmware_details {
148 /* Linux core piece */
149 const struct firmware *fw;
150
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;
160};
161
162/*
163 * The mutex protects fw_state, fw_err, and all of the firmware_details
164 * variables.
165 */
166static DEFINE_MUTEX(fw_mutex);
167enum fw_state {
168 FW_EMPTY,
Dean Luickb3de8422015-12-01 15:38:10 -0500169 FW_TRY,
170 FW_FINAL,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400171 FW_ERR
172};
Jubin Johnf4d507c2016-02-14 20:20:25 -0800173
Mike Marciniszyn77241052015-07-30 15:17:43 -0400174static enum fw_state fw_state = FW_EMPTY;
175static int fw_err;
176static struct firmware_details fw_8051;
177static struct firmware_details fw_fabric;
178static struct firmware_details fw_pcie;
179static struct firmware_details fw_sbus;
180static const struct firmware *platform_config;
181
182/* flags for turn_off_spicos() */
183#define SPICO_SBUS 0x1
184#define SPICO_FABRIC 0x2
185#define ENABLE_SPICO_SMASK 0x1
186
187/* security block commands */
188#define RSA_CMD_INIT 0x1
189#define RSA_CMD_START 0x2
190
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
196
197/* RSA engine timeout, in ms */
198#define RSA_ENGINE_TIMEOUT 100 /* ms */
199
200/* hardware mutex timeout, in ms */
Dean Luickb0506f42016-03-05 08:50:22 -0800201#define HM_TIMEOUT 10 /* ms */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400202
203/* 8051 memory access timeout, in us */
204#define DC8051_ACCESS_TIMEOUT 100 /* us */
205
206/* the number of fabric SerDes on the SBus */
207#define NUM_FABRIC_SERDES 4
208
Dean Luickb3bf2702016-07-25 13:39:02 -0700209/* ASIC_STS_SBUS_RESULT.RESULT_CODE value */
210#define SBUS_READ_COMPLETE 0x4
211
Mike Marciniszyn77241052015-07-30 15:17:43 -0400212/* SBus fabric SerDes addresses, one set per HFI */
213static const u8 fabric_serdes_addrs[2][NUM_FABRIC_SERDES] = {
214 { 0x01, 0x02, 0x03, 0x04 },
215 { 0x28, 0x29, 0x2a, 0x2b }
216};
217
218/* SBus PCIe SerDes addresses, one set per HFI */
219static 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 }
224};
225
226/* SBus PCIe PCS addresses, one set per HFI */
227const 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 }
232};
233
234/* SBus fabric SerDes broadcast addresses, one per HFI */
235static const u8 fabric_serdes_broadcast[2] = { 0xe4, 0xe5 };
236static const u8 all_fabric_serdes_broadcast = 0xe1;
237
238/* SBus PCIe SerDes broadcast addresses, one per HFI */
239const u8 pcie_serdes_broadcast[2] = { 0xe2, 0xe3 };
240static const u8 all_pcie_serdes_broadcast = 0xe0;
241
242/* forwards */
243static void dispose_one_firmware(struct firmware_details *fdet);
Dean Luick53f449e2016-02-03 14:35:40 -0800244static int load_fabric_serdes_firmware(struct hfi1_devdata *dd,
245 struct firmware_details *fdet);
Dean Luickb3bf2702016-07-25 13:39:02 -0700246static void dump_fw_version(struct hfi1_devdata *dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400247
248/*
249 * Read a single 64-bit value from 8051 data memory.
250 *
251 * Expects:
252 * o caller to have already set up data read, no auto increment
253 * o caller to turn off read enable when finished
254 *
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.
258 *
259 * Return 0 on success, -ENXIO on a read error (timeout).
260 */
261static int __read_8051_data(struct hfi1_devdata *dd, u32 addr, u64 *result)
262{
263 u64 reg;
264 int count;
265
Dean Luickd7cf4cc2016-12-07 19:32:22 -0800266 /* step 1: set the address, clear enable */
267 reg = (addr & DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK)
268 << DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400269 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, reg);
Dean Luickd7cf4cc2016-12-07 19:32:22 -0800270 /* step 2: enable */
271 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL,
272 reg | DC_DC8051_CFG_RAM_ACCESS_CTRL_READ_ENA_SMASK);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400273
274 /* wait until ACCESS_COMPLETED is set */
275 count = 0;
276 while ((read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_STATUS)
277 & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK)
278 == 0) {
279 count++;
280 if (count > DC8051_ACCESS_TIMEOUT) {
281 dd_dev_err(dd, "timeout reading 8051 data\n");
282 return -ENXIO;
283 }
284 ndelay(10);
285 }
286
287 /* gather the data */
288 *result = read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_RD_DATA);
289
290 return 0;
291}
292
293/*
294 * Read 8051 data starting at addr, for len bytes. Will read in 8-byte chunks.
295 * Return 0 on success, -errno on error.
296 */
297int read_8051_data(struct hfi1_devdata *dd, u32 addr, u32 len, u64 *result)
298{
299 unsigned long flags;
300 u32 done;
301 int ret = 0;
302
303 spin_lock_irqsave(&dd->dc8051_memlock, flags);
304
305 /* data read set-up, no auto-increment */
306 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, 0);
307
308 for (done = 0; done < len; addr += 8, done += 8, result++) {
309 ret = __read_8051_data(dd, addr, result);
310 if (ret)
311 break;
312 }
313
314 /* turn off read enable */
315 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, 0);
316
317 spin_unlock_irqrestore(&dd->dc8051_memlock, flags);
318
319 return ret;
320}
321
322/*
323 * Write data or code to the 8051 code or data RAM.
324 */
325static int write_8051(struct hfi1_devdata *dd, int code, u32 start,
326 const u8 *data, u32 len)
327{
328 u64 reg;
329 u32 offset;
330 int aligned, count;
331
332 /* check alignment */
333 aligned = ((unsigned long)data & 0x7) == 0;
334
335 /* write set-up */
336 reg = (code ? DC_DC8051_CFG_RAM_ACCESS_SETUP_RAM_SEL_SMASK : 0ull)
337 | DC_DC8051_CFG_RAM_ACCESS_SETUP_AUTO_INCR_ADDR_SMASK;
338 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, reg);
339
340 reg = ((start & DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK)
341 << DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT)
342 | DC_DC8051_CFG_RAM_ACCESS_CTRL_WRITE_ENA_SMASK;
343 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, reg);
344
345 /* write */
346 for (offset = 0; offset < len; offset += 8) {
347 int bytes = len - offset;
348
349 if (bytes < 8) {
350 reg = 0;
351 memcpy(&reg, &data[offset], bytes);
352 } else if (aligned) {
353 reg = *(u64 *)&data[offset];
354 } else {
355 memcpy(&reg, &data[offset], 8);
356 }
357 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_WR_DATA, reg);
358
359 /* wait until ACCESS_COMPLETED is set */
360 count = 0;
361 while ((read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_STATUS)
362 & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK)
363 == 0) {
364 count++;
365 if (count > DC8051_ACCESS_TIMEOUT) {
366 dd_dev_err(dd, "timeout writing 8051 data\n");
367 return -ENXIO;
368 }
369 udelay(1);
370 }
371 }
372
373 /* turn off write access, auto increment (also sets to data access) */
374 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, 0);
375 write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, 0);
376
377 return 0;
378}
379
380/* return 0 if values match, non-zero and complain otherwise */
381static int invalid_header(struct hfi1_devdata *dd, const char *what,
382 u32 actual, u32 expected)
383{
384 if (actual == expected)
385 return 0;
386
387 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800388 "invalid firmware header field %s: expected 0x%x, actual 0x%x\n",
389 what, expected, actual);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400390 return 1;
391}
392
393/*
394 * Verify that the static fields in the CSS header match.
395 */
396static int verify_css_header(struct hfi1_devdata *dd, struct css_header *css)
397{
398 /* verify CSS header fields (most sizes are in DW, so add /4) */
Jubin John17fb4f22016-02-14 20:21:52 -0800399 if (invalid_header(dd, "module_type", css->module_type,
400 CSS_MODULE_TYPE) ||
401 invalid_header(dd, "header_len", css->header_len,
402 (sizeof(struct firmware_file) / 4)) ||
403 invalid_header(dd, "header_version", css->header_version,
404 CSS_HEADER_VERSION) ||
405 invalid_header(dd, "module_vendor", css->module_vendor,
406 CSS_MODULE_VENDOR) ||
Jubin Johnd0d236e2016-02-14 20:20:15 -0800407 invalid_header(dd, "key_size", css->key_size, KEY_SIZE / 4) ||
Jubin John17fb4f22016-02-14 20:21:52 -0800408 invalid_header(dd, "modulus_size", css->modulus_size,
409 KEY_SIZE / 4) ||
410 invalid_header(dd, "exponent_size", css->exponent_size,
411 EXPONENT_SIZE / 4)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400412 return -EINVAL;
413 }
414 return 0;
415}
416
417/*
418 * Make sure there are at least some bytes after the prefix.
419 */
420static int payload_check(struct hfi1_devdata *dd, const char *name,
421 long file_size, long prefix_size)
422{
423 /* make sure we have some payload */
424 if (prefix_size >= file_size) {
425 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800426 "firmware \"%s\", size %ld, must be larger than %ld bytes\n",
427 name, file_size, prefix_size);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400428 return -EINVAL;
429 }
430
431 return 0;
432}
433
434/*
435 * Request the firmware from the system. Extract the pieces and fill in
436 * fdet. If successful, the caller will need to call dispose_one_firmware().
437 * Returns 0 on success, -ERRNO on error.
438 */
439static int obtain_one_firmware(struct hfi1_devdata *dd, const char *name,
440 struct firmware_details *fdet)
441{
442 struct css_header *css;
443 int ret;
444
445 memset(fdet, 0, sizeof(*fdet));
446
447 ret = request_firmware(&fdet->fw, name, &dd->pcidev->dev);
448 if (ret) {
Dean Luickfe072e22016-02-03 14:32:06 -0800449 dd_dev_warn(dd, "cannot find firmware \"%s\", err %d\n",
450 name, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400451 return ret;
452 }
453
454 /* verify the firmware */
455 if (fdet->fw->size < sizeof(struct css_header)) {
456 dd_dev_err(dd, "firmware \"%s\" is too small\n", name);
457 ret = -EINVAL;
458 goto done;
459 }
460 css = (struct css_header *)fdet->fw->data;
461
462 hfi1_cdbg(FIRMWARE, "Firmware %s details:", name);
463 hfi1_cdbg(FIRMWARE, "file size: 0x%lx bytes", fdet->fw->size);
464 hfi1_cdbg(FIRMWARE, "CSS structure:");
465 hfi1_cdbg(FIRMWARE, " module_type 0x%x", css->module_type);
466 hfi1_cdbg(FIRMWARE, " header_len 0x%03x (0x%03x bytes)",
467 css->header_len, 4 * css->header_len);
468 hfi1_cdbg(FIRMWARE, " header_version 0x%x", css->header_version);
469 hfi1_cdbg(FIRMWARE, " module_id 0x%x", css->module_id);
470 hfi1_cdbg(FIRMWARE, " module_vendor 0x%x", css->module_vendor);
471 hfi1_cdbg(FIRMWARE, " date 0x%x", css->date);
472 hfi1_cdbg(FIRMWARE, " size 0x%03x (0x%03x bytes)",
473 css->size, 4 * css->size);
474 hfi1_cdbg(FIRMWARE, " key_size 0x%03x (0x%03x bytes)",
475 css->key_size, 4 * css->key_size);
476 hfi1_cdbg(FIRMWARE, " modulus_size 0x%03x (0x%03x bytes)",
477 css->modulus_size, 4 * css->modulus_size);
478 hfi1_cdbg(FIRMWARE, " exponent_size 0x%03x (0x%03x bytes)",
479 css->exponent_size, 4 * css->exponent_size);
480 hfi1_cdbg(FIRMWARE, "firmware size: 0x%lx bytes",
481 fdet->fw->size - sizeof(struct firmware_file));
482
483 /*
484 * If the file does not have a valid CSS header, fail.
485 * Otherwise, check the CSS size field for an expected size.
486 * The augmented file has r2 and mu inserted after the header
487 * was generated, so there will be a known difference between
488 * the CSS header size and the actual file size. Use this
489 * difference to identify an augmented file.
490 *
491 * Note: css->size is in DWORDs, multiply by 4 to get bytes.
492 */
493 ret = verify_css_header(dd, css);
494 if (ret) {
495 dd_dev_info(dd, "Invalid CSS header for \"%s\"\n", name);
Jubin John8638b772016-02-14 20:19:24 -0800496 } else if ((css->size * 4) == fdet->fw->size) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400497 /* non-augmented firmware file */
498 struct firmware_file *ff = (struct firmware_file *)
499 fdet->fw->data;
500
501 /* make sure there are bytes in the payload */
502 ret = payload_check(dd, name, fdet->fw->size,
Jubin John17fb4f22016-02-14 20:21:52 -0800503 sizeof(struct firmware_file));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400504 if (ret == 0) {
505 fdet->css_header = css;
506 fdet->modulus = ff->modulus;
507 fdet->exponent = ff->exponent;
508 fdet->signature = ff->signature;
509 fdet->r2 = fdet->dummy_header.r2; /* use dummy space */
510 fdet->mu = fdet->dummy_header.mu; /* use dummy space */
511 fdet->firmware_ptr = ff->firmware;
512 fdet->firmware_len = fdet->fw->size -
513 sizeof(struct firmware_file);
514 /*
515 * Header does not include r2 and mu - generate here.
516 * For now, fail.
517 */
518 dd_dev_err(dd, "driver is unable to validate firmware without r2 and mu (not in firmware file)\n");
519 ret = -EINVAL;
520 }
Jubin John8638b772016-02-14 20:19:24 -0800521 } else if ((css->size * 4) + AUGMENT_SIZE == fdet->fw->size) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400522 /* augmented firmware file */
523 struct augmented_firmware_file *aff =
524 (struct augmented_firmware_file *)fdet->fw->data;
525
526 /* make sure there are bytes in the payload */
527 ret = payload_check(dd, name, fdet->fw->size,
Jubin John17fb4f22016-02-14 20:21:52 -0800528 sizeof(struct augmented_firmware_file));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400529 if (ret == 0) {
530 fdet->css_header = css;
531 fdet->modulus = aff->modulus;
532 fdet->exponent = aff->exponent;
533 fdet->signature = aff->signature;
534 fdet->r2 = aff->r2;
535 fdet->mu = aff->mu;
536 fdet->firmware_ptr = aff->firmware;
537 fdet->firmware_len = fdet->fw->size -
538 sizeof(struct augmented_firmware_file);
539 }
540 } else {
541 /* css->size check failed */
542 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800543 "invalid firmware header field size: expected 0x%lx or 0x%lx, actual 0x%x\n",
544 fdet->fw->size / 4,
545 (fdet->fw->size - AUGMENT_SIZE) / 4,
546 css->size);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400547
548 ret = -EINVAL;
549 }
550
551done:
552 /* if returning an error, clean up after ourselves */
553 if (ret)
554 dispose_one_firmware(fdet);
555 return ret;
556}
557
558static void dispose_one_firmware(struct firmware_details *fdet)
559{
560 release_firmware(fdet->fw);
Dean Luickb3de8422015-12-01 15:38:10 -0500561 /* erase all previous information */
562 memset(fdet, 0, sizeof(*fdet));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400563}
564
565/*
Dean Luickb3de8422015-12-01 15:38:10 -0500566 * Obtain the 4 firmwares from the OS. All must be obtained at once or not
567 * at all. If called with the firmware state in FW_TRY, use alternate names.
568 * On exit, this routine will have set the firmware state to one of FW_TRY,
569 * FW_FINAL, or FW_ERR.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400570 *
Dean Luickb3de8422015-12-01 15:38:10 -0500571 * Must be holding fw_mutex.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400572 */
Dean Luickb3de8422015-12-01 15:38:10 -0500573static void __obtain_firmware(struct hfi1_devdata *dd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400574{
575 int err = 0;
576
Dean Luickb3de8422015-12-01 15:38:10 -0500577 if (fw_state == FW_FINAL) /* nothing more to obtain */
578 return;
579 if (fw_state == FW_ERR) /* already in error */
580 return;
581
582 /* fw_state is FW_EMPTY or FW_TRY */
583retry:
584 if (fw_state == FW_TRY) {
585 /*
586 * We tried the original and it failed. Move to the
587 * alternate.
588 */
Dean Luickfe072e22016-02-03 14:32:06 -0800589 dd_dev_warn(dd, "using alternate firmware names\n");
Dean Luickb3de8422015-12-01 15:38:10 -0500590 /*
591 * Let others run. Some systems, when missing firmware, does
592 * something that holds for 30 seconds. If we do that twice
593 * in a row it triggers task blocked warning.
594 */
595 cond_resched();
596 if (fw_8051_load)
597 dispose_one_firmware(&fw_8051);
598 if (fw_fabric_serdes_load)
599 dispose_one_firmware(&fw_fabric);
600 if (fw_sbus_load)
601 dispose_one_firmware(&fw_sbus);
602 if (fw_pcie_serdes_load)
603 dispose_one_firmware(&fw_pcie);
604 fw_8051_name = ALT_FW_8051_NAME_ASIC;
605 fw_fabric_serdes_name = ALT_FW_FABRIC_NAME;
606 fw_sbus_name = ALT_FW_SBUS_NAME;
607 fw_pcie_serdes_name = ALT_FW_PCIE_NAME;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608 }
609
Mike Marciniszyn77241052015-07-30 15:17:43 -0400610 if (fw_sbus_load) {
611 err = obtain_one_firmware(dd, fw_sbus_name, &fw_sbus);
612 if (err)
613 goto done;
614 }
615
616 if (fw_pcie_serdes_load) {
617 err = obtain_one_firmware(dd, fw_pcie_serdes_name, &fw_pcie);
618 if (err)
619 goto done;
620 }
621
Dean Luick6b14e0e2016-02-03 14:31:40 -0800622 if (fw_fabric_serdes_load) {
623 err = obtain_one_firmware(dd, fw_fabric_serdes_name,
624 &fw_fabric);
625 if (err)
626 goto done;
627 }
628
629 if (fw_8051_load) {
630 err = obtain_one_firmware(dd, fw_8051_name, &fw_8051);
631 if (err)
632 goto done;
633 }
634
Dean Luickb3de8422015-12-01 15:38:10 -0500635done:
636 if (err) {
637 /* oops, had problems obtaining a firmware */
Dean Luick6b14e0e2016-02-03 14:31:40 -0800638 if (fw_state == FW_EMPTY && dd->icode == ICODE_RTL_SILICON) {
639 /* retry with alternate (RTL only) */
Dean Luickb3de8422015-12-01 15:38:10 -0500640 fw_state = FW_TRY;
641 goto retry;
642 }
Dean Luickfe072e22016-02-03 14:32:06 -0800643 dd_dev_err(dd, "unable to obtain working firmware\n");
Dean Luickb3de8422015-12-01 15:38:10 -0500644 fw_state = FW_ERR;
645 fw_err = -ENOENT;
646 } else {
647 /* success */
Dean Luickdcc68e52016-02-03 14:32:23 -0800648 if (fw_state == FW_EMPTY &&
649 dd->icode != ICODE_FUNCTIONAL_SIMULATOR)
Dean Luickb3de8422015-12-01 15:38:10 -0500650 fw_state = FW_TRY; /* may retry later */
651 else
652 fw_state = FW_FINAL; /* cannot try again */
653 }
654}
655
656/*
657 * Called by all HFIs when loading their firmware - i.e. device probe time.
658 * The first one will do the actual firmware load. Use a mutex to resolve
659 * any possible race condition.
660 *
661 * The call to this routine cannot be moved to driver load because the kernel
662 * call request_firmware() requires a device which is only available after
663 * the first device probe.
664 */
665static int obtain_firmware(struct hfi1_devdata *dd)
666{
667 unsigned long timeout;
668 int err = 0;
669
670 mutex_lock(&fw_mutex);
671
672 /* 40s delay due to long delay on missing firmware on some systems */
673 timeout = jiffies + msecs_to_jiffies(40000);
674 while (fw_state == FW_TRY) {
675 /*
676 * Another device is trying the firmware. Wait until it
677 * decides what works (or not).
678 */
679 if (time_after(jiffies, timeout)) {
680 /* waited too long */
681 dd_dev_err(dd, "Timeout waiting for firmware try");
682 fw_state = FW_ERR;
683 fw_err = -ETIMEDOUT;
684 break;
685 }
686 mutex_unlock(&fw_mutex);
687 msleep(20); /* arbitrary delay */
688 mutex_lock(&fw_mutex);
689 }
690 /* not in FW_TRY state */
691
Easwar Hariharanc3838b32016-02-09 14:29:13 -0800692 if (fw_state == FW_FINAL) {
693 if (platform_config) {
694 dd->platform_config.data = platform_config->data;
695 dd->platform_config.size = platform_config->size;
696 }
Dean Luickb3de8422015-12-01 15:38:10 -0500697 goto done; /* already acquired */
Easwar Hariharanc3838b32016-02-09 14:29:13 -0800698 } else if (fw_state == FW_ERR) {
Dean Luickb3de8422015-12-01 15:38:10 -0500699 goto done; /* already tried and failed */
Easwar Hariharanc3838b32016-02-09 14:29:13 -0800700 }
Dean Luickb3de8422015-12-01 15:38:10 -0500701 /* fw_state is FW_EMPTY */
702
703 /* set fw_state to FW_TRY, FW_FINAL, or FW_ERR, and fw_err */
704 __obtain_firmware(dd);
705
Mike Marciniszyn77241052015-07-30 15:17:43 -0400706 if (platform_config_load) {
707 platform_config = NULL;
708 err = request_firmware(&platform_config, platform_config_name,
Jubin John17fb4f22016-02-14 20:21:52 -0800709 &dd->pcidev->dev);
Easwar Hariharanc3838b32016-02-09 14:29:13 -0800710 if (err) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400711 platform_config = NULL;
Easwar Hariharanfe4d9242016-10-17 04:19:47 -0700712 dd_dev_err(dd,
713 "%s: No default platform config file found\n",
714 __func__);
Easwar Hariharanc3838b32016-02-09 14:29:13 -0800715 goto done;
716 }
717 dd->platform_config.data = platform_config->data;
718 dd->platform_config.size = platform_config->size;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400719 }
720
Mike Marciniszyn77241052015-07-30 15:17:43 -0400721done:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400722 mutex_unlock(&fw_mutex);
723
Dean Luickb3de8422015-12-01 15:38:10 -0500724 return fw_err;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400725}
726
727/*
728 * Called when the driver unloads. The timing is asymmetric with its
729 * counterpart, obtain_firmware(). If called at device remove time,
730 * then it is conceivable that another device could probe while the
731 * firmware is being disposed. The mutexes can be moved to do that
732 * safely, but then the firmware would be requested from the OS multiple
733 * times.
734 *
735 * No mutex is needed as the driver is unloading and there cannot be any
736 * other callers.
737 */
738void dispose_firmware(void)
739{
740 dispose_one_firmware(&fw_8051);
741 dispose_one_firmware(&fw_fabric);
742 dispose_one_firmware(&fw_pcie);
743 dispose_one_firmware(&fw_sbus);
744
745 release_firmware(platform_config);
746 platform_config = NULL;
747
748 /* retain the error state, otherwise revert to empty */
749 if (fw_state != FW_ERR)
750 fw_state = FW_EMPTY;
751}
752
753/*
Dean Luickb3de8422015-12-01 15:38:10 -0500754 * Called with the result of a firmware download.
755 *
756 * Return 1 to retry loading the firmware, 0 to stop.
757 */
758static int retry_firmware(struct hfi1_devdata *dd, int load_result)
759{
760 int retry;
761
762 mutex_lock(&fw_mutex);
763
764 if (load_result == 0) {
765 /*
766 * The load succeeded, so expect all others to do the same.
767 * Do not retry again.
768 */
769 if (fw_state == FW_TRY)
770 fw_state = FW_FINAL;
771 retry = 0; /* do NOT retry */
772 } else if (fw_state == FW_TRY) {
773 /* load failed, obtain alternate firmware */
774 __obtain_firmware(dd);
775 retry = (fw_state == FW_FINAL);
776 } else {
777 /* else in FW_FINAL or FW_ERR, no retry in either case */
778 retry = 0;
779 }
780
781 mutex_unlock(&fw_mutex);
782 return retry;
783}
784
785/*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400786 * Write a block of data to a given array CSR. All calls will be in
787 * multiples of 8 bytes.
788 */
789static void write_rsa_data(struct hfi1_devdata *dd, int what,
790 const u8 *data, int nbytes)
791{
Jubin John8638b772016-02-14 20:19:24 -0800792 int qw_size = nbytes / 8;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400793 int i;
794
795 if (((unsigned long)data & 0x7) == 0) {
796 /* aligned */
797 u64 *ptr = (u64 *)data;
798
799 for (i = 0; i < qw_size; i++, ptr++)
Jubin John8638b772016-02-14 20:19:24 -0800800 write_csr(dd, what + (8 * i), *ptr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400801 } else {
802 /* not aligned */
803 for (i = 0; i < qw_size; i++, data += 8) {
804 u64 value;
805
806 memcpy(&value, data, 8);
Jubin John8638b772016-02-14 20:19:24 -0800807 write_csr(dd, what + (8 * i), value);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400808 }
809 }
810}
811
812/*
813 * Write a block of data to a given CSR as a stream of writes. All calls will
814 * be in multiples of 8 bytes.
815 */
816static void write_streamed_rsa_data(struct hfi1_devdata *dd, int what,
817 const u8 *data, int nbytes)
818{
819 u64 *ptr = (u64 *)data;
Jubin John8638b772016-02-14 20:19:24 -0800820 int qw_size = nbytes / 8;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400821
822 for (; qw_size > 0; qw_size--, ptr++)
823 write_csr(dd, what, *ptr);
824}
825
826/*
827 * Download the signature and start the RSA mechanism. Wait for
828 * RSA_ENGINE_TIMEOUT before giving up.
829 */
830static int run_rsa(struct hfi1_devdata *dd, const char *who,
831 const u8 *signature)
832{
833 unsigned long timeout;
834 u64 reg;
835 u32 status;
836 int ret = 0;
837
838 /* write the signature */
839 write_rsa_data(dd, MISC_CFG_RSA_SIGNATURE, signature, KEY_SIZE);
840
841 /* initialize RSA */
842 write_csr(dd, MISC_CFG_RSA_CMD, RSA_CMD_INIT);
843
844 /*
845 * Make sure the engine is idle and insert a delay between the two
846 * writes to MISC_CFG_RSA_CMD.
847 */
848 status = (read_csr(dd, MISC_CFG_FW_CTRL)
849 & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK)
850 >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT;
851 if (status != RSA_STATUS_IDLE) {
852 dd_dev_err(dd, "%s security engine not idle - giving up\n",
Jubin John17fb4f22016-02-14 20:21:52 -0800853 who);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400854 return -EBUSY;
855 }
856
857 /* start RSA */
858 write_csr(dd, MISC_CFG_RSA_CMD, RSA_CMD_START);
859
860 /*
861 * Look for the result.
862 *
863 * The RSA engine is hooked up to two MISC errors. The driver
864 * masks these errors as they do not respond to the standard
865 * error "clear down" mechanism. Look for these errors here and
866 * clear them when possible. This routine will exit with the
867 * errors of the current run still set.
868 *
869 * MISC_FW_AUTH_FAILED_ERR
870 * Firmware authorization failed. This can be cleared by
871 * re-initializing the RSA engine, then clearing the status bit.
872 * Do not re-init the RSA angine immediately after a successful
873 * run - this will reset the current authorization.
874 *
875 * MISC_KEY_MISMATCH_ERR
876 * Key does not match. The only way to clear this is to load
877 * a matching key then clear the status bit. If this error
878 * is raised, it will persist outside of this routine until a
879 * matching key is loaded.
880 */
881 timeout = msecs_to_jiffies(RSA_ENGINE_TIMEOUT) + jiffies;
882 while (1) {
883 status = (read_csr(dd, MISC_CFG_FW_CTRL)
884 & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK)
885 >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT;
886
887 if (status == RSA_STATUS_IDLE) {
888 /* should not happen */
889 dd_dev_err(dd, "%s firmware security bad idle state\n",
Jubin John17fb4f22016-02-14 20:21:52 -0800890 who);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400891 ret = -EINVAL;
892 break;
893 } else if (status == RSA_STATUS_DONE) {
894 /* finished successfully */
895 break;
896 } else if (status == RSA_STATUS_FAILED) {
897 /* finished unsuccessfully */
898 ret = -EINVAL;
899 break;
900 }
901 /* else still active */
902
903 if (time_after(jiffies, timeout)) {
904 /*
905 * Timed out while active. We can't reset the engine
906 * if it is stuck active, but run through the
907 * error code to see what error bits are set.
908 */
909 dd_dev_err(dd, "%s firmware security time out\n", who);
910 ret = -ETIMEDOUT;
911 break;
912 }
913
914 msleep(20);
915 }
916
917 /*
918 * Arrive here on success or failure. Clear all RSA engine
919 * errors. All current errors will stick - the RSA logic is keeping
920 * error high. All previous errors will clear - the RSA logic
921 * is not keeping the error high.
922 */
923 write_csr(dd, MISC_ERR_CLEAR,
Jubin John17fb4f22016-02-14 20:21:52 -0800924 MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK |
925 MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400926 /*
Dean Luickfe072e22016-02-03 14:32:06 -0800927 * All that is left are the current errors. Print warnings on
928 * authorization failure details, if any. Firmware authorization
929 * can be retried, so these are only warnings.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400930 */
931 reg = read_csr(dd, MISC_ERR_STATUS);
932 if (ret) {
933 if (reg & MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK)
Dean Luickfe072e22016-02-03 14:32:06 -0800934 dd_dev_warn(dd, "%s firmware authorization failed\n",
935 who);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400936 if (reg & MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK)
Dean Luickfe072e22016-02-03 14:32:06 -0800937 dd_dev_warn(dd, "%s firmware key mismatch\n", who);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400938 }
939
940 return ret;
941}
942
943static void load_security_variables(struct hfi1_devdata *dd,
944 struct firmware_details *fdet)
945{
946 /* Security variables a. Write the modulus */
947 write_rsa_data(dd, MISC_CFG_RSA_MODULUS, fdet->modulus, KEY_SIZE);
948 /* Security variables b. Write the r2 */
949 write_rsa_data(dd, MISC_CFG_RSA_R2, fdet->r2, KEY_SIZE);
950 /* Security variables c. Write the mu */
951 write_rsa_data(dd, MISC_CFG_RSA_MU, fdet->mu, MU_SIZE);
952 /* Security variables d. Write the header */
953 write_streamed_rsa_data(dd, MISC_CFG_SHA_PRELOAD,
Jubin John17fb4f22016-02-14 20:21:52 -0800954 (u8 *)fdet->css_header,
955 sizeof(struct css_header));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400956}
957
958/* return the 8051 firmware state */
959static inline u32 get_firmware_state(struct hfi1_devdata *dd)
960{
961 u64 reg = read_csr(dd, DC_DC8051_STS_CUR_STATE);
962
963 return (reg >> DC_DC8051_STS_CUR_STATE_FIRMWARE_SHIFT)
964 & DC_DC8051_STS_CUR_STATE_FIRMWARE_MASK;
965}
966
967/*
968 * Wait until the firmware is up and ready to take host requests.
969 * Return 0 on success, -ETIMEDOUT on timeout.
970 */
971int wait_fm_ready(struct hfi1_devdata *dd, u32 mstimeout)
972{
973 unsigned long timeout;
974
975 /* in the simulator, the fake 8051 is always ready */
976 if (dd->icode == ICODE_FUNCTIONAL_SIMULATOR)
977 return 0;
978
979 timeout = msecs_to_jiffies(mstimeout) + jiffies;
980 while (1) {
981 if (get_firmware_state(dd) == 0xa0) /* ready */
982 return 0;
983 if (time_after(jiffies, timeout)) /* timed out */
984 return -ETIMEDOUT;
985 usleep_range(1950, 2050); /* sleep 2ms-ish */
986 }
987}
988
989/*
990 * Load the 8051 firmware.
991 */
992static int load_8051_firmware(struct hfi1_devdata *dd,
993 struct firmware_details *fdet)
994{
995 u64 reg;
996 int ret;
997 u8 ver_a, ver_b;
998
999 /*
1000 * DC Reset sequence
1001 * Load DC 8051 firmware
1002 */
1003 /*
1004 * DC reset step 1: Reset DC8051
1005 */
1006 reg = DC_DC8051_CFG_RST_M8051W_SMASK
1007 | DC_DC8051_CFG_RST_CRAM_SMASK
1008 | DC_DC8051_CFG_RST_DRAM_SMASK
1009 | DC_DC8051_CFG_RST_IRAM_SMASK
1010 | DC_DC8051_CFG_RST_SFR_SMASK;
1011 write_csr(dd, DC_DC8051_CFG_RST, reg);
1012
1013 /*
1014 * DC reset step 2 (optional): Load 8051 data memory with link
1015 * configuration
1016 */
1017
1018 /*
1019 * DC reset step 3: Load DC8051 firmware
1020 */
1021 /* release all but the core reset */
1022 reg = DC_DC8051_CFG_RST_M8051W_SMASK;
1023 write_csr(dd, DC_DC8051_CFG_RST, reg);
1024
1025 /* Firmware load step 1 */
1026 load_security_variables(dd, fdet);
1027
1028 /*
1029 * Firmware load step 2. Clear MISC_CFG_FW_CTRL.FW_8051_LOADED
1030 */
1031 write_csr(dd, MISC_CFG_FW_CTRL, 0);
1032
1033 /* Firmware load steps 3-5 */
1034 ret = write_8051(dd, 1/*code*/, 0, fdet->firmware_ptr,
Jubin John17fb4f22016-02-14 20:21:52 -08001035 fdet->firmware_len);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001036 if (ret)
1037 return ret;
1038
1039 /*
1040 * DC reset step 4. Host starts the DC8051 firmware
1041 */
1042 /*
1043 * Firmware load step 6. Set MISC_CFG_FW_CTRL.FW_8051_LOADED
1044 */
1045 write_csr(dd, MISC_CFG_FW_CTRL, MISC_CFG_FW_CTRL_FW_8051_LOADED_SMASK);
1046
1047 /* Firmware load steps 7-10 */
1048 ret = run_rsa(dd, "8051", fdet->signature);
1049 if (ret)
1050 return ret;
1051
1052 /* clear all reset bits, releasing the 8051 */
1053 write_csr(dd, DC_DC8051_CFG_RST, 0ull);
1054
1055 /*
1056 * DC reset step 5. Wait for firmware to be ready to accept host
1057 * requests.
1058 */
1059 ret = wait_fm_ready(dd, TIMEOUT_8051_START);
1060 if (ret) { /* timed out */
1061 dd_dev_err(dd, "8051 start timeout, current state 0x%x\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001062 get_firmware_state(dd));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001063 return -ETIMEDOUT;
1064 }
1065
1066 read_misc_status(dd, &ver_a, &ver_b);
1067 dd_dev_info(dd, "8051 firmware version %d.%d\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001068 (int)ver_b, (int)ver_a);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001069 dd->dc8051_ver = dc8051_ver(ver_b, ver_a);
1070
1071 return 0;
1072}
1073
Mike Marciniszyn77241052015-07-30 15:17:43 -04001074/*
1075 * Write the SBus request register
1076 *
1077 * No need for masking - the arguments are sized exactly.
1078 */
1079void sbus_request(struct hfi1_devdata *dd,
1080 u8 receiver_addr, u8 data_addr, u8 command, u32 data_in)
1081{
1082 write_csr(dd, ASIC_CFG_SBUS_REQUEST,
Jubin John17fb4f22016-02-14 20:21:52 -08001083 ((u64)data_in << ASIC_CFG_SBUS_REQUEST_DATA_IN_SHIFT) |
1084 ((u64)command << ASIC_CFG_SBUS_REQUEST_COMMAND_SHIFT) |
1085 ((u64)data_addr << ASIC_CFG_SBUS_REQUEST_DATA_ADDR_SHIFT) |
1086 ((u64)receiver_addr <<
1087 ASIC_CFG_SBUS_REQUEST_RECEIVER_ADDR_SHIFT));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001088}
1089
1090/*
Dean Luickb3bf2702016-07-25 13:39:02 -07001091 * Read a value from the SBus.
1092 *
1093 * Requires the caller to be in fast mode
1094 */
1095static u32 sbus_read(struct hfi1_devdata *dd, u8 receiver_addr, u8 data_addr,
1096 u32 data_in)
1097{
1098 u64 reg;
1099 int retries;
1100 int success = 0;
1101 u32 result = 0;
1102 u32 result_code = 0;
1103
1104 sbus_request(dd, receiver_addr, data_addr, READ_SBUS_RECEIVER, data_in);
1105
1106 for (retries = 0; retries < 100; retries++) {
1107 usleep_range(1000, 1200); /* arbitrary */
1108 reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
1109 result_code = (reg >> ASIC_STS_SBUS_RESULT_RESULT_CODE_SHIFT)
1110 & ASIC_STS_SBUS_RESULT_RESULT_CODE_MASK;
1111 if (result_code != SBUS_READ_COMPLETE)
1112 continue;
1113
1114 success = 1;
1115 result = (reg >> ASIC_STS_SBUS_RESULT_DATA_OUT_SHIFT)
1116 & ASIC_STS_SBUS_RESULT_DATA_OUT_MASK;
1117 break;
1118 }
1119
1120 if (!success) {
1121 dd_dev_err(dd, "%s: read failed, result code 0x%x\n", __func__,
1122 result_code);
1123 }
1124
1125 return result;
1126}
1127
1128/*
Mike Marciniszyn77241052015-07-30 15:17:43 -04001129 * Turn off the SBus and fabric serdes spicos.
1130 *
1131 * + Must be called with Sbus fast mode turned on.
1132 * + Must be called after fabric serdes broadcast is set up.
1133 * + Must be called before the 8051 is loaded - assumes 8051 is not loaded
1134 * when using MISC_CFG_FW_CTRL.
1135 */
1136static void turn_off_spicos(struct hfi1_devdata *dd, int flags)
1137{
1138 /* only needed on A0 */
Mike Marciniszyn995deaf2015-11-16 21:59:29 -05001139 if (!is_ax(dd))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001140 return;
1141
1142 dd_dev_info(dd, "Turning off spicos:%s%s\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001143 flags & SPICO_SBUS ? " SBus" : "",
1144 flags & SPICO_FABRIC ? " fabric" : "");
Mike Marciniszyn77241052015-07-30 15:17:43 -04001145
1146 write_csr(dd, MISC_CFG_FW_CTRL, ENABLE_SPICO_SMASK);
1147 /* disable SBus spico */
1148 if (flags & SPICO_SBUS)
1149 sbus_request(dd, SBUS_MASTER_BROADCAST, 0x01,
Jubin John17fb4f22016-02-14 20:21:52 -08001150 WRITE_SBUS_RECEIVER, 0x00000040);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001151
1152 /* disable the fabric serdes spicos */
1153 if (flags & SPICO_FABRIC)
1154 sbus_request(dd, fabric_serdes_broadcast[dd->hfi1_id],
1155 0x07, WRITE_SBUS_RECEIVER, 0x00000000);
1156 write_csr(dd, MISC_CFG_FW_CTRL, 0);
1157}
1158
1159/*
Dean Luick53f449e2016-02-03 14:35:40 -08001160 * Reset all of the fabric serdes for this HFI in preparation to take the
1161 * link to Polling.
1162 *
1163 * To do a reset, we need to write to to the serdes registers. Unfortunately,
1164 * the fabric serdes download to the other HFI on the ASIC will have turned
1165 * off the firmware validation on this HFI. This means we can't write to the
1166 * registers to reset the serdes. Work around this by performing a complete
1167 * re-download and validation of the fabric serdes firmware. This, as a
1168 * by-product, will reset the serdes. NOTE: the re-download requires that
1169 * the 8051 be in the Offline state. I.e. not actively trying to use the
1170 * serdes. This routine is called at the point where the link is Offline and
1171 * is getting ready to go to Polling.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001172 */
1173void fabric_serdes_reset(struct hfi1_devdata *dd)
1174{
Dean Luick576531f2016-03-05 08:50:01 -08001175 int ret;
1176
Dean Luick53f449e2016-02-03 14:35:40 -08001177 if (!fw_fabric_serdes_load)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001178 return;
1179
Dean Luick576531f2016-03-05 08:50:01 -08001180 ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
1181 if (ret) {
1182 dd_dev_err(dd,
1183 "Cannot acquire SBus resource to reset fabric SerDes - perhaps you should reboot\n");
1184 return;
1185 }
1186 set_sbus_fast_mode(dd);
1187
Dean Luick53f449e2016-02-03 14:35:40 -08001188 if (is_ax(dd)) {
1189 /* A0 serdes do not work with a re-download */
1190 u8 ra = fabric_serdes_broadcast[dd->hfi1_id];
1191
Dean Luick53f449e2016-02-03 14:35:40 -08001192 /* place SerDes in reset and disable SPICO */
1193 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000011);
1194 /* wait 100 refclk cycles @ 156.25MHz => 640ns */
1195 udelay(1);
1196 /* remove SerDes reset */
1197 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000010);
1198 /* turn SPICO enable on */
1199 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000002);
Dean Luick576531f2016-03-05 08:50:01 -08001200 } else {
1201 turn_off_spicos(dd, SPICO_FABRIC);
1202 /*
1203 * No need for firmware retry - what to download has already
1204 * been decided.
1205 * No need to pay attention to the load return - the only
1206 * failure is a validation failure, which has already been
1207 * checked by the initial download.
1208 */
1209 (void)load_fabric_serdes_firmware(dd, &fw_fabric);
Dean Luick53f449e2016-02-03 14:35:40 -08001210 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001211
Mike Marciniszyn77241052015-07-30 15:17:43 -04001212 clear_sbus_fast_mode(dd);
Dean Luick576531f2016-03-05 08:50:01 -08001213 release_chip_resource(dd, CR_SBUS);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001214}
1215
1216/* Access to the SBus in this routine should probably be serialized */
1217int sbus_request_slow(struct hfi1_devdata *dd,
1218 u8 receiver_addr, u8 data_addr, u8 command, u32 data_in)
1219{
1220 u64 reg, count = 0;
1221
Dean Luick3afb6f62016-03-05 08:49:39 -08001222 /* make sure fast mode is clear */
1223 clear_sbus_fast_mode(dd);
1224
Mike Marciniszyn77241052015-07-30 15:17:43 -04001225 sbus_request(dd, receiver_addr, data_addr, command, data_in);
1226 write_csr(dd, ASIC_CFG_SBUS_EXECUTE,
1227 ASIC_CFG_SBUS_EXECUTE_EXECUTE_SMASK);
1228 /* Wait for both DONE and RCV_DATA_VALID to go high */
1229 reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
1230 while (!((reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) &&
1231 (reg & ASIC_STS_SBUS_RESULT_RCV_DATA_VALID_SMASK))) {
1232 if (count++ >= SBUS_MAX_POLL_COUNT) {
1233 u64 counts = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
1234 /*
1235 * If the loop has timed out, we are OK if DONE bit
1236 * is set and RCV_DATA_VALID and EXECUTE counters
1237 * are the same. If not, we cannot proceed.
1238 */
1239 if ((reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) &&
1240 (SBUS_COUNTER(counts, RCV_DATA_VALID) ==
1241 SBUS_COUNTER(counts, EXECUTE)))
1242 break;
1243 return -ETIMEDOUT;
1244 }
1245 udelay(1);
1246 reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
1247 }
1248 count = 0;
1249 write_csr(dd, ASIC_CFG_SBUS_EXECUTE, 0);
1250 /* Wait for DONE to clear after EXECUTE is cleared */
1251 reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
1252 while (reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) {
1253 if (count++ >= SBUS_MAX_POLL_COUNT)
1254 return -ETIME;
1255 udelay(1);
1256 reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
1257 }
1258 return 0;
1259}
1260
1261static int load_fabric_serdes_firmware(struct hfi1_devdata *dd,
1262 struct firmware_details *fdet)
1263{
1264 int i, err;
1265 const u8 ra = fabric_serdes_broadcast[dd->hfi1_id]; /* receiver addr */
1266
1267 dd_dev_info(dd, "Downloading fabric firmware\n");
1268
1269 /* step 1: load security variables */
1270 load_security_variables(dd, fdet);
1271 /* step 2: place SerDes in reset and disable SPICO */
1272 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000011);
1273 /* wait 100 refclk cycles @ 156.25MHz => 640ns */
1274 udelay(1);
1275 /* step 3: remove SerDes reset */
1276 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000010);
1277 /* step 4: assert IMEM override */
1278 sbus_request(dd, ra, 0x00, WRITE_SBUS_RECEIVER, 0x40000000);
1279 /* step 5: download SerDes machine code */
1280 for (i = 0; i < fdet->firmware_len; i += 4) {
1281 sbus_request(dd, ra, 0x0a, WRITE_SBUS_RECEIVER,
Jubin John17fb4f22016-02-14 20:21:52 -08001282 *(u32 *)&fdet->firmware_ptr[i]);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001283 }
1284 /* step 6: IMEM override off */
1285 sbus_request(dd, ra, 0x00, WRITE_SBUS_RECEIVER, 0x00000000);
1286 /* step 7: turn ECC on */
1287 sbus_request(dd, ra, 0x0b, WRITE_SBUS_RECEIVER, 0x000c0000);
1288
1289 /* steps 8-11: run the RSA engine */
1290 err = run_rsa(dd, "fabric serdes", fdet->signature);
1291 if (err)
1292 return err;
1293
1294 /* step 12: turn SPICO enable on */
1295 sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000002);
1296 /* step 13: enable core hardware interrupts */
1297 sbus_request(dd, ra, 0x08, WRITE_SBUS_RECEIVER, 0x00000000);
1298
1299 return 0;
1300}
1301
1302static int load_sbus_firmware(struct hfi1_devdata *dd,
1303 struct firmware_details *fdet)
1304{
1305 int i, err;
1306 const u8 ra = SBUS_MASTER_BROADCAST; /* receiver address */
1307
1308 dd_dev_info(dd, "Downloading SBus firmware\n");
1309
1310 /* step 1: load security variables */
1311 load_security_variables(dd, fdet);
1312 /* step 2: place SPICO into reset and enable off */
1313 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x000000c0);
1314 /* step 3: remove reset, enable off, IMEM_CNTRL_EN on */
1315 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000240);
1316 /* step 4: set starting IMEM address for burst download */
1317 sbus_request(dd, ra, 0x03, WRITE_SBUS_RECEIVER, 0x80000000);
1318 /* step 5: download the SBus Master machine code */
1319 for (i = 0; i < fdet->firmware_len; i += 4) {
1320 sbus_request(dd, ra, 0x14, WRITE_SBUS_RECEIVER,
Jubin John17fb4f22016-02-14 20:21:52 -08001321 *(u32 *)&fdet->firmware_ptr[i]);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001322 }
1323 /* step 6: set IMEM_CNTL_EN off */
1324 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000040);
1325 /* step 7: turn ECC on */
1326 sbus_request(dd, ra, 0x16, WRITE_SBUS_RECEIVER, 0x000c0000);
1327
1328 /* steps 8-11: run the RSA engine */
1329 err = run_rsa(dd, "SBus", fdet->signature);
1330 if (err)
1331 return err;
1332
1333 /* step 12: set SPICO_ENABLE on */
1334 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000140);
1335
1336 return 0;
1337}
1338
1339static int load_pcie_serdes_firmware(struct hfi1_devdata *dd,
1340 struct firmware_details *fdet)
1341{
1342 int i;
1343 const u8 ra = SBUS_MASTER_BROADCAST; /* receiver address */
1344
1345 dd_dev_info(dd, "Downloading PCIe firmware\n");
1346
1347 /* step 1: load security variables */
1348 load_security_variables(dd, fdet);
1349 /* step 2: assert single step (halts the SBus Master spico) */
1350 sbus_request(dd, ra, 0x05, WRITE_SBUS_RECEIVER, 0x00000001);
1351 /* step 3: enable XDMEM access */
1352 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000d40);
1353 /* step 4: load firmware into SBus Master XDMEM */
Jubin John4d114fd2016-02-14 20:21:43 -08001354 /*
1355 * NOTE: the dmem address, write_en, and wdata are all pre-packed,
1356 * we only need to pick up the bytes and write them
1357 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001358 for (i = 0; i < fdet->firmware_len; i += 4) {
1359 sbus_request(dd, ra, 0x04, WRITE_SBUS_RECEIVER,
Jubin John17fb4f22016-02-14 20:21:52 -08001360 *(u32 *)&fdet->firmware_ptr[i]);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001361 }
1362 /* step 5: disable XDMEM access */
1363 sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000140);
1364 /* step 6: allow SBus Spico to run */
1365 sbus_request(dd, ra, 0x05, WRITE_SBUS_RECEIVER, 0x00000000);
1366
Jubin John4d114fd2016-02-14 20:21:43 -08001367 /*
1368 * steps 7-11: run RSA, if it succeeds, firmware is available to
1369 * be swapped
1370 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001371 return run_rsa(dd, "PCIe serdes", fdet->signature);
1372}
1373
1374/*
1375 * Set the given broadcast values on the given list of devices.
1376 */
1377static void set_serdes_broadcast(struct hfi1_devdata *dd, u8 bg1, u8 bg2,
1378 const u8 *addrs, int count)
1379{
1380 while (--count >= 0) {
1381 /*
1382 * Set BROADCAST_GROUP_1 and BROADCAST_GROUP_2, leave
1383 * defaults for everything else. Do not read-modify-write,
1384 * per instruction from the manufacturer.
1385 *
1386 * Register 0xfd:
1387 * bits what
1388 * ----- ---------------------------------
1389 * 0 IGNORE_BROADCAST (default 0)
1390 * 11:4 BROADCAST_GROUP_1 (default 0xff)
1391 * 23:16 BROADCAST_GROUP_2 (default 0xff)
1392 */
1393 sbus_request(dd, addrs[count], 0xfd, WRITE_SBUS_RECEIVER,
Jubin John17fb4f22016-02-14 20:21:52 -08001394 (u32)bg1 << 4 | (u32)bg2 << 16);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001395 }
1396}
1397
1398int acquire_hw_mutex(struct hfi1_devdata *dd)
1399{
1400 unsigned long timeout;
1401 int try = 0;
1402 u8 mask = 1 << dd->hfi1_id;
1403 u8 user;
1404
1405retry:
1406 timeout = msecs_to_jiffies(HM_TIMEOUT) + jiffies;
1407 while (1) {
1408 write_csr(dd, ASIC_CFG_MUTEX, mask);
1409 user = (u8)read_csr(dd, ASIC_CFG_MUTEX);
1410 if (user == mask)
1411 return 0; /* success */
1412 if (time_after(jiffies, timeout))
1413 break; /* timed out */
1414 msleep(20);
1415 }
1416
1417 /* timed out */
1418 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001419 "Unable to acquire hardware mutex, mutex mask %u, my mask %u (%s)\n",
1420 (u32)user, (u32)mask, (try == 0) ? "retrying" : "giving up");
Mike Marciniszyn77241052015-07-30 15:17:43 -04001421
1422 if (try == 0) {
1423 /* break mutex and retry */
1424 write_csr(dd, ASIC_CFG_MUTEX, 0);
1425 try++;
1426 goto retry;
1427 }
1428
1429 return -EBUSY;
1430}
1431
1432void release_hw_mutex(struct hfi1_devdata *dd)
1433{
1434 write_csr(dd, ASIC_CFG_MUTEX, 0);
1435}
1436
Dean Luicka2ee27a2016-03-05 08:49:50 -08001437/* return the given resource bit(s) as a mask for the given HFI */
1438static inline u64 resource_mask(u32 hfi1_id, u32 resource)
1439{
1440 return ((u64)resource) << (hfi1_id ? CR_DYN_SHIFT : 0);
1441}
1442
1443static void fail_mutex_acquire_message(struct hfi1_devdata *dd,
1444 const char *func)
1445{
1446 dd_dev_err(dd,
1447 "%s: hardware mutex stuck - suggest rebooting the machine\n",
1448 func);
1449}
1450
1451/*
1452 * Acquire access to a chip resource.
1453 *
1454 * Return 0 on success, -EBUSY if resource busy, -EIO if mutex acquire failed.
1455 */
1456static int __acquire_chip_resource(struct hfi1_devdata *dd, u32 resource)
1457{
1458 u64 scratch0, all_bits, my_bit;
1459 int ret;
1460
1461 if (resource & CR_DYN_MASK) {
1462 /* a dynamic resource is in use if either HFI has set the bit */
Dean Luick90315ad2016-04-12 11:26:21 -07001463 if (dd->pcidev->device == PCI_DEVICE_ID_INTEL0 &&
1464 (resource & (CR_I2C1 | CR_I2C2))) {
1465 /* discrete devices must serialize across both chains */
1466 all_bits = resource_mask(0, CR_I2C1 | CR_I2C2) |
1467 resource_mask(1, CR_I2C1 | CR_I2C2);
1468 } else {
1469 all_bits = resource_mask(0, resource) |
Dean Luicka2ee27a2016-03-05 08:49:50 -08001470 resource_mask(1, resource);
Dean Luick90315ad2016-04-12 11:26:21 -07001471 }
Dean Luicka2ee27a2016-03-05 08:49:50 -08001472 my_bit = resource_mask(dd->hfi1_id, resource);
1473 } else {
1474 /* non-dynamic resources are not split between HFIs */
1475 all_bits = resource;
1476 my_bit = resource;
1477 }
1478
1479 /* lock against other callers within the driver wanting a resource */
1480 mutex_lock(&dd->asic_data->asic_resource_mutex);
1481
1482 ret = acquire_hw_mutex(dd);
1483 if (ret) {
1484 fail_mutex_acquire_message(dd, __func__);
1485 ret = -EIO;
1486 goto done;
1487 }
1488
1489 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
1490 if (scratch0 & all_bits) {
1491 ret = -EBUSY;
1492 } else {
1493 write_csr(dd, ASIC_CFG_SCRATCH, scratch0 | my_bit);
1494 /* force write to be visible to other HFI on another OS */
1495 (void)read_csr(dd, ASIC_CFG_SCRATCH);
1496 }
1497
1498 release_hw_mutex(dd);
1499
1500done:
1501 mutex_unlock(&dd->asic_data->asic_resource_mutex);
1502 return ret;
1503}
1504
1505/*
1506 * Acquire access to a chip resource, wait up to mswait milliseconds for
1507 * the resource to become available.
1508 *
1509 * Return 0 on success, -EBUSY if busy (even after wait), -EIO if mutex
1510 * acquire failed.
1511 */
1512int acquire_chip_resource(struct hfi1_devdata *dd, u32 resource, u32 mswait)
1513{
1514 unsigned long timeout;
1515 int ret;
1516
1517 timeout = jiffies + msecs_to_jiffies(mswait);
1518 while (1) {
1519 ret = __acquire_chip_resource(dd, resource);
1520 if (ret != -EBUSY)
1521 return ret;
1522 /* resource is busy, check our timeout */
1523 if (time_after_eq(jiffies, timeout))
1524 return -EBUSY;
1525 usleep_range(80, 120); /* arbitrary delay */
1526 }
1527}
1528
1529/*
1530 * Release access to a chip resource
1531 */
1532void release_chip_resource(struct hfi1_devdata *dd, u32 resource)
1533{
1534 u64 scratch0, bit;
1535
1536 /* only dynamic resources should ever be cleared */
1537 if (!(resource & CR_DYN_MASK)) {
1538 dd_dev_err(dd, "%s: invalid resource 0x%x\n", __func__,
1539 resource);
1540 return;
1541 }
1542 bit = resource_mask(dd->hfi1_id, resource);
1543
1544 /* lock against other callers within the driver wanting a resource */
1545 mutex_lock(&dd->asic_data->asic_resource_mutex);
1546
1547 if (acquire_hw_mutex(dd)) {
1548 fail_mutex_acquire_message(dd, __func__);
1549 goto done;
1550 }
1551
1552 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
1553 if ((scratch0 & bit) != 0) {
1554 scratch0 &= ~bit;
1555 write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
1556 /* force write to be visible to other HFI on another OS */
1557 (void)read_csr(dd, ASIC_CFG_SCRATCH);
1558 } else {
1559 dd_dev_warn(dd, "%s: id %d, resource 0x%x: bit not set\n",
1560 __func__, dd->hfi1_id, resource);
1561 }
1562
1563 release_hw_mutex(dd);
1564
1565done:
1566 mutex_unlock(&dd->asic_data->asic_resource_mutex);
1567}
1568
1569/*
1570 * Return true if resource is set, false otherwise. Print a warning
1571 * if not set and a function is supplied.
1572 */
1573bool check_chip_resource(struct hfi1_devdata *dd, u32 resource,
1574 const char *func)
1575{
1576 u64 scratch0, bit;
1577
1578 if (resource & CR_DYN_MASK)
1579 bit = resource_mask(dd->hfi1_id, resource);
1580 else
1581 bit = resource;
1582
1583 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
1584 if ((scratch0 & bit) == 0) {
1585 if (func)
1586 dd_dev_warn(dd,
1587 "%s: id %d, resource 0x%x, not acquired!\n",
1588 func, dd->hfi1_id, resource);
1589 return false;
1590 }
1591 return true;
1592}
1593
1594static void clear_chip_resources(struct hfi1_devdata *dd, const char *func)
1595{
1596 u64 scratch0;
1597
1598 /* lock against other callers within the driver wanting a resource */
1599 mutex_lock(&dd->asic_data->asic_resource_mutex);
1600
1601 if (acquire_hw_mutex(dd)) {
1602 fail_mutex_acquire_message(dd, func);
1603 goto done;
1604 }
1605
1606 /* clear all dynamic access bits for this HFI */
1607 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
1608 scratch0 &= ~resource_mask(dd->hfi1_id, CR_DYN_MASK);
1609 write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
1610 /* force write to be visible to other HFI on another OS */
1611 (void)read_csr(dd, ASIC_CFG_SCRATCH);
1612
1613 release_hw_mutex(dd);
1614
1615done:
1616 mutex_unlock(&dd->asic_data->asic_resource_mutex);
1617}
1618
1619void init_chip_resources(struct hfi1_devdata *dd)
1620{
1621 /* clear any holds left by us */
1622 clear_chip_resources(dd, __func__);
1623}
1624
1625void finish_chip_resources(struct hfi1_devdata *dd)
1626{
1627 /* clear any holds left by us */
1628 clear_chip_resources(dd, __func__);
1629}
1630
Mike Marciniszyn77241052015-07-30 15:17:43 -04001631void set_sbus_fast_mode(struct hfi1_devdata *dd)
1632{
1633 write_csr(dd, ASIC_CFG_SBUS_EXECUTE,
Jubin John17fb4f22016-02-14 20:21:52 -08001634 ASIC_CFG_SBUS_EXECUTE_FAST_MODE_SMASK);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001635}
1636
1637void clear_sbus_fast_mode(struct hfi1_devdata *dd)
1638{
1639 u64 reg, count = 0;
1640
1641 reg = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
1642 while (SBUS_COUNTER(reg, EXECUTE) !=
1643 SBUS_COUNTER(reg, RCV_DATA_VALID)) {
1644 if (count++ >= SBUS_MAX_POLL_COUNT)
1645 break;
1646 udelay(1);
1647 reg = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
1648 }
1649 write_csr(dd, ASIC_CFG_SBUS_EXECUTE, 0);
1650}
1651
1652int load_firmware(struct hfi1_devdata *dd)
1653{
1654 int ret;
1655
Easwar Hariharanabfc4452015-10-26 10:28:46 -04001656 if (fw_fabric_serdes_load) {
Dean Luick576531f2016-03-05 08:50:01 -08001657 ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001658 if (ret)
1659 return ret;
1660
1661 set_sbus_fast_mode(dd);
1662
Easwar Hariharanabfc4452015-10-26 10:28:46 -04001663 set_serdes_broadcast(dd, all_fabric_serdes_broadcast,
Jubin John17fb4f22016-02-14 20:21:52 -08001664 fabric_serdes_broadcast[dd->hfi1_id],
1665 fabric_serdes_addrs[dd->hfi1_id],
1666 NUM_FABRIC_SERDES);
Easwar Hariharanabfc4452015-10-26 10:28:46 -04001667 turn_off_spicos(dd, SPICO_FABRIC);
Dean Luickb3de8422015-12-01 15:38:10 -05001668 do {
1669 ret = load_fabric_serdes_firmware(dd, &fw_fabric);
1670 } while (retry_firmware(dd, ret));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001671
Mike Marciniszyn77241052015-07-30 15:17:43 -04001672 clear_sbus_fast_mode(dd);
Dean Luick576531f2016-03-05 08:50:01 -08001673 release_chip_resource(dd, CR_SBUS);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001674 if (ret)
1675 return ret;
1676 }
1677
1678 if (fw_8051_load) {
Dean Luickb3de8422015-12-01 15:38:10 -05001679 do {
1680 ret = load_8051_firmware(dd, &fw_8051);
1681 } while (retry_firmware(dd, ret));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001682 if (ret)
1683 return ret;
1684 }
1685
Dean Luickb3bf2702016-07-25 13:39:02 -07001686 dump_fw_version(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001687 return 0;
1688}
1689
1690int hfi1_firmware_init(struct hfi1_devdata *dd)
1691{
1692 /* only RTL can use these */
1693 if (dd->icode != ICODE_RTL_SILICON) {
1694 fw_fabric_serdes_load = 0;
1695 fw_pcie_serdes_load = 0;
1696 fw_sbus_load = 0;
1697 }
1698
1699 /* no 8051 or QSFP on simulator */
1700 if (dd->icode == ICODE_FUNCTIONAL_SIMULATOR) {
1701 fw_8051_load = 0;
1702 platform_config_load = 0;
1703 }
1704
1705 if (!fw_8051_name) {
1706 if (dd->icode == ICODE_RTL_SILICON)
1707 fw_8051_name = DEFAULT_FW_8051_NAME_ASIC;
1708 else
1709 fw_8051_name = DEFAULT_FW_8051_NAME_FPGA;
1710 }
1711 if (!fw_fabric_serdes_name)
1712 fw_fabric_serdes_name = DEFAULT_FW_FABRIC_NAME;
1713 if (!fw_sbus_name)
1714 fw_sbus_name = DEFAULT_FW_SBUS_NAME;
1715 if (!fw_pcie_serdes_name)
1716 fw_pcie_serdes_name = DEFAULT_FW_PCIE_NAME;
1717 if (!platform_config_name)
1718 platform_config_name = DEFAULT_PLATFORM_CONFIG_NAME;
1719
1720 return obtain_firmware(dd);
1721}
1722
Easwar Hariharan97167e82016-02-09 14:29:22 -08001723/*
1724 * This function is a helper function for parse_platform_config(...) and
1725 * does not check for validity of the platform configuration cache
1726 * (because we know it is invalid as we are building up the cache).
1727 * As such, this should not be called from anywhere other than
1728 * parse_platform_config
1729 */
1730static int check_meta_version(struct hfi1_devdata *dd, u32 *system_table)
1731{
1732 u32 meta_ver, meta_ver_meta, ver_start, ver_len, mask;
1733 struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
1734
1735 if (!system_table)
1736 return -EINVAL;
1737
1738 meta_ver_meta =
1739 *(pcfgcache->config_tables[PLATFORM_CONFIG_SYSTEM_TABLE].table_metadata
1740 + SYSTEM_TABLE_META_VERSION);
1741
1742 mask = ((1 << METADATA_TABLE_FIELD_START_LEN_BITS) - 1);
1743 ver_start = meta_ver_meta & mask;
1744
1745 meta_ver_meta >>= METADATA_TABLE_FIELD_LEN_SHIFT;
1746
1747 mask = ((1 << METADATA_TABLE_FIELD_LEN_LEN_BITS) - 1);
1748 ver_len = meta_ver_meta & mask;
1749
1750 ver_start /= 8;
1751 meta_ver = *((u8 *)system_table + ver_start) & ((1 << ver_len) - 1);
1752
1753 if (meta_ver < 5) {
1754 dd_dev_info(
1755 dd, "%s:Please update platform config\n", __func__);
1756 return -EINVAL;
1757 }
1758 return 0;
1759}
1760
Mike Marciniszyn77241052015-07-30 15:17:43 -04001761int parse_platform_config(struct hfi1_devdata *dd)
1762{
1763 struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
1764 u32 *ptr = NULL;
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001765 u32 header1 = 0, header2 = 0, magic_num = 0, crc = 0, file_length = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001766 u32 record_idx = 0, table_type = 0, table_length_dwords = 0;
Easwar Hariharan97167e82016-02-09 14:29:22 -08001767 int ret = -EINVAL; /* assume failure */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001768
Easwar Hariharanfe4d9242016-10-17 04:19:47 -07001769 /*
1770 * For integrated devices that did not fall back to the default file,
1771 * the SI tuning information for active channels is acquired from the
1772 * scratch register bitmap, thus there is no platform config to parse.
1773 * Skip parsing in these situations.
1774 */
1775 if (is_integrated(dd) && !platform_config_load)
1776 return 0;
1777
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001778 if (!dd->platform_config.data) {
Easwar Hariharanfe4d9242016-10-17 04:19:47 -07001779 dd_dev_err(dd, "%s: Missing config file\n", __func__);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001780 goto bail;
1781 }
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001782 ptr = (u32 *)dd->platform_config.data;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001783
1784 magic_num = *ptr;
1785 ptr++;
1786 if (magic_num != PLATFORM_CONFIG_MAGIC_NUM) {
Easwar Hariharanfe4d9242016-10-17 04:19:47 -07001787 dd_dev_err(dd, "%s: Bad config file\n", __func__);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001788 goto bail;
1789 }
1790
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001791 /* Field is file size in DWORDs */
1792 file_length = (*ptr) * 4;
1793 ptr++;
1794
1795 if (file_length > dd->platform_config.size) {
1796 dd_dev_info(dd, "%s:File claims to be larger than read size\n",
1797 __func__);
1798 goto bail;
1799 } else if (file_length < dd->platform_config.size) {
Easwar Hariharan97167e82016-02-09 14:29:22 -08001800 dd_dev_info(dd,
1801 "%s:File claims to be smaller than read size, continuing\n",
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001802 __func__);
1803 }
1804 /* exactly equal, perfection */
1805
1806 /*
1807 * In both cases where we proceed, using the self-reported file length
1808 * is the safer option
1809 */
1810 while (ptr < (u32 *)(dd->platform_config.data + file_length)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001811 header1 = *ptr;
1812 header2 = *(ptr + 1);
1813 if (header1 != ~header2) {
Easwar Hariharanfe4d9242016-10-17 04:19:47 -07001814 dd_dev_err(dd, "%s: Failed validation at offset %ld\n",
1815 __func__, (ptr - (u32 *)
1816 dd->platform_config.data));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001817 goto bail;
1818 }
1819
1820 record_idx = *ptr &
1821 ((1 << PLATFORM_CONFIG_HEADER_RECORD_IDX_LEN_BITS) - 1);
1822
1823 table_length_dwords = (*ptr >>
1824 PLATFORM_CONFIG_HEADER_TABLE_LENGTH_SHIFT) &
1825 ((1 << PLATFORM_CONFIG_HEADER_TABLE_LENGTH_LEN_BITS) - 1);
1826
1827 table_type = (*ptr >> PLATFORM_CONFIG_HEADER_TABLE_TYPE_SHIFT) &
1828 ((1 << PLATFORM_CONFIG_HEADER_TABLE_TYPE_LEN_BITS) - 1);
1829
1830 /* Done with this set of headers */
1831 ptr += 2;
1832
1833 if (record_idx) {
1834 /* data table */
1835 switch (table_type) {
1836 case PLATFORM_CONFIG_SYSTEM_TABLE:
1837 pcfgcache->config_tables[table_type].num_table =
1838 1;
Easwar Hariharan97167e82016-02-09 14:29:22 -08001839 ret = check_meta_version(dd, ptr);
1840 if (ret)
1841 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001842 break;
1843 case PLATFORM_CONFIG_PORT_TABLE:
1844 pcfgcache->config_tables[table_type].num_table =
1845 2;
1846 break;
1847 case PLATFORM_CONFIG_RX_PRESET_TABLE:
1848 /* fall through */
1849 case PLATFORM_CONFIG_TX_PRESET_TABLE:
1850 /* fall through */
1851 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
1852 /* fall through */
1853 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
1854 pcfgcache->config_tables[table_type].num_table =
1855 table_length_dwords;
1856 break;
1857 default:
Easwar Hariharanfe4d9242016-10-17 04:19:47 -07001858 dd_dev_err(dd,
1859 "%s: Unknown data table %d, offset %ld\n",
1860 __func__, table_type,
1861 (ptr - (u32 *)
1862 dd->platform_config.data));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001863 goto bail; /* We don't trust this file now */
1864 }
1865 pcfgcache->config_tables[table_type].table = ptr;
1866 } else {
1867 /* metadata table */
1868 switch (table_type) {
1869 case PLATFORM_CONFIG_SYSTEM_TABLE:
1870 /* fall through */
1871 case PLATFORM_CONFIG_PORT_TABLE:
1872 /* fall through */
1873 case PLATFORM_CONFIG_RX_PRESET_TABLE:
1874 /* fall through */
1875 case PLATFORM_CONFIG_TX_PRESET_TABLE:
1876 /* fall through */
1877 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
1878 /* fall through */
1879 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
1880 break;
1881 default:
Easwar Hariharanfe4d9242016-10-17 04:19:47 -07001882 dd_dev_err(dd,
1883 "%s: Unknown meta table %d, offset %ld\n",
1884 __func__, table_type,
1885 (ptr -
1886 (u32 *)dd->platform_config.data));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001887 goto bail; /* We don't trust this file now */
1888 }
1889 pcfgcache->config_tables[table_type].table_metadata =
1890 ptr;
1891 }
1892
1893 /* Calculate and check table crc */
1894 crc = crc32_le(~(u32)0, (unsigned char const *)ptr,
Jubin John17fb4f22016-02-14 20:21:52 -08001895 (table_length_dwords * 4));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001896 crc ^= ~(u32)0;
1897
1898 /* Jump the table */
1899 ptr += table_length_dwords;
1900 if (crc != *ptr) {
Easwar Hariharanfe4d9242016-10-17 04:19:47 -07001901 dd_dev_err(dd, "%s: Failed CRC check at offset %ld\n",
1902 __func__, (ptr -
1903 (u32 *)dd->platform_config.data));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001904 goto bail;
1905 }
1906 /* Jump the CRC DWORD */
1907 ptr++;
1908 }
1909
1910 pcfgcache->cache_valid = 1;
1911 return 0;
1912bail:
1913 memset(pcfgcache, 0, sizeof(struct platform_config_cache));
Easwar Hariharan97167e82016-02-09 14:29:22 -08001914 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001915}
1916
Easwar Hariharanfe4d9242016-10-17 04:19:47 -07001917static void get_integrated_platform_config_field(
1918 struct hfi1_devdata *dd,
1919 enum platform_config_table_type_encoding table_type,
1920 int field_index, u32 *data)
1921{
1922 struct hfi1_pportdata *ppd = dd->pport;
1923 u8 *cache = ppd->qsfp_info.cache;
1924 u32 tx_preset = 0;
1925
1926 switch (table_type) {
1927 case PLATFORM_CONFIG_SYSTEM_TABLE:
1928 if (field_index == SYSTEM_TABLE_QSFP_POWER_CLASS_MAX)
1929 *data = ppd->max_power_class;
1930 else if (field_index == SYSTEM_TABLE_QSFP_ATTENUATION_DEFAULT_25G)
1931 *data = ppd->default_atten;
1932 break;
1933 case PLATFORM_CONFIG_PORT_TABLE:
1934 if (field_index == PORT_TABLE_PORT_TYPE)
1935 *data = ppd->port_type;
1936 else if (field_index == PORT_TABLE_LOCAL_ATTEN_25G)
1937 *data = ppd->local_atten;
1938 else if (field_index == PORT_TABLE_REMOTE_ATTEN_25G)
1939 *data = ppd->remote_atten;
1940 break;
1941 case PLATFORM_CONFIG_RX_PRESET_TABLE:
1942 if (field_index == RX_PRESET_TABLE_QSFP_RX_CDR_APPLY)
1943 *data = (ppd->rx_preset & QSFP_RX_CDR_APPLY_SMASK) >>
1944 QSFP_RX_CDR_APPLY_SHIFT;
1945 else if (field_index == RX_PRESET_TABLE_QSFP_RX_EMP_APPLY)
1946 *data = (ppd->rx_preset & QSFP_RX_EMP_APPLY_SMASK) >>
1947 QSFP_RX_EMP_APPLY_SHIFT;
1948 else if (field_index == RX_PRESET_TABLE_QSFP_RX_AMP_APPLY)
1949 *data = (ppd->rx_preset & QSFP_RX_AMP_APPLY_SMASK) >>
1950 QSFP_RX_AMP_APPLY_SHIFT;
1951 else if (field_index == RX_PRESET_TABLE_QSFP_RX_CDR)
1952 *data = (ppd->rx_preset & QSFP_RX_CDR_SMASK) >>
1953 QSFP_RX_CDR_SHIFT;
1954 else if (field_index == RX_PRESET_TABLE_QSFP_RX_EMP)
1955 *data = (ppd->rx_preset & QSFP_RX_EMP_SMASK) >>
1956 QSFP_RX_EMP_SHIFT;
1957 else if (field_index == RX_PRESET_TABLE_QSFP_RX_AMP)
1958 *data = (ppd->rx_preset & QSFP_RX_AMP_SMASK) >>
1959 QSFP_RX_AMP_SHIFT;
1960 break;
1961 case PLATFORM_CONFIG_TX_PRESET_TABLE:
1962 if (cache[QSFP_EQ_INFO_OFFS] & 0x4)
1963 tx_preset = ppd->tx_preset_eq;
1964 else
1965 tx_preset = ppd->tx_preset_noeq;
1966 if (field_index == TX_PRESET_TABLE_PRECUR)
1967 *data = (tx_preset & TX_PRECUR_SMASK) >>
1968 TX_PRECUR_SHIFT;
1969 else if (field_index == TX_PRESET_TABLE_ATTN)
1970 *data = (tx_preset & TX_ATTN_SMASK) >>
1971 TX_ATTN_SHIFT;
1972 else if (field_index == TX_PRESET_TABLE_POSTCUR)
1973 *data = (tx_preset & TX_POSTCUR_SMASK) >>
1974 TX_POSTCUR_SHIFT;
1975 else if (field_index == TX_PRESET_TABLE_QSFP_TX_CDR_APPLY)
1976 *data = (tx_preset & QSFP_TX_CDR_APPLY_SMASK) >>
1977 QSFP_TX_CDR_APPLY_SHIFT;
1978 else if (field_index == TX_PRESET_TABLE_QSFP_TX_EQ_APPLY)
1979 *data = (tx_preset & QSFP_TX_EQ_APPLY_SMASK) >>
1980 QSFP_TX_EQ_APPLY_SHIFT;
1981 else if (field_index == TX_PRESET_TABLE_QSFP_TX_CDR)
1982 *data = (tx_preset & QSFP_TX_CDR_SMASK) >>
1983 QSFP_TX_CDR_SHIFT;
1984 else if (field_index == TX_PRESET_TABLE_QSFP_TX_EQ)
1985 *data = (tx_preset & QSFP_TX_EQ_SMASK) >>
1986 QSFP_TX_EQ_SHIFT;
1987 break;
1988 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
1989 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
1990 default:
1991 break;
1992 }
1993}
1994
Mike Marciniszyn77241052015-07-30 15:17:43 -04001995static int get_platform_fw_field_metadata(struct hfi1_devdata *dd, int table,
Jubin John17fb4f22016-02-14 20:21:52 -08001996 int field, u32 *field_len_bits,
1997 u32 *field_start_bits)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001998{
1999 struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
2000 u32 *src_ptr = NULL;
2001
2002 if (!pcfgcache->cache_valid)
2003 return -EINVAL;
2004
2005 switch (table) {
2006 case PLATFORM_CONFIG_SYSTEM_TABLE:
2007 /* fall through */
2008 case PLATFORM_CONFIG_PORT_TABLE:
2009 /* fall through */
2010 case PLATFORM_CONFIG_RX_PRESET_TABLE:
2011 /* fall through */
2012 case PLATFORM_CONFIG_TX_PRESET_TABLE:
2013 /* fall through */
2014 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
2015 /* fall through */
2016 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
2017 if (field && field < platform_config_table_limits[table])
2018 src_ptr =
2019 pcfgcache->config_tables[table].table_metadata + field;
2020 break;
2021 default:
2022 dd_dev_info(dd, "%s: Unknown table\n", __func__);
2023 break;
2024 }
2025
2026 if (!src_ptr)
2027 return -EINVAL;
2028
2029 if (field_start_bits)
2030 *field_start_bits = *src_ptr &
2031 ((1 << METADATA_TABLE_FIELD_START_LEN_BITS) - 1);
2032
2033 if (field_len_bits)
2034 *field_len_bits = (*src_ptr >> METADATA_TABLE_FIELD_LEN_SHIFT)
2035 & ((1 << METADATA_TABLE_FIELD_LEN_LEN_BITS) - 1);
2036
2037 return 0;
2038}
2039
2040/* This is the central interface to getting data out of the platform config
2041 * file. It depends on parse_platform_config() having populated the
2042 * platform_config_cache in hfi1_devdata, and checks the cache_valid member to
2043 * validate the sanity of the cache.
2044 *
2045 * The non-obvious parameters:
2046 * @table_index: Acts as a look up key into which instance of the tables the
2047 * relevant field is fetched from.
2048 *
2049 * This applies to the data tables that have multiple instances. The port table
2050 * is an exception to this rule as each HFI only has one port and thus the
2051 * relevant table can be distinguished by hfi_id.
2052 *
2053 * @data: pointer to memory that will be populated with the field requested.
2054 * @len: length of memory pointed by @data in bytes.
2055 */
2056int get_platform_config_field(struct hfi1_devdata *dd,
Jubin John17fb4f22016-02-14 20:21:52 -08002057 enum platform_config_table_type_encoding
2058 table_type, int table_index, int field_index,
2059 u32 *data, u32 len)
Mike Marciniszyn77241052015-07-30 15:17:43 -04002060{
2061 int ret = 0, wlen = 0, seek = 0;
2062 u32 field_len_bits = 0, field_start_bits = 0, *src_ptr = NULL;
2063 struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
2064
2065 if (data)
2066 memset(data, 0, len);
2067 else
2068 return -EINVAL;
2069
Easwar Hariharanfe4d9242016-10-17 04:19:47 -07002070 if (is_integrated(dd) && !platform_config_load) {
2071 /*
2072 * Use saved configuration from ppd for integrated platforms
2073 */
2074 get_integrated_platform_config_field(dd, table_type,
2075 field_index, data);
2076 return 0;
2077 }
2078
Mike Marciniszyn77241052015-07-30 15:17:43 -04002079 ret = get_platform_fw_field_metadata(dd, table_type, field_index,
Jubin John17fb4f22016-02-14 20:21:52 -08002080 &field_len_bits,
2081 &field_start_bits);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002082 if (ret)
2083 return -EINVAL;
2084
2085 /* Convert length to bits */
2086 len *= 8;
2087
2088 /* Our metadata function checked cache_valid and field_index for us */
2089 switch (table_type) {
2090 case PLATFORM_CONFIG_SYSTEM_TABLE:
2091 src_ptr = pcfgcache->config_tables[table_type].table;
2092
2093 if (field_index != SYSTEM_TABLE_QSFP_POWER_CLASS_MAX) {
2094 if (len < field_len_bits)
2095 return -EINVAL;
2096
Jubin John8638b772016-02-14 20:19:24 -08002097 seek = field_start_bits / 8;
2098 wlen = field_len_bits / 8;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002099
2100 src_ptr = (u32 *)((u8 *)src_ptr + seek);
2101
Jubin John4d114fd2016-02-14 20:21:43 -08002102 /*
2103 * We expect the field to be byte aligned and whole byte
2104 * lengths if we are here
2105 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04002106 memcpy(data, src_ptr, wlen);
2107 return 0;
2108 }
2109 break;
2110 case PLATFORM_CONFIG_PORT_TABLE:
Easwar Hariharanc3838b32016-02-09 14:29:13 -08002111 /* Port table is 4 DWORDS */
Mike Marciniszyn77241052015-07-30 15:17:43 -04002112 src_ptr = dd->hfi1_id ?
2113 pcfgcache->config_tables[table_type].table + 4 :
2114 pcfgcache->config_tables[table_type].table;
2115 break;
2116 case PLATFORM_CONFIG_RX_PRESET_TABLE:
2117 /* fall through */
2118 case PLATFORM_CONFIG_TX_PRESET_TABLE:
2119 /* fall through */
2120 case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
2121 /* fall through */
2122 case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
2123 src_ptr = pcfgcache->config_tables[table_type].table;
2124
2125 if (table_index <
2126 pcfgcache->config_tables[table_type].num_table)
2127 src_ptr += table_index;
2128 else
2129 src_ptr = NULL;
2130 break;
2131 default:
2132 dd_dev_info(dd, "%s: Unknown table\n", __func__);
2133 break;
2134 }
2135
2136 if (!src_ptr || len < field_len_bits)
2137 return -EINVAL;
2138
Jubin John8638b772016-02-14 20:19:24 -08002139 src_ptr += (field_start_bits / 32);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002140 *data = (*src_ptr >> (field_start_bits % 32)) &
2141 ((1 << field_len_bits) - 1);
2142
2143 return 0;
2144}
2145
2146/*
2147 * Download the firmware needed for the Gen3 PCIe SerDes. An update
2148 * to the SBus firmware is needed before updating the PCIe firmware.
2149 *
Dean Luick576531f2016-03-05 08:50:01 -08002150 * Note: caller must be holding the SBus resource.
Mike Marciniszyn77241052015-07-30 15:17:43 -04002151 */
2152int load_pcie_firmware(struct hfi1_devdata *dd)
2153{
2154 int ret = 0;
2155
2156 /* both firmware loads below use the SBus */
2157 set_sbus_fast_mode(dd);
2158
Dean Luick65fcf552015-11-06 20:06:59 -05002159 if (fw_sbus_load) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04002160 turn_off_spicos(dd, SPICO_SBUS);
Dean Luickb3de8422015-12-01 15:38:10 -05002161 do {
2162 ret = load_sbus_firmware(dd, &fw_sbus);
2163 } while (retry_firmware(dd, ret));
Mike Marciniszyn77241052015-07-30 15:17:43 -04002164 if (ret)
2165 goto done;
2166 }
2167
2168 if (fw_pcie_serdes_load) {
2169 dd_dev_info(dd, "Setting PCIe SerDes broadcast\n");
2170 set_serdes_broadcast(dd, all_pcie_serdes_broadcast,
Jubin John17fb4f22016-02-14 20:21:52 -08002171 pcie_serdes_broadcast[dd->hfi1_id],
2172 pcie_serdes_addrs[dd->hfi1_id],
2173 NUM_PCIE_SERDES);
Dean Luickb3de8422015-12-01 15:38:10 -05002174 do {
2175 ret = load_pcie_serdes_firmware(dd, &fw_pcie);
2176 } while (retry_firmware(dd, ret));
Mike Marciniszyn77241052015-07-30 15:17:43 -04002177 if (ret)
2178 goto done;
2179 }
2180
2181done:
2182 clear_sbus_fast_mode(dd);
2183
2184 return ret;
2185}
2186
2187/*
2188 * Read the GUID from the hardware, store it in dd.
2189 */
2190void read_guid(struct hfi1_devdata *dd)
2191{
Easwar Hariharan7c03ed82015-10-26 10:28:28 -04002192 /* Take the DC out of reset to get a valid GUID value */
2193 write_csr(dd, CCE_DC_CTRL, 0);
Jubin John50e5dcb2016-02-14 20:19:41 -08002194 (void)read_csr(dd, CCE_DC_CTRL);
Easwar Hariharan7c03ed82015-10-26 10:28:28 -04002195
Mike Marciniszyn77241052015-07-30 15:17:43 -04002196 dd->base_guid = read_csr(dd, DC_DC8051_CFG_LOCAL_GUID);
2197 dd_dev_info(dd, "GUID %llx",
Jubin John17fb4f22016-02-14 20:21:52 -08002198 (unsigned long long)dd->base_guid);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002199}
Dean Luickb3bf2702016-07-25 13:39:02 -07002200
2201/* read and display firmware version info */
2202static void dump_fw_version(struct hfi1_devdata *dd)
2203{
2204 u32 pcie_vers[NUM_PCIE_SERDES];
2205 u32 fabric_vers[NUM_FABRIC_SERDES];
2206 u32 sbus_vers;
2207 int i;
2208 int all_same;
2209 int ret;
2210 u8 rcv_addr;
2211
2212 ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
2213 if (ret) {
2214 dd_dev_err(dd, "Unable to acquire SBus to read firmware versions\n");
2215 return;
2216 }
2217
2218 /* set fast mode */
2219 set_sbus_fast_mode(dd);
2220
2221 /* read version for SBus Master */
2222 sbus_request(dd, SBUS_MASTER_BROADCAST, 0x02, WRITE_SBUS_RECEIVER, 0);
2223 sbus_request(dd, SBUS_MASTER_BROADCAST, 0x07, WRITE_SBUS_RECEIVER, 0x1);
2224 /* wait for interrupt to be processed */
2225 usleep_range(10000, 11000);
2226 sbus_vers = sbus_read(dd, SBUS_MASTER_BROADCAST, 0x08, 0x1);
2227 dd_dev_info(dd, "SBus Master firmware version 0x%08x\n", sbus_vers);
2228
2229 /* read version for PCIe SerDes */
2230 all_same = 1;
2231 pcie_vers[0] = 0;
2232 for (i = 0; i < NUM_PCIE_SERDES; i++) {
2233 rcv_addr = pcie_serdes_addrs[dd->hfi1_id][i];
2234 sbus_request(dd, rcv_addr, 0x03, WRITE_SBUS_RECEIVER, 0);
2235 /* wait for interrupt to be processed */
2236 usleep_range(10000, 11000);
2237 pcie_vers[i] = sbus_read(dd, rcv_addr, 0x04, 0x0);
2238 if (i > 0 && pcie_vers[0] != pcie_vers[i])
2239 all_same = 0;
2240 }
2241
2242 if (all_same) {
2243 dd_dev_info(dd, "PCIe SerDes firmware version 0x%x\n",
2244 pcie_vers[0]);
2245 } else {
2246 dd_dev_warn(dd, "PCIe SerDes do not have the same firmware version\n");
2247 for (i = 0; i < NUM_PCIE_SERDES; i++) {
2248 dd_dev_info(dd,
2249 "PCIe SerDes lane %d firmware version 0x%x\n",
2250 i, pcie_vers[i]);
2251 }
2252 }
2253
2254 /* read version for fabric SerDes */
2255 all_same = 1;
2256 fabric_vers[0] = 0;
2257 for (i = 0; i < NUM_FABRIC_SERDES; i++) {
2258 rcv_addr = fabric_serdes_addrs[dd->hfi1_id][i];
2259 sbus_request(dd, rcv_addr, 0x03, WRITE_SBUS_RECEIVER, 0);
2260 /* wait for interrupt to be processed */
2261 usleep_range(10000, 11000);
2262 fabric_vers[i] = sbus_read(dd, rcv_addr, 0x04, 0x0);
2263 if (i > 0 && fabric_vers[0] != fabric_vers[i])
2264 all_same = 0;
2265 }
2266
2267 if (all_same) {
2268 dd_dev_info(dd, "Fabric SerDes firmware version 0x%x\n",
2269 fabric_vers[0]);
2270 } else {
2271 dd_dev_warn(dd, "Fabric SerDes do not have the same firmware version\n");
2272 for (i = 0; i < NUM_FABRIC_SERDES; i++) {
2273 dd_dev_info(dd,
2274 "Fabric SerDes lane %d firmware version 0x%x\n",
2275 i, fabric_vers[i]);
2276 }
2277 }
2278
2279 clear_sbus_fast_mode(dd);
2280 release_chip_resource(dd, CR_SBUS);
2281}